鱼C论坛

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

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

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

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

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

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

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

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

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

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

        for(i = 0; i < student_now; i++)
        {
                printf("请输入第%d的3门成绩:", 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;

}

不过若想在 C 语言完成动态数组,那么可以使用 malloc() 函数实现
#include <stdio.h>
#include <stdlib.h>

int main(void){
    
    int course = 3;         // 課程門數
    int student_count;      // 學生人數
    int i,j;
    
    printf("輸入學生人數:");
    scanf("%d", &student_count);
    
    int **grade, *data;
    int *student_total_score, *course_total_score;
    
    //int grade[student_count][course];
    grade = (int**)malloc(student_count*sizeof(int*) + student_count*course*sizeof(int));
    data = (int*)grade+student_count;
    for(i=0; i<student_count; i++, data+=course)
        grade[i] = data;
    
    //int student_total_score[student_count] = {0}; 
    student_total_score = (int*)malloc(student_count*sizeof(int));
    for(i=0; i<student_count; i++)
        student_total_score[i] = 0;
    
    //int course_total_score[course] = {0};
    course_total_score = (int*)malloc(course*sizeof(int));
    for(i=0; i<course; i++)
        course_total_score[i] = 0;
    
    for(i=0; i<student_count; i++){
        printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
        for(j=0; j<course; j++){
            scanf("%d", &grade[i][j]);
            /* 當前學生的總分 */
            student_total_score[i] += grade[i][j];
            
            /* 每門課程的總分 */
            course_total_score[j] += grade[i][j];
        }
    }
    
    printf("\n**********課程成績**********\n");
    
    printf("總分:");
    for(i=0; i<course; i++)
        printf("%d\t", course_total_score[i]);
    
    printf("\n");
    
    printf("平均:");
    for(i=0; i<course; i++)
        printf("%.1f\t", (float)course_total_score[i] / student_count);
    
    printf("\n**********個人成績**********\n");
    
    /* 打印所有學生的分數、總分和平均分 */
    for(i=0; i<student_count; i++){
        printf("學生編號 %02d:\t", i+1);
        for(j=0; j<course; j++){
            printf("%d\t", grade[i][j]);
        }
        printf("%d\t", student_total_score[i]);
        printf("%.1f\t", (float)student_total_score[i] / course);
        printf("\n");
    }
    
    free(grade);
    free(student_total_score);
    free(course_total_score);
    
    return 0;
}
IMG_20181128_215310__01.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

int main(void){
    
    int course = 3;         // 課程門數
    int student_count;      // 學生人數
    int i,j;
    
    printf("輸入學生人數:");
    scanf("%d", &student_count);
    
    int grade[student_count][course];
    int student_total_score[student_count] = {0}; 
    int course_total_score[course] = {0};
    
    for(i=0; i<student_count; i++){
        printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
        for(j=0; j<course; j++){
            scanf("%d", &grade[i][j]);
            /* 當前學生的總分 */
            student_total_score[i] += grade[i][j];
            
            /* 每門課程的總分 */
            course_total_score[j] += grade[i][j];
        }
    }
    
    printf("\n**********課程成績**********\n");
    
    printf("總分:");
    for(i=0; i<course; i++)
        printf("%d\t", course_total_score[i]);
    
    printf("\n");
    
    printf("平均:");
    for(i=0; i<course; i++)
        printf("%.1f\t", float(course_total_score[i]) / student_count);
    
    printf("\n**********個人成績**********\n");
    
    /* 打印所有學生的分數、總分和平均分 */
    for(i=0; i<student_count; i++){
        printf("學生編號 %02d:\t", i+1);
        for(j=0; j<course; j++){
            printf("%d\t", grade[i][j]);
        }
        printf("%d\t", student_total_score[i]);
        printf("%.1f\t", float(student_total_score[i]) / course);
        printf("\n");
    }
}
想知道小甲鱼最近在做啥?请访问 -> 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
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

抱歉我这里使用 C++ 编译,没注意到这个问题
想知道小甲鱼最近在做啥?请访问 -> 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;

}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

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

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

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

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

        for(i = 0; i < student_now; i++)
        {
                printf("请输入第%d的3门成绩:", 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;

}

不过若想在 C 语言完成动态数组,那么可以使用 malloc() 函数实现
#include <stdio.h>
#include <stdlib.h>

int main(void){
    
    int course = 3;         // 課程門數
    int student_count;      // 學生人數
    int i,j;
    
    printf("輸入學生人數:");
    scanf("%d", &student_count);
    
    int **grade, *data;
    int *student_total_score, *course_total_score;
    
    //int grade[student_count][course];
    grade = (int**)malloc(student_count*sizeof(int*) + student_count*course*sizeof(int));
    data = (int*)grade+student_count;
    for(i=0; i<student_count; i++, data+=course)
        grade[i] = data;
    
    //int student_total_score[student_count] = {0}; 
    student_total_score = (int*)malloc(student_count*sizeof(int));
    for(i=0; i<student_count; i++)
        student_total_score[i] = 0;
    
    //int course_total_score[course] = {0};
    course_total_score = (int*)malloc(course*sizeof(int));
    for(i=0; i<course; i++)
        course_total_score[i] = 0;
    
    for(i=0; i<student_count; i++){
        printf("學生編號 %02d 的 %d 個成績輸入(空白分隔):", i+1, course);
        for(j=0; j<course; j++){
            scanf("%d", &grade[i][j]);
            /* 當前學生的總分 */
            student_total_score[i] += grade[i][j];
            
            /* 每門課程的總分 */
            course_total_score[j] += grade[i][j];
        }
    }
    
    printf("\n**********課程成績**********\n");
    
    printf("總分:");
    for(i=0; i<course; i++)
        printf("%d\t", course_total_score[i]);
    
    printf("\n");
    
    printf("平均:");
    for(i=0; i<course; i++)
        printf("%.1f\t", (float)course_total_score[i] / student_count);
    
    printf("\n**********個人成績**********\n");
    
    /* 打印所有學生的分數、總分和平均分 */
    for(i=0; i<student_count; i++){
        printf("學生編號 %02d:\t", i+1);
        for(j=0; j<course; j++){
            printf("%d\t", grade[i][j]);
        }
        printf("%d\t", student_total_score[i]);
        printf("%.1f\t", (float)student_total_score[i] / course);
        printf("\n");
    }
    
    free(grade);
    free(student_total_score);
    free(course_total_score);
    
    return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-10-3 00:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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