鱼C论坛

 找回密码
 立即注册
查看: 1292|回复: 5

[已解决]求大佬帮忙写一个简单的程序

[复制链接]
发表于 2018-11-28 21:56:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
才学到数组,被这个难住了,  
最佳答案
2018-11-30 03:54:44
本帖最后由 TakeOver5 于 2018-11-30 04:28 编辑

上方已经解决这个问题了,不过有几个小 Bug 稍微除错一下

  1. #include "stdio.h"

  2. #define student_num 200                //最多可以输入200人

  3. int main()
  4. {
  5.         int student[student_num][3];
  6.         int i, student_now;
  7.         int Total_score[3] = {0};
  8.         int student_score[student_num] = {0};
  9.         float student_total_score[student_num] = {0}, average[3] = {0, 0, 0};

  10.         printf("请输入:");
  11.         scanf("%d", &student_now);

  12.         for(i = 0; i < student_now; i++)
  13.         {
  14.                 printf("请输入第%d的3门成绩:", i+1);
  15.                 scanf("%d, %d, %d", &student[i][0], &student[i][1], &student[i][2]);
  16.         }

  17.         for(i= 0; i< student_now; i++)
  18.         {
  19.                 Total_score[0] += student[i][0];
  20.                 Total_score[1] += student[i][1];
  21.                 Total_score[2] += student[i][2];

  22.                 student_score[i] += student[i][0] + student[i][1] + student[i][2];
  23.                 student_total_score[i] = (float)student_score[i] / 3;
  24.         }

  25.         average[0] = (float)Total_score[0]/student_now;
  26.         average[1] = (float)Total_score[1]/student_now;
  27.         average[2] = (float)Total_score[2]/student_now;

  28.         for(i= 0 ; i< student_now; i++)
  29.         {
  30.                 printf("第%d位同学的总分是:%d \r\n", i+1, student_score[i]);
  31.                 printf("第%d位同学的平均分是:%f \r\n", i+1, student_total_score[i]);
  32.         }
  33.         
  34.         printf("全班的总分是:%d, %d, %d \r\n", Total_score[0], Total_score[1], Total_score[2]);
  35.         printf("全班的平均分是:%f, %f, %f \r\n", average[0], average[1], average[2]);

  36.         return 0;

  37. }
复制代码


不过若想在 C 语言完成动态数组,那么可以使用 malloc() 函数实现

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main(void){
  4.    
  5.     int course = 3;         // 課程門數
  6.     int student_count;      // 學生人數
  7.     int i,j;
  8.    
  9.     printf("輸入學生人數:");
  10.     scanf("%d", &student_count);
  11.    
  12.     int **grade, *data;
  13.     int *student_total_score, *course_total_score;
  14.    
  15.     //int grade[student_count][course];
  16.     grade = (int**)malloc(student_count*sizeof(int*) + student_count*course*sizeof(int));
  17.     data = (int*)grade+student_count;
  18.     for(i=0; i<student_count; i++, data+=course)
  19.         grade[i] = data;
  20.    
  21.     //int student_total_score[student_count] = {0};
  22.     student_total_score = (int*)malloc(student_count*sizeof(int));
  23.     for(i=0; i<student_count; i++)
  24.         student_total_score[i] = 0;
  25.    
  26.     //int course_total_score[course] = {0};
  27.     course_total_score = (int*)malloc(course*sizeof(int));
  28.     for(i=0; i<course; i++)
  29.         course_total_score[i] = 0;
  30.    
  31.     for(i=0; i<student_count; i++){
  32.         printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
  33.         for(j=0; j<course; j++){
  34.             scanf("%d", &grade[i][j]);
  35.             /* 當前學生的總分 */
  36.             student_total_score[i] += grade[i][j];
  37.             
  38.             /* 每門課程的總分 */
  39.             course_total_score[j] += grade[i][j];
  40.         }
  41.     }
  42.    
  43.     printf("\n**********課程成績**********\n");
  44.    
  45.     printf("總分:");
  46.     for(i=0; i<course; i++)
  47.         printf("%d\t", course_total_score[i]);
  48.    
  49.     printf("\n");
  50.    
  51.     printf("平均:");
  52.     for(i=0; i<course; i++)
  53.         printf("%.1f\t", (float)course_total_score[i] / student_count);
  54.    
  55.     printf("\n**********個人成績**********\n");
  56.    
  57.     /* 打印所有學生的分數、總分和平均分 */
  58.     for(i=0; i<student_count; i++){
  59.         printf("學生編號 %02d:\t", i+1);
  60.         for(j=0; j<course; j++){
  61.             printf("%d\t", grade[i][j]);
  62.         }
  63.         printf("%d\t", student_total_score[i]);
  64.         printf("%.1f\t", (float)student_total_score[i] / course);
  65.         printf("\n");
  66.     }
  67.    
  68.     free(grade);
  69.     free(student_total_score);
  70.     free(course_total_score);
  71.    
  72.     return 0;
  73. }
