求助一道C语言编程题,题目如下
用数组编写,不要用指针。 展开
#include<stdio.h>
int main(void)
{
int scores[5][3] = { { 60, 50, 30 },
{ 90, 50, 80 },
{ 100, 50, 60 },
{ 80, 90, 80 },
{ 90, 80, 60 }};
float average[5] = { 0.0 };//保存各个学生的平均分
int sum[5] = { 0 };//保存各个学生的总分
float average_scores[3] = { 0.0 };//保存各科的平均分
int sum_scores[3] = { 0 };//保存各科的总计分
int i, j;
int n;
float m;
for (i = 0; i < 5; i++)
{
sum[i] = scores[i][0] + scores[i][1] + scores[i][2];
average[i] = sum[i] / 3.0;
if (i <= 2)
{
sum_scores[i] = 0;
}
for (j = 0; j < 5; j++)
{
if (i >= 3)
{
break;
}
sum_scores[i] += scores[j][i];
}
if (i <= 2)
{
average_scores[i] = sum_scores[i] / 5.0;
}
}
printf("\t--------------------------------------------\n");
printf("\t编号\t语文\t数学\t英语\t平均分\t总分\n");
for (i = 0; i < 5; i++)
{
printf("\t%3d ", i + 1);
for (j = 0; j < 3; j++)
{
printf("\t%d ", scores[i][j]);
}
printf("\t%.2f ", average[i]);
printf("\t%d \n", sum[i]);
}
printf("\t平均分");
for (i = 0; i < 3; i++)
{
printf("\t%.2f ", average_scores[i]);
}
m = (average_scores[0] + average_scores[1] + average_scores[2]) / 3.0;
printf("\t%.2f\t%.0f\n", m, m*3);
printf("\t总计分");
for (i = 0; i < 3; i++)
{
printf("\t%d ", sum_scores[i]);
}
n = sum_scores[0] + sum_scores[1] + sum_scores[2];
printf("\t%.2f\t%d\n", n/3.0, n);
return 0;
}
如果是要聪键盘输入成绩该做呢,我只知道是需要加一个循环,可是不知道怎么写代码
/* int scores[5][3] = { { 60, 50, 30 },
{ 90, 50, 80 },
{ 100, 50, 60 },
{ 80, 90, 80 },
{ 90, 80, 60 }}; 这段代码是直接初始化的*/
下面的是从键盘读入:
在所有变量定义之后编写就可以了
for(i=0;i<N;i++)
for(j=0;j<M;j++)
{
scanf("%d",&scores[i][j]);
}
/* N,M分别代表二维数组的下标 ,如果 scores[i][j]=scores[5][3],那么N,M分别是5,3 */