在主函数中保持零

时间:2014-02-09 22:21:39

标签: c function

我添加了一些printf stantments以查看信息是否是计算机并正确传递,并且在calcMean函数中正确计算时,我在main函数中不断获得零。我不确定我的错误在哪里。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void calcMean(int [], int, float);
void calcVariance(int [], int, float);



int main(void)
{
/*create array and initialize it with values*/
int mainArr [ ] = {71,1899,272,1694,1697,296,722,12,2726,1899,
                ....... 
                    1652,488,1123,17,290,1324,2495,1221,2361,1244,
                    813,2716,1808,2328,2840,1059,2382,2391,2453,1672,
                    1469,778,2639,357,2691,1113,2131,23,2535,1514,
                    2317,45,1465,1799,2642,557,1846,1824,1144,1468,-1};
/*create initilized values for variables*/
float mean = 0.0;
int counter = 0;

calcMean(mainArr, counter, mean);

calcVariance(mainArr, counter, mean);

printf ("Mean: %10.2f\n", mean);       /*gives the average number to one decimal place*/
printf ("counter: %10.0d\n", counter);
getchar();
}/*End of the program*/

void calcMean(int mainArr[], int counter, float mean)/*This function finds the     Mean(average) of the function, and the size of the array*/
{   
int sentinel = -1;
float sum = 0.0;
int index = 0;
for (index; mainArr[index] != sentinel; index++) /*gets the total numbers in the function and the sum*/
{
    counter++;
    sum = sum + mainArr[index];
}       /*end of the sumation and counting of integers*/

printf ("%0.0d\n", counter);
mean = ((float) sum / counter);             /*Divides the sum by the total number of numbers to find the mean*/
printf ("mean: %10.2f\n", mean);
}

void calcVariance(int mainArr[], int counter, float mean)
{
int varindex = 0;
int numerator =0;
int square = 0;
int variance = 0;
int sumation = 0;
for(varindex; varindex<counter; varindex++)
{
    numerator = (mainArr[varindex] - mean);
    square = (numerator * numerator);
    sumation = sumation + square;
    variance = sumation / (counter-1);
}
printf ("variance %10.2f\n", variance);
}

4 个答案:

答案 0 :(得分:1)

您假设您的函数更改了外部变量的值。它不会。

calcMean(mainArr, counter, mean);
                  ^        ^ pass by value

void calcMean(int mainArr[], int counter, float mean)
                                 ^              ^ receive by value

修改函数内的counter只会更改局部变量。

答案 1 :(得分:1)

功能参数 复制 到函数中。因此函数内部的变量不会改变外部函数中的参数。

对于要从函数传回多个值的任何函数,您需要执行以下操作。 (如果只需要传回一个可以使用函数return语句返回的值)。

//声明您希望传递给调用函数的参数作为指针

void calcMean(int [], *int, *float);

//传递变量的地址:

calcMean(mainArr, &counter, &mean);

//使用derefence operator *

获取calcMean中指针的值
printf ("%0.0d\n", *counter);
*mean = ((float) sum / *counter);   

答案 2 :(得分:0)

C是一种按值传递的语言。修改其他功能中的meancounter不会影响main()中的值。

答案 3 :(得分:0)

OP后来请求了很少使用指针的答案。

正如其他人(@suspectus,@ Karoly Horvath,@ Car Norum)正确指出传递给函数mean的问题并没有改变它。

main()有一个名为mean的对象。该对象的值用于在mean的参数列表中初始化名为calcMean()的对象。 calcMean()继续将mean设置为某个值。在函数结束时,mean中的原始main()

相反,请使用函数calcMean() return 值在mean中设置main()

// void calcMean(int mainArr[], int counter, float mean)
float calcMean(int mainArr[], int counter) {
  float mean; 
  ...
  mean = ((float) sum / counter);
  return mean;
}

int main(void) {
  ... 
  float mean = 0.0;
  ...
  // calcMean(mainArr, counter, mean);
  mean = calcMean(mainArr, counter);
  ...
}

calcVariance

执行相同的操作