复制代码
IMG_20181128_215310__01.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2018-11-29 06:21:30 | 显示全部楼层
这个问题可以通过二维数组解决

  1. #include <stdio.h>

  2. int main(void){
  3.    
  4.     int course = 3;         // 課程門數
  5.     int student_count;      // 學生人數
  6.     int i,j;
  7.    
  8.     printf("輸入學生人數:");
  9.     scanf("%d", &student_count);
  10.    
  11.     int grade[student_count][course];
  12.     int student_total_score[student_count] = {0};
  13.     int course_total_score[course] = {0};
  14.    
  15.     for(i=0; i<student_count; i++){
  16.         printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
  17.         for(j=0; j<course; j++){
  18.             scanf("%d", &grade[i][j]);
  19.             /* 當前學生的總分 */
  20.             student_total_score[i] += grade[i][j];
  21.             
  22.             /* 每門課程的總分 */
  23.             course_total_score[j] += grade[i][j];
  24.         }
  25.     }
  26.    
  27.     printf("\n**********課程成績**********\n");
  28.    
  29.     printf("總分:");
  30.     for(i=0; i<course; i++)
  31.         printf("%d\t", course_total_score[i]);
  32.    
  33.     printf("\n");
  34.    
  35.     printf("平均:");
  36.     for(i=0; i<course; i++)
  37.         printf("%.1f\t", float(course_total_score[i]) / student_count);
  38.    
  39.     printf("\n**********個人成績**********\n");
  40.    
  41.     /* 打印所有學生的分數、總分和平均分 */
  42.     for(i=0; i<student_count; i++){
  43.         printf("學生編號 %02d:\t", i+1);
  44.         for(j=0; j<course; j++){
  45.             printf("%d\t", grade[i][j]);
  46.         }
  47.         printf("%d\t", student_total_score[i]);
  48.         printf("%.1f\t", float(student_total_score[i]) / course);
  49.         printf("\n");
  50.     }
  51. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-11-29 09:19:01 | 显示全部楼层
TakeOver5 发表于 2018-11-29 06:21
这个问题可以通过二维数组解决

scanf("%d", &student_count);
   
    int grade[student_count][course];
    int student_total_score[student_count] = {0};
    int course_total_score[course] = {0};
这里是错的,不能动态定义数组
Screenshot_20181129-091553.jpg
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-29 16:35:08 | 显示全部楼层
saberAMD 发表于 2018-11-29 09:19
scanf("%d", &student_count);
   
    int grade[student_count][course];

抱歉我这里使用 C++ 编译,没注意到这个问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-29 19:13:21 | 显示全部楼层
#include "stdio.h"

#define student_num 200                //最多可以输入200人

int main()
{
        int student[student_num][3];
        int i, student_now;
        int        Total_score[3];
        int student_score[student_num] = {0};
        float student_total_score[student_num] = {0}, average[3] = {0, 0, 0};

        scanf("请输入:%d", &student_now);

        for(i = 0; i < student_now; i++)
        {
                printf("请输入第%d的3门成绩:\r\n", i+1);
                scanf("%d, %d, %d", &student[i][0], &student[i][1], &student[i][2]);
        }

        for(i= 0; i< student_now; i++)
        {
                Total_score[0] += student[i][0];
                Total_score[1] += student[i][1];
                Total_score[2] += student[i][2];

                student_score[i] += student[i][0] + student[i][1] + student[i][2];
                student_total_score[i] = (float)student_score[i] / 3;
        }

        average[0] = (float)Total_score[0]/student_now;
        average[1] = (float)Total_score[1]/student_now;
        average[2] = (float)Total_score[2]/student_now;

        for(i= 0 ; i< student_now; i++)
        {
                printf("第%d位同学的总分是:%d \r\n", i+1, student_score[i]);
                printf("第%d位同学的平均分是:%f \r\n", i+1, student_total_score[i]);
                printf("全班的总分是:%d, %d, %d \r\n", Total_score[0], Total_score[1], Total_score[2]);
                printf("全班的平均分是:%f, %f, %f \r\n", average[0], average[1], average[2]);
        }

        return 0;

}
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-11-30 03:54:44 | 显示全部楼层    本楼为最佳答案   
本帖最后由 TakeOver5 于 2018-11-30 04:28 编辑

上方已经解决这个问题了,不过有几个小 Bug 稍微除错一下

  1. #include "stdio.h"

  2. #define student_num 200                //最多可以输入200人

  3. int main()
  4. {
  5.         int student[student_num][3];
  6.         int i, student_now;
  7.         int Total_score[3] = {0};
  8.         int student_score[student_num] = {0};
  9.         float student_total_score[student_num] = {0}, average[3] = {0, 0, 0};

  10.         printf("请输入:");
  11.         scanf("%d", &student_now);

  12.         for(i = 0; i < student_now; i++)
  13.         {
  14.                 printf("请输入第%d的3门成绩:", i+1);
  15.                 scanf("%d, %d, %d", &student[i][0], &student[i][1], &student[i][2]);
  16.         }

  17.         for(i= 0; i< student_now; i++)
  18.         {
  19.                 Total_score[0] += student[i][0];
  20.                 Total_score[1] += student[i][1];
  21.                 Total_score[2] += student[i][2];

  22.                 student_score[i] += student[i][0] + student[i][1] + student[i][2];
  23.                 student_total_score[i] = (float)student_score[i] / 3;
  24.         }

  25.         average[0] = (float)Total_score[0]/student_now;
  26.         average[1] = (float)Total_score[1]/student_now;
  27.         average[2] = (float)Total_score[2]/student_now;

  28.         for(i= 0 ; i< student_now; i++)
  29.         {
  30.                 printf("第%d位同学的总分是:%d \r\n", i+1, student_score[i]);
  31.                 printf("第%d位同学的平均分是:%f \r\n", i+1, student_total_score[i]);
  32.         }
  33.         
  34.         printf("全班的总分是:%d, %d, %d \r\n", Total_score[0], Total_score[1], Total_score[2]);
  35.         printf("全班的平均分是:%f, %f, %f \r\n", average[0], average[1], average[2]);

  36.         return 0;

  37. }
复制代码


不过若想在 C 语言完成动态数组,那么可以使用 malloc() 函数实现

  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. int main(void){
  4.    
  5.     int course = 3;         // 課程門數
  6.     int student_count;      // 學生人數
  7.     int i,j;
  8.    
  9.     printf("輸入學生人數:");
  10.     scanf("%d", &student_count);
  11.    
  12.     int **grade, *data;
  13.     int *student_total_score, *course_total_score;
  14.    
  15.     //int grade[student_count][course];
  16.     grade = (int**)malloc(student_count*sizeof(int*) + student_count*course*sizeof(int));
  17.     data = (int*)grade+student_count;
  18.     for(i=0; i<student_count; i++, data+=course)
  19.         grade[i] = data;
  20.    
  21.     //int student_total_score[student_count] = {0};
  22.     student_total_score = (int*)malloc(student_count*sizeof(int));
  23.     for(i=0; i<student_count; i++)
  24.         student_total_score[i] = 0;
  25.    
  26.     //int course_total_score[course] = {0};
  27.     course_total_score = (int*)malloc(course*sizeof(int));
  28.     for(i=0; i<course; i++)
  29.         course_total_score[i] = 0;
  30.    
  31.     for(i=0; i<student_count; i++){
  32.         printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
  33.         for(j=0; j<course; j++){
  34.             scanf("%d", &grade[i][j]);
  35.             /* 當前學生的總分 */
  36.             student_total_score[i] += grade[i][j];
  37.             
  38.             /* 每門課程的總分 */
  39.             course_total_score[j] += grade[i][j];
  40.         }
  41.     }
  42.    
  43.     printf("\n**********課程成績**********\n");
  44.    
  45.     printf("總分:");
  46.     for(i=0; i<course; i++)
  47.         printf("%d\t", course_total_score[i]);
  48.    
  49.     printf("\n");
  50.    
  51.     printf("平均:");
  52.     for(i=0; i<course; i++)
  53.         printf("%.1f\t", (float)course_total_score[i] / student_count);
  54.    
  55.     printf("\n**********個人成績**********\n");
  56.    
  57.     /* 打印所有學生的分數、總分和平均分 */
  58.     for(i=0; i<student_count; i++){
  59.         printf("學生編號 %02d:\t", i+1);
  60.         for(j=0; j<course; j++){
  61.             printf("%d\t", grade[i][j]);
  62.         }
  63.         printf("%d\t", student_total_score[i]);
  64.         printf("%.1f\t", (float)student_total_score[i] / course);
  65.         printf("\n");
  66.     }
  67.    
  68.     free(grade);
  69.     free(student_total_score);
  70.     free(course_total_score);
  71.    
  72.     return 0;
  73. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-13 20:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表