数组函数 returns 只有第一个元素,我需要整个数组。请帮助
array function returns first element only, i need whole array. help please
在函数 returnAvg 中,我需要 return 数组的代码,但它只是 return 我不熟悉指针的第一个元素。 ar[0] 完全平均,但 ar[1] 始终为 0 为什么会这样?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double returnAvg(int allTest[2][2],int students,int test);
int main ()
{
int students = 2, test = 2, i,j;
int allTest[students][test];
double ar[students];
for(i = 0; i < students; i++){
for(j = 0; j < test; j++){
printf("Student [%d] test [%d] score was> ", i + 1, j + 1);
scanf("%d", &allTest[i][j]);
}
}
*ar = returnAvg(allTest, students, test);
for(i = 0;i<students;i++){
printf("\nthe average score of student[%d] is : %.2lf\n",i+1, ar[i]);
}
return 0;
}
double returnAvg(int allTest[2][2],int students,int test){
int i,j;
double avg[students];
for(i=0;i<students;i++){
int sum = 0;
for(j=0;j<test;j++){
sum += (allTest[i][j]);
}
avg[i] = (float)sum/test;
}
return *avg;
}
您正在尝试 return 局部数组到其他错误的函数。
当您的函数 return 其本地内存消失时..
您需要对该数组使用 Malloc,然后 return 它的指针
double* returnAvg(int allTest[2][2],int students,int test){
int i,j;
double *avg;
avg = malloc(sizeof(double) * students);
for(i=0;i<students;i++){
int sum = 0;
for(j=0;j<test;j++){
sum += (allTest[i][j]);
}
avg[i] = (float)sum/test;
}
return avg;
}
不要忘记在使用后释放内存:)
无法在 C 中 return 数组,抱歉。
您可以 return 包含数组的结构,但这仅适用于固定大小的数组。
此处最好的解决方案是要求调用者进行 space:
void computeAvg( int students, int test, int input[students][test], double output[students])
{
// ...
output[i] = (double)sum/test;
// ...
}
在调用代码中:
double ar[students];
computeAvg(students, test, allTest, ar);
我重新排列了你的函数参数的顺序,这样你就可以写出正确的维度 int input[students][test]
而不是 [2][2]
,这对于 student
和 [= 的其他值来说是错误的16=].
另一个可能的解决方案是一次只计算一个学生的平均分,return double
。然后你的主函数循环执行:
double studentAvg(int test, int input[test])
{
int sum = 0;
for(int j=0;j<test;j++)
sum += input[j];
return (double)sum / test;
}
// in main
for (int i = 0; i < students; ++i)
ar[i] = studentAvg(test, allTest[i]);
在函数 returnAvg 中,我需要 return 数组的代码,但它只是 return 我不熟悉指针的第一个元素。 ar[0] 完全平均,但 ar[1] 始终为 0 为什么会这样?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double returnAvg(int allTest[2][2],int students,int test);
int main ()
{
int students = 2, test = 2, i,j;
int allTest[students][test];
double ar[students];
for(i = 0; i < students; i++){
for(j = 0; j < test; j++){
printf("Student [%d] test [%d] score was> ", i + 1, j + 1);
scanf("%d", &allTest[i][j]);
}
}
*ar = returnAvg(allTest, students, test);
for(i = 0;i<students;i++){
printf("\nthe average score of student[%d] is : %.2lf\n",i+1, ar[i]);
}
return 0;
}
double returnAvg(int allTest[2][2],int students,int test){
int i,j;
double avg[students];
for(i=0;i<students;i++){
int sum = 0;
for(j=0;j<test;j++){
sum += (allTest[i][j]);
}
avg[i] = (float)sum/test;
}
return *avg;
}
您正在尝试 return 局部数组到其他错误的函数。
当您的函数 return 其本地内存消失时..
您需要对该数组使用 Malloc,然后 return 它的指针
double* returnAvg(int allTest[2][2],int students,int test){
int i,j;
double *avg;
avg = malloc(sizeof(double) * students);
for(i=0;i<students;i++){
int sum = 0;
for(j=0;j<test;j++){
sum += (allTest[i][j]);
}
avg[i] = (float)sum/test;
}
return avg;
}
不要忘记在使用后释放内存:)
无法在 C 中 return 数组,抱歉。
您可以 return 包含数组的结构,但这仅适用于固定大小的数组。
此处最好的解决方案是要求调用者进行 space:
void computeAvg( int students, int test, int input[students][test], double output[students])
{
// ...
output[i] = (double)sum/test;
// ...
}
在调用代码中:
double ar[students];
computeAvg(students, test, allTest, ar);
我重新排列了你的函数参数的顺序,这样你就可以写出正确的维度 int input[students][test]
而不是 [2][2]
,这对于 student
和 [= 的其他值来说是错误的16=].
另一个可能的解决方案是一次只计算一个学生的平均分,return double
。然后你的主函数循环执行:
double studentAvg(int test, int input[test])
{
int sum = 0;
for(int j=0;j<test;j++)
sum += input[j];
return (double)sum / test;
}
// in main
for (int i = 0; i < students; ++i)
ar[i] = studentAvg(test, allTest[i]);