有哪位高手帮我解决一下两个C++问题,万分感谢!

lsthz 2009-12-08 09:57:02
1.已知一个二维数组中存放一个班30人三门课学生的成绩,求每个人的平均成绩,每门课程的平均成绩。(用指针实现)
程序:
#include"iostream.h"
void pcave(double *t);
void psave(double *t);
void main()
{
double s[30][3]={{88,71,90},{94,93,79},{81,75,67},{72,69,83},{81,78,77},{89,69,71},{85,89,83},{78,67,69},{90,98,89},{83,77,79},{66,80,65},{77,79,85},{92,95,99},{99,75,78},{98,96,85},{87,68,69},{63,89,78},{95,94,91},{95,96,92},{93,98,82},{86,97,83},{84,78,72},{86,84,72},{76,96,81},{95,86,72},{73,86,87},{87,84,82},{91,68,69},{96,67,82},{83,84,99}};
double *p;
p=*s;
cout<<"全体学生三门课的成绩"<<endl;
for(int i=0;i<90;i++)
{ cout<<*p++<<" ";
if((i+1)%3==0) cout<<endl;
}
p=*s;
cout<<"每门课的平均成绩为:"<<endl;
pcave(p);
p=*s;
cout<<"每个学生的平均成绩为:"<<endl;
psave(p);
}
void pcave(double *t)
{
double sum[3];
for(int i=0;i<3;i++)
{ for(int j=0;j<30;j+=3)
sum[i]+=*t;
t+=3;
}
for(i=0;i<3;i++)
{ cout<<sum[i]/30;
cout<<endl;
}
}
void psave(double *t)
{
double sum[30];
for(int i=0;i<30;i++)
{
for(int j=0;j<3;j+=3)
sum[i]+=*t;
t++;
}
for(i=0;i<30;i++)
{ cout<<sum[i]/3;
cout<<endl;
}
}
2.六个学生的资料,要求先算出每个学生的平均成绩,再按平均成绩从大到小排列,依次输出。
程序:
#include "iostream.h"

struct student
{
int num;
char name[10];
double score_chinese;
double score_english;
double score_math;
}stu[6];

void ave(struct student stu[6]);
void arrange(struct student stu[6],double scroe[6]);

void main()
{
stu[6]={{001,"zhang",88,95,83},{002,"wang",98,95,77},{003,"li",76,84,97},{004,"zhao",85,95,87},{005,"sun",98,88,86},{006,"qian",74,82,90}};
cout<<"学生数据如下:"<<endl;
for(int i=0;i<6 ;i++)
cout<<stu[i].num<<stu[i].name<<stu[i]. score_chinese <<stu[i]. score_english <<su[i]t. score_math <<endl;
ave(struct student stu[6]);
arrange(struct student stu[6],double scroe[6]);
cout<<"学生成绩排序如下:"<<endl;
for(i=0;i<6;i++)
cout<<stu[i].num<<stu[i].name<<stu[i]. score_chinese <<stu[i]. score_english <<su[i]t. score_math <<endl;
}

void ave(struct student stu[6])
{
double ave[6] ;
for(int i=0;i<6;i++)
ave[i]=(stu[i]. score_chinese+ stu[i]. score_english+ su[i]t. score_math)/3;
cout<< "每个学生的平均成绩是:"<<endl;
for(i=0 ;i<6;i++)
cout<<stu[i].num<<stu[i].name<<ave[i]<<endl;
}

void arrange(struct student stu[6],double scroe[6])
{
struct student temp,*p ;
for(int i=0;i<5;i++)
for(int j=1;j>i;j++)
{if(score[i]<score[j])
{temp=stu[i]; stu[i]=stu[j]; stu[j]=temp;
}
}
}


