我目前在學習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