1

I am trying to write a code that will take movie ratings from 5 judges and store them in a 2D array. Next i will add all the ratings for each individual movie using pointers and store them in a separate array called sumArray.

Here is my attempt:

#include <stdio.h>

int main()
{
    int movie_Scores[8][5]; //Declaration of a 2D array
    int *p; //Declaration of a pointer variable
    p=&movie_Scores; // holds the address of 1st element of movie_Scores
    int sumArray[8];
    int sum;

    //Array of pointers to strings and initializtion
    char movie_Names[][100] =      {"1. Movie 1",
                                    "2. Movie 2",
                                    "3. Movie 3",
                                    "4. Movie 4",
                                    "5. Movie 5",
                                    "6. Movie 6",
                                    "7. Movie 7",
                                    "8. Movie 8"
                                   };

    for(int i=0; i<5; i++)
    {
        printf("Judge %d, rate each movie from 1-10:\n\n", i+1);
        for(int j=0;j<8;j++)
        {
            printf("%s:\t\t", movie_Names[j]);
            scanf("%d", (p+i+(j*5)));

            while(*(p+i+(j*5))<1 || *(p+i+(j*5))>10)
            {
                printf("\n!!Enter number between 1-10!!\n");
                printf("\n%s:\t\t", movie_Names[j]);
                scanf("%d", (p+i+(j*5)));
            }
        }
        printf("\n\n");
    }

    for(int i=0; i<8; i++)
    {
        for(int j=0; j<5; j++)
        {
            sum=0; //re-initializing sum to 0 for each iteration
            sum = sum + (*(p+j+(i*5)));
            sumArray[i] = sum;
        }
    }

    for(int i=0; i<8 ; i++)
    {
        printf ("%d\n", sumArray[i]);
    }

getch();
}

i have to achieve this using pointers/pointer arithmetic. I tried the above code, but however when I print out sumArray, I dont get the sum of ratings for each individual movie.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Habib Khan
  • 37
  • 6
  • 1
    Note that the type of `&movie_Scores` is `int (*)[8][5]`, it's not a pointer to the first element, that would be `&movie_Scores[0][0]` (which also have the type `int *`). The assignment to `p` should get complaints from the compiler. – Some programmer dude Dec 24 '22 at 09:55
  • And why are you using pointers and pointer arithmetic? What is your original and actual assignment or exercise? Please [edit] your question to copy-paste the assignment-text (as text) including all limitations and requirements. – Some programmer dude Dec 24 '22 at 09:58
  • @Someprogrammerdude i have to use pointers to write a program which will take ratings from 5 judges, and sum them. The use of pointers in necessary – Habib Khan Dec 24 '22 at 10:14
  • 1
    For any array *or pointer* `a` and index `i`, the expression `a[i]` is exactly equal to `*(a + i)`. So even when using "array" syntax there's an implicit pointer arithmetic operation. What requirements do you have to make it explicit? – Some programmer dude Dec 24 '22 at 10:40
  • @Someprogrammerdude i'v made some changes to make the question more clear. where is your confusion? – Habib Khan Dec 24 '22 at 10:43
  • What @Someprogrammerdude is saying is that `a[i]` is, by definition of the `[]` operator, 100% identical to `*(a + i)`. Therefore, `a[i]` is also using a pointer and pointer arithmetic. That is just a shorter way of writing the same thing. Is it so that you are allowed to write `*(a + i)`, but not `a[i]`? – Andreas Wenzel Dec 24 '22 at 10:49
  • @AndreasWenzel ahh yes, i'd prefer to write it in its base form: *(a+i) instead of a[i]. But the main problem here is that I'm trying to sum each element of the rows, and store them in a seperate array However it is not working and i'm unable to debug what i am doing wrong – Habib Khan Dec 24 '22 at 11:11
  • 2
    I suggest you *start* with the normal array indexing. Make sure it builds cleanly (even with extra warnings enabled) and that everything works. *Then* you start replacing the array indexes with pointer arithmetic, one by one, with testing in between. If there's problems you [*debug*](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) the program to find out the problem. – Some programmer dude Dec 24 '22 at 11:20
  • comments like `int *p; //Declaration of a pointer variable` are not very descriptive – tstanisl Jan 24 '23 at 17:02

1 Answers1

3

The lines

