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.