我目前在学习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[])
{
}
无关的答案的学生人数, *(scorePTR + count)与scorePTR [count] – vmpstr 2012-02-09 20:42:49
相同我*请*重申这是'[c]'? – Xeo 2012-02-09 20:44:55
@Xeo它正在使用流:) – vmpstr 2012-02-09 20:45:44