简体   繁体   English

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

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

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. 我最初将float平均函数设置为带有浮点数的int来表示'numbers','size'和'arrayAverage',但我想我会将它们全部更改为float以便它们不发生冲突。 like converting an int to a float etc.. 例如将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! 任何提示,不胜感激!

average is a function, which you need to call , and print what it returns. average是一个函数,您需要调用并打印它返回的内容。 What you are printing now is the address of that function. 现在要打印的是该功能的地址

There are a number of problems here. 这里有很多问题。 First: 第一:

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

This is simply printing out the address of the average function, not calling it. 这只是打印出average函数的地址,而不是调用它。 What you wanted to do was: 您想做的是:

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 . 期望的数组值类型为float ,但您正在向其传递一个int数组。 This won't work, as the compiler will not allow the implicit conversion from int[] to float[] . 这将不起作用,因为编译器将不允许从int[]float[]的隐式转换。 Your size argument should be an int in the method signature as well, not float , since array sizes are always integers. 您的size参数也应该是方法签名中的int ,而不是float ,因为数组大小始终是整数。

Third, the arrayAverage parameter seems to have no purpose except to possibly throw off your math. 第三, arrayAverage参数似乎没有任何意义,只是可能使您无法进行数学运算。 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: 因此,您的方法签名应如下所示:

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 / size is wrong. 特别是, arrayAverage = sum / size是错误的。 Or rather, is only right during the final loop iteration. 或更确切地说,仅在最终循环迭代期间正确。 Meaning this is just wasted math. 这意味着这只是在浪费数学。 It should be: 它应该是:

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

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 : 您没有将任何东西传递给函数平均值,浮点平均值(浮点数[],浮点大小,浮点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); 在您的主代码中,您进行了一个float averageResult = average(array,size); qDebug()《 averageResult; qDebug()《 averageResult;

#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;
}

1 : in your function "float average(float numbers[], float size, float arrayAverage)" you need not to give arrayAverage as a perimeter, it should be initialize within function scope , 2 : if you are beginners , well you will learn about pointers later, in which you will be come to know function is actually address ! 1:在函数“ float average(float average(float数[],float大小,float arrayAverage)”中)中,您不必将arrayAverage设置为周长,应在函数范围内对其进行初始化; 2:如果您是初学者,那么您将学到关于指针,稍后您将了解其中的功能实际上是地址! 3 : using this line (cout << "The average is: " << average(array,10) << endl;) ,remember initialize float arrayAverage within function scope 3:使用此行(cout <<“平均值为:” << average(array,10)<< endl;),记住初始化float arrayAverage在函数范围内

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM