如何将这个结构数组传递给函数,我的代码怎么了?

时间:2012-04-15 13:03:59

标签: c arrays

问题是:

课程的特点如下:

Course Name: cName (string of 32 characters)
Course Number: cNumber (integer)
Number of Students enrolled: nStudents (integer)
Level: Level (character: ‘G’ for graduate, ‘U’ for undergraduate)
Grades: sGrades (array of 40 integers)

Define a structure, called CourseType, which includes the above properties.

In the main program: use the structure you defined to declare an array of 5 courses, and store in this array the information below about these courses (the information below specify the course name, number, number of students, and level:

“Introduction to Programming”, 230, 37, ‘U’
“Computer Networks”, 450, 44, ‘U’
“Data Structures & Algorithms”, 330, 38, ‘U’
“Distributed Databases”, 630, 18, ‘G’
“Mobile Ad hoc Networks”, 656, 34, ‘G’

关于成绩,对于每门课程,生成与学生一样多的随机值,并将其存储在成绩数组(sGrades)中。这些值应介于50和100之间。

Write a function DisplayCourseStatistics that takes as input a course structure, and computes the average grade, minimum grade, and maximum grade in the course. After computing these three values, the function displays them.

In the main program: after defining the structure and the five courses in part c, and populating these courses with the data (including the grades), ask the user to specify the course number. Then call the function DisplayCourseStatistics and pass it the corresponding course structure in order for it to display the grade statistics (average, minimum, and maximum).

我写的代码我仍然有错误= [

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct CourseType CourseType;
struct CourseType
{
char cName[32];
int cNumber;
int nStudents;
char Level;
int sGrades[44];
};
void DisplayCourseStatistics(CourseType course[],int n);
int main()
{
int i;
CourseType course[5];
strcpy(course[0].cName,"Introduction to Programming");
course[0].cNumber=230;
course[0].nStudents=37;
course[0].Level='U';
for(i=0;i<37;i++)
{
course[0].sGrades[i]=50+rand()%(50);
}
strcpy(course[1].cName,"Computer Networks");
course[1].cNumber=450;
course[1].nStudents=44;
course[1].Level='U';
for(i=0;i<44;i++)
{
course[1].sGrades[i]=50+rand()%(50);
}
strcpy(course[2].cName,"Data Structures & Algorithms");
course[2].cNumber=330;
course[2].nStudents=38;
course[2].Level='U';
for(i=0;i<38;i++)
{
course[2].sGrades[i]=50+rand()%(50);
}
strcpy(course[03].cName,"Distributed Databases");
course[3].cNumber=630;
course[3].nStudents=18;
course[3].Level='G';
for(i=0;i<18;i++)
{
course[3].sGrades[i]=50+rand()%(50);
}
strcpy(course[4].cName,"Mobile Ad hoc Networks");
course[4].cNumber=656;
course[4].nStudents=34;
course[4].Level='G';
for(i=0;i<34;i++)
{
course[4].sGrades[i]=50+rand()%(50);
}
int n;
printf("enter the course number that you'd like to display its stat");
scanf("%d",&n); 
DisplayCourseStatistics(CourseType course[n],n);
}
void DisplayCourseStatistics(CourseType course[n],int n)
{
int average_grade;
int minimum_grade;
int maximum_grade;
int sum=0;
int i;
for(i=0;i<course[n].nStudents;i++)   
{
sum+=course[n].sGrades[i];
if(course[n].sGrades[i]>course[n].sGrades[i+1])
{  
minimum_grade=course[n].sGrades[i+1];
maximum_grade=course[n].sGrades[i];
}
else if(course[n].sGrades[i]<course[n].sGrades[i+1])
{
minimum_grade=course[n].sGrades[i];
maximum_grade=course[n].sGrades[i+1];
}
}
printf("The Average Grade is %d\nThe Maximum Grade is %d\nThe Minimum Grade is %d\n",average_grade=(sum/course[n].nStudents),maximum_grade,minimum_grade);

}

什么是错的,我能做些什么 感谢!!!

评论 它应该是40但我使用44因为在一个课程中学生的数量是44这是> 40所以和错误实际上我一直是usinf键盘所以我不确定它说错误:函数'DisplayCourseStatistics'的参数太少

2 个答案:

答案 0 :(得分:1)

在这里,您需要像在数组的情况下一样传递结构数组。 考虑这一行:

DisplayCourseStatistics(CourseType course[n],n);

这是错误的。 您可以编写与数组类似的内容,只需将结构名称传递为:

DisplayCourseStatistics(course,n);

现在你需要确定你的功能:

void DisplayCourseStatistics(CourseType course[n],int n)

这应该是

void DisplayCourseStatistics(CourseType course[],int n)

现在结束,如果你需要传递一个结构或它的数组,你需要将指向fisrt成员的结构或指针传递给数组的结构数组。这就是你可以将结构变量传递给函数的方法。

答案 1 :(得分:0)

符合文字

任务是询问课程编号并显示该课程。截至目前,您要求数组索引并显示它。此外,您不应该发送索引和数组,只发送DisplayCourseStatistics()函数的特定过程。

正如您在问题标题中所说的那样“如何将此结构数组传递给函数[...]”; - 但这打破了任务,

你应该有:

                                           +--- Struct, not array of struct
                                           |
void DisplayCourseStatistics(CourseType course)
{
    ...

主要:

/* You can't specify type when calling */
DisplayCourseStatistics(CourseType course[n], n);
                        \__________________/
                                 |
                                 +--- Why do you do this?

/* Becomes: */
DisplayCourseStatistics(course);

然后你必须改变DisplayCourseStatistics()的原型和签名以符合这一点;如上所示。

完成此操作后,您必须修复该功能以不使用数组。

除此之外, Display -function中也有一个错误:当你同时.sGrades[i+1] i时,你超出了数组的范围}成为.sGrades[]中的项目数。


作为旁注:在使用换行符时没问题,即:

printf("The Average Grade is %d\n"
       "The Maximum Grade is %d\n"
       "The Minimum Grade is %d\n",
       average_grade=(sum/course[n].nStudents),
       maximum_grade,minimum_grade);

另外:您应该通过确保它返回1来验证scanf()(对于成功解析的1个项目,即“%d”)。

如果有人输入非整数,则无法“访问”n,因为它未初始化。

在成功读取数字后,您需要找到一种方法来“翻译”该数字以更正course数组的课程索引,并将其传递给“计算和打印”功能。

用户输入330,您需要找到正确的索引,截至目前course[2]并传递该索引。