注册 登录
编程论坛 C++教室

求助看一下这道题我哪里错了

zr1234561555 发布于 2009-07-17 21:01, 583 次点击
帮忙看下哪里错了,求学生的总成绩,平均,最大和最小
#include <iostream.h>
#include <math.h>
void main()
{
    int n,i,max,min,sum=0;
    int a[];
    float ave;
    cout<<"学生总人数";
    cin>>n;
    for (i=0;i<n;i++)
    {
     cout<<"输入学生分数";
     cin>>a[i];
     sum+=a[i];
    }
    min=max=a[0];
    for (i=1;i<n;i++)
    {
        if (a[i]<max)
        min=a[i];
        else max=a[i];
    }
    ave=float(sum/n);
    cout<<"学生的总成绩:"<<sum<<endl;
    cout<<"学生的平均成绩:"<<ave<<endl;
    cout<<"学生最低分:"<<min<<endl;
    cout<<"学生最高分:"<<max<<endl;
}
8 回复
#2
jackie19182009-07-17 22:08
int a[];对数组进行定义必须要指定个数的,这里有2种办法进行修改:一种办法是自己开辟空间,可以使用堆内存,不过这里没必要。另外就是直接定义个指针就完了用int *a;就OK
#3
jackie19182009-07-17 22:15
用堆内存可以这样~~
#include <iostream.h>
#include <math.h>
void main()
{
    int n,i,max,min,sum=0;
    int *a;//指针
    float ave;
    cout<<"学生总人数";
    cin>>n;
    a=new int[n];//堆内存
    for (i=0;i<n;i++)
    {
     cout<<"输入学生分数";
     cin>>a[i];
     sum+=a[i];
    }
    min=max=a[0];
    for (i=1;i<n;i++)
    {
        if (a[i]<max)
        min=a[i];
        else max=a[i];
    }
    ave=float(sum/n);
    cout<<"学生的总成绩:"<<sum<<endl;
    cout<<"学生的平均成绩:"<<ave<<endl;
    cout<<"学生最低分:"<<min<<endl;
    cout<<"学生最高分:"<<max<<endl;
    delete [] a;//释放堆内存
}
#4
fjwddzzc1232009-07-17 22:30
int a[]={1,2}  这样可以不用写数组个数  没初始化不能写成这样  int a[] 不知道包含几个元素
#5
jackie19182009-07-17 23:07
int a[]={1,2}是不错,但相当于int a[2]={1,2}.就把数组个数给定下来了~~对于程序就没什么用处了
#6
mfkblue2009-07-17 23:35
初期的题难度不大,我一般直接定义a[1000],反正用不完。
#7
zr12345615552009-07-20 13:21
我已经弄懂了,感谢啊,最近自学c++,幼稚的问题比较多哈
#8
zhqer2009-07-20 17:21
楼主还是用vector吧,标准cpp不是提倡用stl吗。
#9
sydyh432009-07-22 15:04
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int   n,i,max,min;
    int   sum = 0;
    int   *a;
    float ave;
   
    a = new int;
   
    cout << "input the number of students:";
    cin  >> n;
   
    cout << "input the score of all of students:";
    for(i = 0; i < n; ++i)
    {
          cin >> *(a + i);
          sum += *(a + i);
    }
   
    max = min = a[0];
    for(i = 1; i < n; i++)
    {
          if(max < a[i])
          {
                 max = a[i];
          }
         
          if(min > a[i])
          {
                 min = a[i];
          }
    }
   
    ave = (float) sum / n;
   
    cout << "the number of the students is:" << n << endl;
    cout << "the sum of the students is:" << sum << endl;
    cout << "the average of the students is:" << ave << endl;
    cout << "the maxium of the score is:" << max << endl;
    cout << "the minium of the score is:" << min << endl;
   
    delete a;
   
    system("pause");
   
}
1