int *p; //Declaration of a pointer variable
p=&movie_Scores; // holds the address of 1st element of movie_Scores

are wrong. If you want it to point to the first element of the outer array, then you should write

int (*p)[5]; //Declaration of a pointer variable
p = movie_Scores; // holds the address of 1st element of movie_Scores

instead.

The line int (*p)[5]; will declare a pointer to an array of 5 int elements (instead of a pointer to a single one of these elements).

Note that due to array to pointer decay, the line

p = movie_Scores;

is identical to:

p = &movie_Scores[0];

Also, in the line

scanf("%d", (p+i+(j*5)));

the expression (p+i+(j*5)) is wrong. It should be, &movie_Scores[j][i], which is identical to &p[j][i] or, if you must use pointer notation, &*(*(p+j)+i), which is simply *(p+j)+i, because the & and * cancel each other out.

You are using the incorrect expression (p+i+(j*5)) also in 4 other places in your program.

Another problem is that in your first set of loops, the outer loop counts to 5 and the inner loop counts to 8, but in the second set of loops, it is the other way around: The outer loop counts to 8 and the inner loop counts to 5.

Also, your third loop counts to 8 instead of 5, thereby accessing the outer array of the 2D array out of bounds.

Another problem is the position of the following line:

sum=0; //re-initializing sum to 0 for each iteration

You have it inside the inner loop, but it should be in the outer loop.

After applying these fixes, your program should look like this:

#include <stdio.h>

int main()
{
    int movie_Scores[8][5]; //Declaration of a 2D array
    int (*p)[5]; //Declaration of a pointer variable
    p = movie_Scores; // holds the address of 1st element of movie_Scores
    int sumArray[8];
    int sum;

    //Array of pointers to strings and initializtion
    char movie_Names[][100] =      {"1. Movie 1",
                                    "2. Movie 2",
                                    "3. Movie 3",
                                    "4. Movie 4",
                                    "5. Movie 5",
                                    "6. Movie 6",
                                    "7. Movie 7",
                                    "8. Movie 8"
                                   };

    for(int i=0; i<5; i++)
    {
        printf("Judge %d, rate each movie from 1-10:\n\n", i+1);
        for(int j=0;j<8;j++)
        {
            printf("%s:\t\t", movie_Names[j]);
            scanf("%d", *(p+j)+i );

            while( *(*(p+j)+i) < 1 || *(*(p+j)+i) > 10)
            {
                printf("\n!!Enter number between 1-10!!\n");
                printf("\n%s:\t\t", movie_Names[j]);
                scanf("%d", *(p+j)+i );
            }
        }
        printf("\n\n");
    }

    for(int i=0; i<5; i++)
    {
        sum=0; //re-initializing sum to 0 for each iteration

        for(int j=0; j<8; j++)
        {
            sum = sum + *(*(p+j)+i);
            sumArray[i] = sum;
        }
    }

    for(int i=0; i<5 ; i++)
    {
        printf ("%d\n", sumArray[i]);
    }
}

Your program now has the correct output:

Judge 1, rate each movie from 1-10:

1. Movie 1:             1
2. Movie 2:             1
3. Movie 3:             1
4. Movie 4:             1
5. Movie 5:             1
6. Movie 6:             1
7. Movie 7:             1
8. Movie 8:             1


Judge 2, rate each movie from 1-10:

1. Movie 1:             2
2. Movie 2:             2
3. Movie 3:             2
4. Movie 4:             2
5. Movie 5:             2
6. Movie 6:             2
7. Movie 7:             2
8. Movie 8:             2


Judge 3, rate each movie from 1-10:

1. Movie 1:             3
2. Movie 2:             3
3. Movie 3:             3
4. Movie 4:             3
5. Movie 5:             3
6. Movie 6:             3
7. Movie 7:             3
8. Movie 8:             3


Judge 4, rate each movie from 1-10:

1. Movie 1:             4
2. Movie 2:             4
3. Movie 3:             4
4. Movie 4:             4
5. Movie 5:             4
6. Movie 6:             4
7. Movie 7:             4
8. Movie 8:             4


Judge 5, rate each movie from 1-10:

1. Movie 1:             5
2. Movie 2:             5
3. Movie 3:             5
4. Movie 4:             5
5. Movie 5:             5
6. Movie 6:             5
7. Movie 7:             5
8. Movie 8:             5


8
16
24
32
40
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39