gpt4 book ai didi

c - 函数 C 不会打印

转载 作者:行者123 更新时间:2023-11-30 16:08:48 25 4
gpt4 key购买 nike

我的程序不会打印出 setAverage 和largestNum 变量的任何值。我的编译器没有出现任何错误。我在这里缺少什么?

#include <stdio.h>
#include <stdlib.h>
#define ROWS 3
#define COLS 5

void arrayNumbers(float set_numbers[ROWS][COLS], int, int, float, float);
void inputData(float set_numbers[ROWS][COLS], int, int);

int main(){
// 2D array of 15 numbers declaration and variables declared
float set_numbers[ROWS][COLS], largestNum, setAverage;
//counter variables for the loop
int column, row;
inputData(set_numbers, row, column);
arrayNumbers(set_numbers, row, column, setAverage, largestNum);
printf("\n");
printf("\tThe total average of the set: %.1f\n", setAverage);
printf("\tThe largest number in the array is: %.1f\n\n", largestNum);
return 0;
}

void inputData(float set_numbers[ROWS][COLS], int row, int column){
printf("\tYou're given an array which is a 3-by-5 array.\n");
printf("\tYou're going to put in each of the total 15 values.\n");
printf("\t///////////////////////////////////////////////////\n");
for(row = 0; row < ROWS; row++){
for(column = 0; column < COLS; column++){
printf("\tAdd data to position [%d]:[%d]", row + 1, column + 1);
scanf("%f", &set_numbers[row][column]);
}
}
printf("\n\tYour entered values are: \n\n");
for(row = 0; row < ROWS; row++){
for(column = 0; column < COLS; column++){
printf("\t%.1f\t", set_numbers[row][column]);
}
printf("\n");
}
}

void arrayNumbers(float set_numbers[ROWS][COLS], int row, int column,
float setAverage, float largestNum){
printf("\t/////////////////////////////////////////////////////////");
printf("//////////\n");
printf("\n\tThe average of your 3-by-5 sets of numbers are: \n\n");
float total, sum;
for(row = 0; row < ROWS; row++){
for(column = 0; column < COLS; column++){
sum = 0;
sum = sum + set_numbers[row][column];
total = total + set_numbers[row][column];
setAverage = total / (COLS * ROWS);

if(largestNum < set_numbers[row][column]){
largestNum = set_numbers[row][column];
}
}
printf("\n\tAverage of row [%d]: %.1f\t", row + 1, sum/COLS);
}
}

我似乎找不到我错过了 af 值或进行了错误计算的地方。在为我的工程类(class)提交程序之前,这是我需要的最后一件事。我检查了其他一些stacoverflows,但没有出现任何有用的结果。

最佳答案

在函数arrayNumbers(set_numbers, row, columns, setAverage,largestNum);setAverage 和largestNum 通过值( https://www.geeksforgeeks.org/passing-by-pointer-vs-passing-by-reference-in-c/ ) 传递。所以这些值不会更新。

在 arrayNumbers 函数中,sum 被放置在 2 个循环内。所以它只是该行的最后一个值。

关于c - 函数 C 不会打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59256144/

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