You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言如何在函数间传递2D数组并显示数组中的指定数据

C语言比赛得分管理程序修改方案

现有代码核心问题

  • gameResults函数内定义的gameInput是局部栈变量,函数执行结束后内存会被系统回收,无法在其他函数中访问;且数组大小为[1][2]仅能存储1场比赛得分,无法满足多场录入需求。
  • 函数调用前未做声明,会触发C语言隐式声明报错。
  • gameResults中的return写在循环内部,仅录入第一个得分就会终止函数,无法完成单场两队得分的录入逻辑。

最简实现方案

对于小体量作业,直接将存储得分的数组和已录入场数变量定义为全局变量即可,无需处理复杂的数组传参逻辑,所有功能函数可直接读写全局变量:

#include <stdio.h>
#include <stdlib.h>
// 预设最多支持录入100场比赛,可按需调整
#define MAX_GAMES 100
// 全局变量:所有函数可直接访问
int gameScores[MAX_GAMES][2]; // 第二维0存我方得分,1存对手得分
int gameCount = 0; // 已录入的比赛总场数

// 函数提前声明
int getUserChoice();
void inputGameResult();
void showStatistics();
void showWinGames();
void sortAndShowGames();

int main(void) {
    int choice;
    do {
        choice = getUserChoice();
        switch (choice) {
            case 1:
                inputGameResult();
                break;
            case 2:
                showStatistics();
                break;
            case 3:
                showWinGames();
                break;
            case 4:
                sortAndShowGames();
                break;
            case 5:
                printf("程序退出\n");
                break;
            default:
                printf("请输入1-5之间的有效数字\n");
                system("pause");
        }
    } while (choice != 5);
    return 0;
}

int getUserChoice() {
    int x = 0;
    printf("\n===== 操作菜单 =====\n");
    printf("[1] 录入比赛结果\n");
    printf("[2] 查看胜/平/负场统计\n");
    printf("[3] 查看所有获胜赛事结果\n");
    printf("[4] 按我方得分从低到高展示所有结果\n");
    printf("[5] 退出程序\n");
    printf("请输入你的选择:");
    // 如果是GCC编译,将scanf_s改为scanf即可
    scanf_s("%d", &x);
    return x;
}

// 录入单场比赛结果
void inputGameResult() {
    if (gameCount >= MAX_GAMES) {
        printf("已达最大录入场次上限,无法继续录入\n");
        return;
    }
    printf("请输入第%d场比赛我方得分:", gameCount + 1);
    scanf_s("%d", &gameScores[gameCount][0]);
    printf("请输入第%d场比赛对手得分:", gameCount + 1);
    scanf_s("%d", &gameScores[gameCount][1]);
    gameCount++;
    printf("录入成功\n");
    system("pause");
}

// 展示胜平负统计
void showStatistics() {
    int wins = 0, ties = 0, losses = 0;
    for (int i = 0; i < gameCount; i++) {
        if (gameScores[i][0] > gameScores[i][1]) wins++;
        else if (gameScores[i][0] == gameScores[i][1]) ties++;
        else losses++;
    }
    printf("\n===== 赛事统计 =====\n");
    printf("总场数:%d\n胜场:%d\n平场:%d\n负场:%d\n", gameCount, wins, ties, losses);
    system("pause");
}

// 展示所有获胜赛事
void showWinGames() {
    printf("\n===== 所有获胜赛事 =====\n");
    for (int i = 0; i < gameCount; i++) {
        if (gameScores[i][0] > gameScores[i][1]) {
            printf("第%d场:我方%d - %d对手\n", i+1, gameScores[i][0], gameScores[i][1]);
        }
    }
    system("pause");
}

// 按我方得分从低到高排序展示
void sortAndShowGames() {
    // 拷贝一份数组排序,不改动原始录入顺序
    int tempScores[MAX_GAMES][2];
    for (int i = 0; i < gameCount; i++) {
        tempScores[i][0] = gameScores[i][0];
        tempScores[i][1] = gameScores[i][1];
    }
    // 冒泡排序
    for (int i = 0; i < gameCount - 1; i++) {
        for (int j = 0; j < gameCount - 1 - i; j++) {
            if (tempScores[j][0] > tempScores[j+1][0]) {
                int t1 = tempScores[j][0], t2 = tempScores[j][1];
                tempScores[j][0] = tempScores[j+1][0];
                tempScores[j][1] = tempScores[j+1][1];
                tempScores[j+1][0] = t1;
                tempScores[j+1][1] = t2;
            }
        }
    }
    printf("\n===== 按我方得分升序排列 =====\n");
    for (int i = 0; i < gameCount; i++) {
        printf("我方%d - %d对手\n", tempScores[i][0], tempScores[i][1]);
    }
    system("pause");
}

关键说明

  • 如果不想用全局变量,可将数组定义在main函数内,传递数组参数时需要指定第二维的大小,函数参数写法为void func(int arr[][2], int count),调用时直接传入数组名和当前场数即可。
  • 如果需要修改排序规则,比如按两队总得分排序,只需要修改冒泡排序的判断条件即可。

内容的提问来源于stack exchange,提问作者Hagen Farrell

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 22:24:01