c# 未知数组类型

wlzqhblo  于 2023-09-28  发布在  C#
关注(0)|答案(1)|浏览(241)

我需要遵守并获得结果的代码。//wap输入2个学生的分数并打印每个学生的平均分数;

#include<stdio.h>//foramte of c programming
void avg(arr[][],int a);//defined the function to calculate the average marks
int main(){//formate
   int marks[2][3];//defcalration of the arras with the space to be occpied
   for(int i=0;i<2;i++)//for the input of the numbers
   {
    printf("Enter the three subject marks of %d students:",i+1);
    for(int j=0;j<3;j++)
    {
        scanf("%d",marks[i][j]);
    } 
   }//end of the input
   int avg1=avg(marks,0);
   printf("The average marks of the 1st student is %d",avg1);
   int avg2=avg(marks,1);
   printf("The average marks of the 2nd student is %d",avg2);
   return 0;
}
void  avg(arr[][],int a)//function definition
{
    for( int i=a;i<a+1;i++)
    {
        for(int j=0;j<3;j++)
        {
            sum=sum+marks[i][j];
        }
        
    }
     int average=sum/3;
     return average;//return the average marks of the arra that is declared
}

在编译代码时,我得到了很多错误:错误也粘贴在这里:

averageof2students.c:3:10: error: unknown type name 'arr'
 void avg(arr[][],int a);//defined the function to calculate the average marks
          ^~~
averageof2students.c: In function 'main':
averageof2students.c:14:13: warning: implicit declaration of function 'avg' [-Wimplicit-function-declaration]
    int avg1=avg(marks,0);
             ^~~
averageof2students.c: At top level:
averageof2students.c:20:11: error: unknown type name 'arr'
 void  avg(arr[][],int a)//function definition`

请帮助我得到想要的结果。我是编程世界的初学者

从上面的结果我想要的结果的平均成绩的2名学生的3个不同的科目分数使用数组和函数的概念。我已经遵循了DSA(数据结构算法),但在完成时,我去了错误信息。请帮我解决这个问题。我在网上和书上搜索,但无法找到答案。

nom7f22z

nom7f22z1#

有很多问题:

更正码:

#include<stdio.h>//foramte of c programming

int avg(int arr[2][3], int a);  // (1)  see below

int main() {//formate
    int marks[2][3];//defcalration of the arras with the space to be occpied
    for (int i = 0; i < 2; i++)//for the input of the numbers
    {
        printf("Enter the three subject marks of %d students:", i + 1);
        for (int j = 0; j < 3; j++)
        {
            scanf("%d", &marks[i][j]);  // (2) see below
        }
    }//end of the input
    int avg1 = avg(marks, 0);
    printf("The average marks of the 1st student is %d", avg1);
    int avg2 = avg(marks, 1);
    printf("The average marks of the 2nd student is %d", avg2);
    return 0;
}

int  avg(int arr[2][3], int a)  // (1)  see below
{
    int sum = 0;   // (3)
    for (int i = a; i < a + 1; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            sum = sum + arr[i][j];  // (4) see below
        }

    }
    int average = sum / 3;
    return average; //return the average marks of the arra that is declared
}

说明:

1.函数定义错误。您需要将其声明为int,并且需要正确声明aa参数。
1.缺少&。仔细阅读学习资料中有关scanf的章节
1.您需要声明sum并在使用它之前将其初始化为0
1.在avg函数中使用的是arr,而不是marks

免责声明

我还没有检查是否正确的代码实际上工作,我把这个留给你。

相关问题