1、
#include <stdio.h>
int main(void)
{
int i, j;
int a[6][2];
puts("please input the elements of the 6 * 2 matrix.");
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++ )
{
printf("a[%d][%d] = ", i, j ); scanf("%d", &a[i][j]);
}
}
int majorsum[2] = {0};
int majormean[2];
for (i = 0; i < 2; i++)
{
for (j = 0; j < 6; j++)
{
majorsum[i] += a[j][i];
}
majormean[i] = majorsum[i]/6;
}
for (i = 0; i < 2; i++)
{
printf("majorsum[%d] = %d\n", i, majorsum[i]);
printf("majormean[%d] = %d\n", i, majormean[i]);
}
puts("--------\n--------\n---------");
int studentsum[6];
int studentmean[6];
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++)
{
studentsum[i] += a[i][j];
}
studentmean[i] = studentsum[i]/2;
}
for (i = 0; i < 6; i++)
{
printf("studentsum[%d] = %d\n", i, studentsum[i]);
printf("studentmean[%d] = %d\n", i, studentmean[i]);
}
return 0;
}
↓
#include <stdio.h>
int main(void)
{
int a[6][2];
int i, j;
puts("please input the elements of the 6 * 2 matrix.");
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++)
{
printf("a[%d][%d] = ", i, j);
scanf("%d", &a[i][j]);
}
}
int majorsum[2] = {0};
double majormean[2];
for (i = 0; i < 2; i++)
{
for (j = 0; j < 6; j++)
{
majorsum[i] += a[j][i];
}
majormean[i] = majorsum[i]/6;
}
for (i = 0; i < 2; i++)
{
printf("majorsum[%d] = %d\n", i, majorsum[i]);
printf("majormean[%d] = %.1f\n", i, majormean[i]);
}
puts("--------\n--------\n--------");
int studentsum[6] = {};
double studentmean[6];
for (i = 0; i < 6; i++)
{
for (j = 0; j < 2; j++)
{
studentsum[i] += a[i][j];
}
studentmean[i] = studentsum[i]/2;
}
for (i = 0; i < 6; i++)
{
printf("studentsum[%d] = %d\n", i, studentsum[i]);
printf("studentmean[%d] = %.1f\n", i, studentmean[i]);
}
return 0;
}