第一道题检查没错,但是输出结果不对;第二道题检查有许多标点符号的错误,就是不知道怎么改。请各位高手不吝赐教,谢谢!!!
...全文
145 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
double s[30][3]={{88,71,90},{94,93,79},{81,75,67},{72,69,83},{81,78,77},{89,69,71},{85,89,83},{78,67,69},{90,98,89},{83,77,79},{66,80,65},{77,79,85},{92,95,99},{99,75,78},{98,96,85},{87,68,69},{63,89,78},{95,94,91},{95,96,92},{93,98,82},{86,97,83},{84,78,72},{86,84,72},{76,96,81},{95,86,72},{73,86,87},{87,84,82},{91,68,69},{96,67,82},{83,84,99}};
double *p;
p=*s;
cout < <"全体学生三门课的成绩" < <endl;
for(int i=0;i <90;i++)
{ cout < <*p++ < <" ";
if((i+1)%3==0) cout < <endl;
}
========================================================================
p=*s;
P应该指向的是第一行,p所指的是一行数据,如果你想引用该行的其它数据,可以用P[0],P[1],P[2]
当P++的时候,p就指向了下一行,所以看你的意思应该这样写:

for(int i=0;i <30;i++)
{
for(int j = 0; j < 3; j++)
cout < <p[j]< <" ";

p++;
cout < <endl;
}
下面的代码没看了,我这边没环境,好像应该是这样的吧,试一下。
祝福!
ffq614 2009-12-09
  • 打赏
  • 举报
回复
#include "Stdafx.h"
#include"iostream.h"
struct student
{
int num;
char name[10];
double score_chinese;
double score_english;
double score_math;
double score;
};
void ave(struct student *p);
void arrange(struct student *p);

void main()
{
struct student stu[6]={{001,"zhang",88,95,83},{002,"wang",98,95,77},{003,"li",76,84,97},{004,"zhao",85,95,87},{005,"sun",98,88,86},{006,"qian",74,82,90}};
cout <<"学生数据如下:" <<endl;
for(int i=0;i<6 ;i++)
{
cout<<stu[i].num<<stu[i].name<<stu[i].score_chinese<<stu[i].score_english<<stu[i].score_math<<endl;
stu[i].score=(stu[i].score_chinese+stu[i].score_english+stu[i].score_math)/3;
}
struct student *p;
p=stu;
ave(p);
arrange(p);
}


void ave(struct student *p)
{
cout << "每个学生的平均成绩是:" <<endl;
for(int i=0;i<6 ;i++)
{
p->score=(p->score_chinese+p->score_english+p->score_math)/3;
cout<<p->name<<" "<<p->score<<endl;
p++;
}
p=p-6;
}

void arrange(struct student *p)
{
cout <<"学生成绩排序如下:" <<endl;
struct student temp;
for(int i=0;i<6 ;i++)
for(int j=0;j<5-i;j++)
{
if ((p+j)->score<(p+j+1)->score)
{
temp=*(p+j+1);
*(p+1+j)=*(p+j);
*(p+j)=temp;
}
}
for(i=0;i<6 ;i++)
{
cout<<p->name<<" "<<p->score<<endl;
p++;
}
p=p-6;
}
ffq614 2009-12-09
  • 打赏
  • 举报
回复
#include "Stdafx.h"
#include"iostream.h"
struct student
{
int num;
char name[10];
double score_chinese;
double score_english;
double score_math;
double score;
};
void main()
{
struct student stu[]={{001,"zhang",88,95,83},{002,"wang",98,95,77},{003,"li",76,84,97},{004,"zhao",85,95,87},{005,"sun",98,88,86},{006,"qian",74,82,90}};
cout <<"学生数据如下:" <<endl;
for(int i=0;i<6 ;i++)
{
cout<<stu[i].num<<stu[i].name<<stu[i].score_chinese<<stu[i].score_english<<stu[i].score_math<<endl;
stu[i].score=(stu[i].score_chinese+stu[i].score_english+stu[i].score_math)/3;
}
struct student temp;
for(i=0;i<6 ;i++)
for(int j=0;j<5-i;j++)
{
if (stu[j].score<stu[j+1].score)
{
temp=stu[j+1];
stu[j+1]=stu[j];
stu[j]=temp;
}
}
for(i=0;i<6;i++)
cout<<stu[i].num<<stu[i].name<<stu[i].score_chinese<<stu[i].score_english<<stu[i].score_math<<" "<<stu[i].score<<endl;
}
o_yale_o 2009-12-09
  • 打赏
  • 举报
