| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 583 人关注过本帖
标题:求助看一下这道题我哪里错了
只看楼主 加入收藏
zr1234561555
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-7-17
结帖率:100%
收藏
已结贴  问题点数:50 回复次数:8 
求助看一下这道题我哪里错了
帮忙看下哪里错了,求学生的总成绩,平均,最大和最小
#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;
}
搜索更多相关主题的帖子: 跪求答案 大哥大姐来看下啦 
2009-07-17 21:01
jackie1918
Rank: 2
等 级:论坛游民
帖 子:15
专家分:72
注 册:2009-7-14
收藏
得分:10 
int a[];对数组进行定义必须要指定个数的,这里有2种办法进行修改:一种办法是自己开辟空间,可以使用堆内存,不过这里没必要。另外就是直接定义个指针就完了用int *a;就OK
2009-07-17 22:08
jackie1918
Rank: 2
等 级:论坛游民
帖 子:15
专家分:72
注 册:2009-7-14
收藏
得分:20 
用堆内存可以这样~~
#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;//释放堆内存
}
2009-07-17 22:15
fjwddzzc123
Rank: 2
等 级:论坛游民
帖 子:56
专家分:79
注 册:2009-5-7
收藏
得分:5 
int a[]={1,2}  这样可以不用写数组个数  没初始化不能写成这样  int a[] 不知道包含几个元素
2009-07-17 22:30
jackie1918
Rank: 2
等 级:论坛游民
帖 子:15
专家分:72
注 册:2009-7-14
收藏
得分:5 
int a[]={1,2}是不错,但相当于int a[2]={1,2}.就把数组个数给定下来了~~对于程序就没什么用处了
2009-07-17 23:07
mfkblue
Rank: 5Rank: 5
等 级:职业侠客
帖 子:472
专家分:343
注 册:2008-12-21
收藏
得分:10 
初期的题难度不大,我一般直接定义a[1000],反正用不完。
2009-07-17 23:35
zr1234561555
Rank: 1
来 自:湖北
等 级:新手上路
帖 子:9
专家分:0
注 册:2009-7-17
收藏
得分:0 
我已经弄懂了,感谢啊,最近自学c++,幼稚的问题比较多哈
2009-07-20 13:21
zhqer
Rank: 2
等 级:论坛游民
帖 子:12
专家分:18
注 册:2008-8-21
收藏
得分:0 
楼主还是用vector吧,标准cpp不是提倡用stl吗。
2009-07-20 17:21
sydyh43
Rank: 1
等 级:新手上路
帖 子:10
专家分:9
注 册:2009-6-17
收藏
得分:0 
#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");
   
}
2009-07-22 15:04
快速回复:求助看一下这道题我哪里错了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018350 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved