Habib Khan
Habib Khan

Reputation: 37

How do I sum each row of a 2D Array using pointers and what is wrong with my code?

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.

Upvotes: 1

Views: 86

Answers (1)

Andreas Wenzel
Andreas Wenzel

Reputation: 25021

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

Upvotes: 3

Related Questions