数组结果有问题 [英] Problem with the array results

查看:84
本文介绍了数组结果有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使学生平均水平和班级平均水平发挥作用,但我无法根据自己掌握的信息来考虑如何做到这一点
我是否需要另一个数组来处理这个问题,如果是这样,当我知道对于学生平均水平,我需要取他们的总成绩,然后除以分数,然后对平均水平进行除以除以班级平均水平,我将如何措辞呢? numstudents.但是我无法弄清楚如何从数组中获取信息,但是我一无所获,但有类似char或not defined之类的错误,还有另一种方法可以使这一点更容易理解吗?谢谢

I am trying to get the student average and the class average to function and I cant think of how to do this based on the information that I have
Do I need another array to process this and if so how would I word it to work when I know that for the student average I need to take their total curgrades then divide by the numgrades.and for class average total of avggrade and divide it by numstudents. But I cant figure out how to get the information out of the array when I try i get nothing but errors like char or not defined is there another way to make this easier to figure out? thanks

int numgrade;
int numstudent;
const int max_students = 499;
const int max_grade = 499;
string student [max_students];
int grades [max_students][max_grade] = {0,0};

for (int curstudent =0; curstudent < numstudent; curstudent++){	
	cout << "Enter the students name: ";
	cin >> student[curstudent];
    for (int curgrade =0; curgrade < numgrade; curgrade++){
		cout << "Enter the grade: ";
		cin >> grades[curstudent][curgrade];
	}
}
for (int curstudent = 0; curstudent < numstudent; curstudent++){  
	cout << student [curstudent] << endl;
	for (int curgrade = 0; curgrade < numgrade; curgrade++){
		cout << grades[curstudent][curgrade] << endl;
	}
}

            
system("pause");
return 0;
}

推荐答案

您有两个循环,例如:
You have two loops like:
for (int curstudent =0; curstudent < numstudent; curstudent++)


但是,由于numstudent为零,所以两者都不做任何事情.我看到了与numgrade类似的问题.

最好先将所有数据读入数组,然后对数组进行迭代以计算平均值.


However, since numstudent is zero neither will do anything. I see a similar issue with numgrade.

It would probably be better to read all the data into your arrays first and then iterate the arrays to calculate your averages.


在您的第一个循环中,终止测试不能基于numstudent,因为在您读完所有学生之前,该值是不确定的.您需要重复此循环,直到用户用表明不再有学生的值响应为止.读取所有成绩的内循环也遵循相同的规则.但是,在读取这些值时,您应该增加学生人数,以便知道在程序的第二部分中要处理多少个条目.您还可以使用一些特殊值(如果为字符串,则为NULL;如果为数字,则为-1)来指示信息的结尾.在第二个循环中,您处理每个学生数组,然后将成绩加在一起,最后一个值相加后,就可以将总数除以成绩数,得出学生的平均值.在处理这些课程时,您还应该将它们添加到班级总数中,最后将其除以班级中的总成绩,以得出您的班级平均数.尝试先在纸上写下各个步骤,以了解在流程的每个步骤中需要保持哪些价值以及需要使用哪些计数器.然后,将您的设计转换为代码是一件简单的事情.
In your first loop your termination test cannot be based on numstudent as this value is indeterminate until you have read in all the students. You need to repeat this loop until the user responds with some value that indicates no more students. The same rule holds for the inner loop that reads all the grades. However while reading these values you should be incrementing the count of students so you know how many entries to process in the second part of the program. You could also use some special value (NULL if a string, or -1 if numeric) to indicate the end of the information. In your second loop you process each student array and add the grades together as you go and after adding the last value you can divide the total by the number of grades to get the student average. While processing these you should also be adding them to the class total and at the very end divide by the total number of grades in the class to get your class average. Try writing the individual steps on paper first so you understand what values you need to keep at each step in the process and what counters you need. Then it is a simple matter to transform your design into code.


语录:如何从中得出学生平均和班级平均的结果?"

请重新阅读我以前的文章,我不能说得更清楚.

伪代码
Quote: " how will I get the results of student average and class average out of it?"

Please re-read my previous post, I cannot make it much clearer than this.

Pseudo code
classaverage = 0
classcount = 0
for each student
{
    studentaverage = 0
    gradecount = 0
    for each grade
    {
        studentaverage = studentaverage + grade
        gradecount = gradecount + 1
    }
    classaverage = classaverage + studentaverage
    classcount = classcount + gradecount

    studentaverage = studentaverage / gradecount
    print studentname, studentaverage
}
classaverage = classaverage / classcount
print "Class average", classaverage


这篇关于数组结果有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