//编写一个程序,最多将10个donation值读入到一个double数组中。

#include
int main()
{
using namespace std;
int donation;
double average;
int sum;
int c,i;
for(i=1;i<=10;i++)
{
while(isdigit(donation))
cout<<"Enter "< cin>>donation[i];
sum+=donation[i];
average=sum/i;
}
while(donation[i]>average)
c++;
cout<<"平均值="<<average;
cout<<"一共有"<<c<<"个数字大于平均值";
return 0;
}
[Error] D:\My Documents\C-Free\Temp\未命名1.cpp:14: error: invalid types int[int]' for array subscript
[Error] D:\My Documents\C-Free\Temp\未命名1.cpp:17: error: invalid types
int[int]' for array subscript
请问各位大大这个错误怎么解决啊!!!好纠结!!!

6个回答

#include<iostream>
int main()
{
    using namespace std;
    int donation[10];
    double average;
    int sum=0;
    int c=0,i;
    for(i=1;i<=10;i++)
    {
        while(isdigit(donation[i]))
        cout<<"Enter "<<i<<" number:\n";
        cin>>donation[i];
        sum+=donation[i];
        average=sum/i;
    }
    while(donation[i]>average)
    c++;
    cout<<"平均值="<<average;
    cout<<"一共有"<<c<<"个数字大于平均值"; 
    return 0;
} 
qq_29650553
Qiuuuuu 错的....
5 年多之前 回复

你写的是:
int donation;
//
.....
//
cout<<"Enter "< cin>>donation[i];

很明显donation应该定义成数组,或者把donation[i]改成donation。

谢谢各位大大!!!!

#include
using namespace std;

int main()
{
double donation=0.0,dona[10],sum=0.0,avg=0.0;
int i=0,n=0,j=0;
cout<<"Enter a number for donation,you may enter ten times:"< cout while((i>donation))
{
dona[i]=donation;
sum+=dona[i];
i++;
j++;
if(i<10)
cout<<"#"<<i+1<<": ";
}

avg=sum/j;
cout<<"the average is: "<<avg<<endl;
cout<<"there have bigger than average: ";
for(i=0,n=0;i<10;i++)
{
    if(dona[i]>avg)
    {
        cout<<dona[i]<<"\t";
        n++;
    }

}
cout<<endl;
cout<<"there have "<<n<<" number bigger than average."<<endl;;
return 0;

}

题目是要一个double的数组吧。。。我这样写的 但是我感觉如果输入错误,就不应该出现平均值和大与平均值的数,想知道怎么改?

#include
using namespace std;
int main(){
int i,c=0;
double a,sum=0,average,donation[10];
for (i=0;i<10;i++){

    if (!(cin>>donation[i]))
    break;
    sum+=donation[i];
}
a=i; //避免sum/i被强制转换为int
average=sum/a;
for (int j=0;j<i;j++)
    if (donation[j]>average)
        c++;
cout<<"average= "<<average<<endl<<"the number of above-average "<<c<<endl;
return 0; 

}

int donation[100];

定义数组

立即提问
相关内容推荐