gpt4 book ai didi

c - 在c中按列迭代二维数组

转载 作者:行者123 更新时间:2023-11-30 17:06:10 25 4
gpt4 key购买 nike

请有人向我解释如何按列迭代二维数组,即保持行不变,直到到达列的末尾?谢谢。

我正在编写一个程序来计算一个类(class)学生的某些科目的分数。它计算每个学生获得的总分以及每个科目的最高分。

我遇到困难的棘手部分是计算每个科目的最低分数。这将需要向下迭代每一列,并仅在获得该列(主题)的最高分数后才更改行

代码(也 - http://pastebin.com/FmP4yX59 ):

#include <stdio.h>
#include <stdlib.h>
#define MAX_ST 3
#define MAX_SU 3

int main() {
int student[MAX_ST][MAX_SU];

int total_mark[MAX_ST];
printf("\n_________________PRESS TAB KEY TO SHIFT A COLUMN_________________\n");
printf("\nRoll_NO subject1 subject2 subject3\n");
printf("________________________________________________________________\n");
for(int i = 0; i < MAX_ST; i++) {
printf("%d\t|\t",i+1);
for(int j = 0; j < MAX_SU; j++){
scanf("%d",&student[i][j]);
}
}
//total marks obtained by each student
printf("\nTOTAL MARKS\n");
for(int i = 0; i < MAX_ST; i++) {
total_mark[i] = 0;
for(int j = 0; j < MAX_SU; j++) {
total_mark[i] += student[i][j];
}
printf("Student %d = %d\n",i+1, total_mark[i]);
}

//highest total mark
int overRoll = 0;
int high_S = 0;
for(int i = 0; i < MAX_ST; i++) {
if(high_S <= total_mark[i]) {
high_S = total_mark[i];
overRoll = i;
}
}

printf("Highest total marks is %d for Student %d\n", high_S,overRoll + 1);

//highest mark in each subject
//where the problem lies
int highest_mark[MAX_SU];
int highest_sudent[MAX_SU];

for(int i = 0; i < MAX_SU;i++){
for(;j = t;) {
Highest_mark[i] = 0;
if(student[i][j] > highest_mark[i]) {
highest_mark[j] = student[][j];
highest_student[i] = i;
}
}
printf("the highest");
}
}

最佳答案

通常你会做这样的事情:

typedef void action_t (int*);

void traverse_matrix ( size_t col,
size_t row,
int matrix[col][row],
action_t* action )
{
for(size_t x=0; x<col; x++)
{
for(size_t y=0; y<row; y++)
{
action(&matrix[x][y]);
}
}
}

关于c - 在c中按列迭代二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34872056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com