【问题标题】:Error when using array as array element使用数组作为数组元素时出错
【发布时间】:2012-02-10 01:55:58
【问题描述】:

我正在课堂上学习指针和数组。我的麻烦在于 dupScore 函数。 我试图找出分数相同的学生人数。 scoreArrays 元素编号标识学生并保存分数。

我想创建一个新数组以使用学生分数作为数组的索引。我在 scoreArrays 下得到了一个红色的波浪线。错误是“表达式必须是整数或枚举”

for(int count = 0; count < maxStudents; count++)
        comparisonArray[scoreArray[count]]++;

这是我的全部代码。

#include <iostream>
using namespace std;

const int maxStudents = 30;
void readScores(double[]);
void gradeCounter(double[],int&,int&,int&,int&,int&);
int dupScore(double[]);
void readoutFunc(double[], int, int, int, int, int, int);

int main()
{
    int As = 0, Bs = 0, Cs = 0, Ds = 0, Fs = 0; // Distribution of scores
    int sameScore = 0;

    double scoreArray[maxStudents];
    readScores(scoreArray);//Read in Scores
    gradeCounter(scoreArray, As, Bs, Cs, Ds, Fs);//Count letter grades
    sameScore = dupScore(scoreArray);//Detect duplicate scores

    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;
    }
}

int dupScore(double scoreArray[])
{
    const int maxGrade = 101;
    double comparisonArray[maxGrade];
    int sameScores = 0;


    for(int count = 0; count < maxStudents; count++)
        comparisonArray[scoreArray[count]]++;

    for(int count2 = 0; count2 < maxGrade; count2++)
    {
        if(comparisonArray[count2] > 0)
            sameScores+=1;
    }


        return sameScores;

}

void readoutFunc(double scoreArray[], int As, int Bs, int Cs, int Ds, int Fs, int sameScore)
{
        int numofStudents = 0;
    for(int count = 0; scoreArray[count] >= 0; count++)
    { 
        numofStudents += 1;
    }
    cout<<"\n\nReport";
    cout<<"\n---------";
    cout<<"\nNumber of students: "<<numofStudents;
    cout<<"\nNumber of As: "<<As;
    cout<<"\nNumber of Bs: "<<Bs;
    cout<<"\nNumber of Cs: "<<Cs;
    cout<<"\nNumber of Ds: "<<Ds;
    cout<<"\nNumber of Fs: "<<Fs;

    cout<<"\n\n"<<sameScore<<" students have the same score."
}

【问题讨论】:

    标签: c++ arrays function pointers loops


    【解决方案1】:

    scoreArray 被定义为double[] 所以scoreArray[count] 会给你一个double。您需要一个整数才能引用 compareArray 中的元素。

    for(int count = 0; count < maxStudents; count++) comparisonArray[(int)scoreArray[count]]++;

    这将解决您当前面临的问题,但也会损失很多精度。如果成绩总是整数,您可能会考虑将double[] 更改为int[]。否则,您可能需要查看匹配 scoreArray 元素的不同方法。

    【讨论】:

    • 我认为在这种情况下切换到 int 会起作用:]谢谢,现在我不必熬夜了。
    • 虽然当我更改它并尝试编译所有内容时,我在 compareArray[(int)scoreArray[count]]++;
    • 编译时遇到访问冲突?或者你什么时候运行的?
    • 任何分数 100 将导致访问分配的 comparisonArray 之外。您可以首先检查 scoreArray 中的元素,以确保所有内容都 >= 0 &&
    【解决方案2】:

    scoreArraydouble 的数组,您不能使用double 来索引数组(当您使用comparisonArray[scoreArray[count]] 时,您正在尝试这样做。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-28
      • 2018-01-11
      • 2022-06-15
      • 2011-05-12
      • 2016-09-05
      • 1970-01-01
      • 2014-09-21
      相关资源
      最近更新 更多