C语言 如何使用指针对二维数组的每一行求和?我的代码有什么问题?

wnrlj8wa  于 2022-12-26  发布在  其他
关注(0)|答案(1)|浏览(196)

我试着写一段代码,从5个评委那里获得电影评分,并将它们存储在一个2D数组中。接下来,我将使用指针将每部电影的所有评分相加,并将它们存储在一个名为sumArray的单独数组中。
下面是我的尝试:

#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 itteration
            sum = sum + (*(p+j+(i*5)));
            sumArray[i] = sum;
        }
    }

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

getch();
}

我必须使用指针/指针算法来实现这一点。我尝试了上面的代码,但是当我打印出sumarray时,我没有得到每部电影的收视率之和。

dgsult0t

dgsult0t1#

台词

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

错误。如果希望它指向外部数组的第一个元素,则应将

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

而不是。
int (*p)[5];行将声明一个指向5个int元素的数组的指针(而不是指向这些元素中的单个元素的指针)。
请注意,由于array to pointer decay的原因,

p = movie_Scores;

等同于:

p = &movie_Scores[0];

还有,在排队的时候

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

表达式(p+i+(j*5))是错误的。它应该是&movie_Scores[j][i],它与&p[j][i]相同,或者,如果必须使用指针表示法,它应该是&*(*(p+j)+i),它简单地是*(p+j)+i,因为&*相互抵消。
您在程序的其他4个地方也使用了不正确的表达式(p+i+(j*5))
另一个问题是,在第一组循环中,外部循环计数为5,内部循环计数为8,但在第二组循环中,情况正好相反:外循环计数为8,内循环计数为5
此外,第三个循环计数为8而不是5,因此访问的2D数组的外部数组超出了界限。
另一个问题是下面一行的位置:

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

你把它放在内环里面,但是它应该放在外环里面。
应用这些修复后,程序应如下所示:

#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]);
    }
}

现在,您的程序具有正确的输出:

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

相关问题