我可以使用什么算法来计算有多少学生具有相同的分数?
我目前正在 C++ 课程中学习指针。下面的代码有点令人困惑,但我最终明白了,我当前的问题是我的逻辑。
有人告诉我,我可以找到具有相同分数的学生数量,而无需排序或搜索并使用单个索引,但我一生都无法弄清楚。
我将分数存储在 ScoresArray 中,元素编号标识了它属于哪个学生。
#include <iostream>
using namespace std;
const int maxStudents = 30;
void readScores(double[]);
void gradeCounter(double[],int&,int&,int&,int&,int&);
void sameScore(double[]);
int main()
{
int As = 0, Bs = 0, Cs = 0, Ds = 0, Fs = 0; // Distribution of scores
double scoreArray[maxStudents];
readScores(scoreArray);
gradeCounter(scoreArray, As, Bs, Cs, Ds, Fs);
system ("PAUSE");
return 0;
}
void readScores(double scoreArray[])
{
double *scorePTR;
scorePTR = scoreArray;
for(int count = 0; count < maxStudents; count++)
{
cout<<"Please enter score for student "<<count+1<<" or -999 to end.\n";
cin>>*(scorePTR+count);
if(*(scorePTR+count) == -999)
break;
}
}
void gradeCounter(double scoreArray[],int &As,int &Bs,int &Cs,int &Ds,int &Fs)
{
double *scorePTR2;
scorePTR2 = scoreArray;
for(int count = 0; count < maxStudents; count++)
{
if(scoreArray[count] >= 90)
As+=1;
else if(*(scorePTR2+count) >= 80 && *(scorePTR2+count) < 90)
Bs+=1;
else if(*(scorePTR2+count) >= 70 && *(scorePTR2+count) < 80)
Cs+=1;
else if(*(scorePTR2+count) >= 60 && *(scorePTR2+count) < 70)
Ds+=1;
else if(*(scorePTR2+count) >= 0 && *(scorePTR2+count) < 60)
Fs+=1;
}
}
void sameScore(double scoreArray[])
{
}
I am currently learning about pointers in my C++ class. The following code was slightly confusing but I eventually got it, my current problem is my logic.
I'm told I can find the number of students that have the same score without sorting or searching and using a single index but I can't for the life of me figure it out.
I have scores stored in scoresArray and the element number identifys which student it belongs to.
#include <iostream>
using namespace std;
const int maxStudents = 30;
void readScores(double[]);
void gradeCounter(double[],int&,int&,int&,int&,int&);
void sameScore(double[]);
int main()
{
int As = 0, Bs = 0, Cs = 0, Ds = 0, Fs = 0; // Distribution of scores
double scoreArray[maxStudents];
readScores(scoreArray);
gradeCounter(scoreArray, As, Bs, Cs, Ds, Fs);
system ("PAUSE");
return 0;
}
void readScores(double scoreArray[])
{
double *scorePTR;
scorePTR = scoreArray;
for(int count = 0; count < maxStudents; count++)
{
cout<<"Please enter score for student "<<count+1<<" or -999 to end.\n";
cin>>*(scorePTR+count);
if(*(scorePTR+count) == -999)
break;
}
}
void gradeCounter(double scoreArray[],int &As,int &Bs,int &Cs,int &Ds,int &Fs)
{
double *scorePTR2;
scorePTR2 = scoreArray;
for(int count = 0; count < maxStudents; count++)
{
if(scoreArray[count] >= 90)
As+=1;
else if(*(scorePTR2+count) >= 80 && *(scorePTR2+count) < 90)
Bs+=1;
else if(*(scorePTR2+count) >= 70 && *(scorePTR2+count) < 80)
Cs+=1;
else if(*(scorePTR2+count) >= 60 && *(scorePTR2+count) < 70)
Ds+=1;
else if(*(scorePTR2+count) >= 0 && *(scorePTR2+count) < 60)
Fs+=1;
}
}
void sameScore(double scoreArray[])
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建第二个包含 101 个元素(从 0 到 100)的数组,将其全部初始化为 0,并使用当前学生的分数作为该数组的索引。
因此,如果当前学生的分数为 87,那么您将递增this_new_array[87] 除以 1。
最后,索引 X 处的数组将包含得分 X 的学生数量。
You can create a second array of 101 elements (from 0 to 100), initialize it all to 0, and use the current student's score as the index into this array
So, if the current student has a score of 87, then you would increment this_new_array[87] by 1.
In the end, the array at index X will contain the number of students that have score X.