回复
又是作业
hzy694358 2009-12-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 akirya 的回复:]
作业还是自己完成的好
[/Quote]
easy,自己搞定
fffanpei 2009-12-08
  • 打赏
  • 举报
回复
作业自己写才可以提高吧
自己慢慢调试吧
zottff 2009-12-08
  • 打赏
  • 举报
回复
第2题:(晕死,问题太多,输出格式自己调吧)
#include"iostream.h"
struct student
{
int num;
char name[10];
double score_chinese;
double score_english;
double score_math;
};

void ave(struct student stu[], double score[]);
void arrange(struct student stu[],double score[]);

void main()
{
double score[6];
struct student stu[]={{001,"zhang",88,95,83},{002,"wang",98,95,77},{003,"li",76,84,97},{004,"zhao",85,95,87},{005,"sun",98,88,86},{006,"qian",74,82,90}};
cout <<"学生数据如下:" <<endl;
for(int i=0;i <6 ;i++)
cout <<stu[i].num<<"," <<stu[i].name<<"," <<stu[i].score_chinese<<"," <<stu[i].score_english<<"," <<stu[i].score_math<<"," <<endl;
ave(stu, score);
arrange(stu,score);
cout <<"学生成绩排序如下:" <<endl;
for(i=0;i <6;i++)
cout <<stu[i].num <<stu[i].name <<stu[i]. score_chinese <<stu[i]. score_english <<stu[i]. score_math <<endl;
}

void ave(struct student stu[6], double score[])
{

for(int i=0;i <6;i++)
score[i]=(stu[i]. score_chinese+ stu[i]. score_english+ stu[i]. score_math)/3;
cout << "每个学生的平均成绩是:" <<endl;
for(i=0 ;i <6;i++)
cout <<stu[i].num <<stu[i].name <<score[i] <<endl;
}

void arrange(struct student stu[],double score[])
{
struct student temp ;
double tmp;
for(int i=0;i <5;i++)
for(int j=i+1;j<6;j++)
{if(score[i] < score[j])
{temp=stu[i]; stu[i]=stu[j]; stu[j]=temp;
tmp = score[i];score[i] = score[j];score[j]=tmp;
}
}
}
zottff 2009-12-08
  • 打赏
  • 举报
回复
第一题修改如下:
void pcave(double *t)
{
double sum[3];

for(int i=0;i <3;i++)
{
sum[i] = 0;
for(int j=0;j <30; j++)
sum[i]+=*(t+i+j*3);
}

for(i=0;i <3;i++)
{ cout <<sum[i]/30;
cout <<endl;
}
}
void psave(double *t)
{
double sum[30];
for(int i=0;i <30;i++)
{
sum[i] = 0;
for(int j=0;j <3;j++)
sum[i]+=*t++;
}
for(i=0;i <30;i++)
{ cout <<sum[i]/3;
cout <<endl;
}
}
qiaozhiyuan 2009-12-08
  • 打赏
  • 举报
回复
第一题sum[3]数组需要初始化。第二题函数内的参数struct 需要去掉,arrange(struct student stu[6],double scroe[6]) 修改为arrange(student stu[],double scroe[]) 。

  • 打赏
  • 举报
回复
作业还是自己完成的好
ziplj 2009-12-08
  • 打赏
  • 举报
回复
ffq614 2009-12-08
  • 打赏
  • 举报
回复
void pcave(double *t)
{
double sum[3];
for(int j=0;j<3;j++)
{
sum[j]=0;
for(int i=0;i<30;i++)
sum[j]+=*(t+3*i+j);
cout<<sum[j]/30<<endl;
}

}
void psave(double *t)
{
double sum[30];
for(int i=0;i <30;i++)
{
sum[i]=0;
for(int j=0;j<3;j++)
sum[i]+=*t++;
cout <<sum[i]/3<<endl;
}
}

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