使用函数查找数组的平均值 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28569738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 21:00:47  来源:igfitidea点击:

Using a function to find the average of an array C++

c++arraysfunction

提问by molebox

the purpose of this task is to find the average of an array but not within the main, I have to call a function to do the sum and show the average.

这个任务的目的是找到一个数组的平均值,但不是在主数组中,我必须调用一个函数来求和并显示平均值。

I though my code was sound but it just returns " the average is 011014F1"

我虽然我的代码是健全的,但它只是返回“平均值是 011014F1”

I have tried a few different ways of doing the function but I've gone wrong somewhere, maybe everywhere!

我尝试了几种不同的方法来完成这个功能,但我在某个地方出错了,也许到处都是!

Just a heads up, im just starting out with programing.

只是提醒一下,我刚刚开始编程。

Heres my code:

这是我的代码:

#include <iostream>
#include <vector>

using namespace std;


void printArray(int theArray[], int sizeOfarray);
float average(float numbers[], float size, float arrayAverage);

int main()
{

   int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

   printArray(array, 10);

   cout << "The average is: " << average << endl;

   return 0;
}

void printArray(int theArray[], int sizeOfarray)
{
   for (int x = 0; x < sizeOfarray; x++)
  {
    cout << theArray[x] << endl;

  }
}

float average(float numbers[], float size, float arrayAverage)
{
        double sum = 0.0;
        for (int x = 0; x < size; x++)
    {
        sum += numbers[x];
        arrayAverage = sum / size;
    }
        return (arrayAverage);
}

I had the float average function initially set as a float with int for 'numbers', 'size' and 'arrayAverage' but thought i would change them all to float so they dont clash. like converting an int to a float etc..

我将浮点平均函数最初设置为带有 int 的浮点数,用于“数字”、“大小”和“arrayAverage”,但我想我会将它们全部更改为浮点数,这样它们就不会发生冲突。比如将 int 转换为 float 等。

As i said im new to this so my logic is not really there but i think im n the right tracks.

正如我所说,我是新手,所以我的逻辑并不存在,但我认为我是正确的。

Any idea why its returning 011014F1 and numbers like that instead of just the average of 1-10?

知道为什么它返回 011014F1 和类似的数字而不是 1-10 的平均值吗?

Any tips much appreciated!

任何提示非常感谢!

回答by Scott Hunter

averageis a function, which you need to call, and print what it returns. What you are printing now is the addressof that function.

average是一个函数,您需要调用它并打印它返回的内容。你现在打印的是那个函数的地址

回答by aruisdante

There are a number of problems here. First:

这里有很多问题。第一的:

cout << "The average is: " << average << endl;

This is simply printing out the address of the averagefunction, not calling it. What you wanted to do was:

这只是打印出average函数的地址,而不是调用它。你想做的是:

cout << "The average is: " << average(array, 10, 0) << endl;

Second, your method signature has all kinds of type missmatches. The expected array value type is float, yet you're passing it an array of int. This won't work, as the compiler will not allow the implicit conversion from int[]to float[]. Your sizeargument should be an intin the method signature as well, not float, since array sizes are always integers.

其次,您的方法签名有各种类型不匹配。预期的数组值类型是float,但您传递给它的是int. 这是行不通的,因为编译器不允许从int[]到的隐式转换float[]。您的size参数也应该是int方法签名中的an ,而不是float,因为数组大小始终是整数。

Third, the arrayAverageparameter seems to have no purpose except to possibly throw off your math. You use it as a running accumulator, which is fine, but there's no reason to pass it to the function, it could just be a local value. So, your method signature should look like this:

第三,该arrayAverage参数似乎没有任何用途,只是可能会影响您的数学计算。您将它用作正在运行的累加器,这很好,但是没有理由将它传递给函数,它可能只是一个本地值。因此,您的方法签名应如下所示:

float average(float numbers[], int size);

Finally, your math for calulating the average of an array is wrong. You do:

最后,您计算数组平均值的数学是错误的。你做:

for (int x = 0; x < size; x++)
{
    sum += numbers[x];
    arrayAverage = sum / size;
}

Particularly, the arrayAverage = sum / sizeis wrong. Or rather, is only right during the final loop iteration. Meaning this is just wasted math. It should be:

特别是,arrayAverage = sum / size是错误的。或者更确切地说,仅在最终循环迭代期间才正确。这意味着这只是浪费数学。它应该是:

float average(float numbers[], int size) {
    double sum = 0;
    for (int x = 0; x < size; x++)
    {
        sum += numbers[x];
    }
    return sum /(double)size;
}

回答by molebox

You are not passing any thing to your function average, float average(float numbers[], float size, float arrayAverage)You should pass your array as first parameter and the size of the array in the second, the third one you dont need it , I recommand you to delete itYour function will be :

您没有将任何东西传递给您的函数 average, float average(float numbers[], float size, float arrayAverage) 您应该将数组作为第一个参数传递,并将数组的大小作为第二个参数传递,第三个参数不需要它,我建议你删除它你的功能是:

float average(float numbers[], float size)
{
float average;
        double sum = 0.0;
        for (int x = 0; x < size; x++)
    {
        sum += numbers[x];
        arrayAverage = sum / size;
    }
        return (average);
}

and in your main you do a float averageResult = average(array, size); qDebug()《 averageResult;

在你的主要你做一个 float averageResult = average(array, size); qDebug()《averageResult;

回答by LampPost

#include <iostream>
#include <vector>

using namespace std;

void printArray(int theArray[], int sizeOfarray);

int main()
{
   int array[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
   printArray(array, 10);
   return 0;
}

void printArray(int theArray[], int sizeOfarray)
{
   for (int x = 0; x < len(theArray); x++)
  {
    average = average + theArray[x]
  }
  average = average/(len(theArray));
  cout << average;
}