2012-02-09 36 views
0

我目前在学习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[]) 
{ 

} 
+0

无关的答案的学生人数, *(scorePTR + count)与scorePTR [count] – vmpstr 2012-02-09 20:42:49

+2

相同我*请*重申这是'[c]'? – Xeo 2012-02-09 20:44:55

+0

@Xeo它正在使用流:) – vmpstr 2012-02-09 20:45:44

回答

4

您可以创建101元(从0到100)的第二阵列,这一切都初始化为0,并使用当前学生的分数作为这一数组

因此,如果当前学生有87分,那么你会增加this_new_array [87]通过1

最后,该数组中的索引X将包含有得分X.

+0

我怎样才能把得分87说成元素1,让编译器知道我想让它指向new_array [87]? – sircrisp 2012-02-09 20:59:05

+0

我的意思是说,我知道如何访问scoreArray,我会使用for循环来读取每个分数。我得不到的是,我如何获得分数,并让编译器知道我想增加与该分数相匹配的元素。 – sircrisp 2012-02-09 21:08:10

+0

this_new_array [scoreArray [count]] ++ – 2012-02-09 21:35:25

相关问题