1

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"

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..

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?

Any tips much appreciated!

6
  • What you're printing is the address of the average function. You have to invoke it with parameters in order for it to work (how else would it know what array is it meant to use?) Commented Feb 17, 2015 at 19:41
  • There is a typo. In the cout you want average(array, 10) else, as you discovered, it prints the function pointer. Commented Feb 17, 2015 at 19:42
  • 1
    @user3528438: std::valarray is about the worst thing of the entire C++ standard library, right next to std::vector<bool>. I've never seen it recommended (or used) anywhere, and the fact that it's in the standard library is more of a historical accident. See e.g. Josuttis' "The C++ Standard Library" book for details. Commented Feb 17, 2015 at 20:08
  • You should take a different C++ course, one that teaches std::vector before arrays. Arrays are more work when passing to and from functions. Commented Feb 17, 2015 at 20:26
  • @ChristianHackl if you enjoy hand-writing average/min/max functions so much Commented Feb 17, 2015 at 21:11

4 Answers 4

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Also, as written, the arrayAverage parameter is most definitly not going to do what the OP intends for it to do. In fact, their math for calculating average is also incorrect.
Actually, shouldn't 1 be printed because the function pointer is converted to a bool? This is in fact what happens with VC 2013, including a warning about the conversion (warning C4305: 'argument' : truncation from 'float (*)(float [],float,float)' to 'std::_Bool'). I get the same behaviour on ideone.com, by the way. The question is: why does the OP get an apparent address printed here?
@ChristianHackl it's possible that this is compiler-specific (or platform specific). I certainly wouldn't expect a function pointer to be implicitly cast to bool in a stream.
2

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. 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. This won't work, as the compiler will not allow the implicit conversion from int[] to float[]. Your size argument should be an int in the method signature as well, not float, since array sizes are always integers.

Third, the arrayAverage parameter 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:

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

9 Comments

just hit enter by mistake, anyway i did actually do the math that way in another go but the whole thing was wrong and didnt work so i changed it....for the worse it would seem. as i sadi above my logic and understanding isnt quite there to be honst and that is why i changed the int's to float etc... but i have changed them back now. though on "the average is: " << average(array,10,0) <<endl; the 'array' word is highlightd red and the program wont run...
it says that int is incompatible with float..... is that because of the 'numbers' is a float?
If you've modfied the average method's signature as I suggested, then it'd just be average(array, 10) since the arrayAverage parameter is no longer needed as an argument.
@molebox ah, correct, I forgot that C++ will not implicitly cast an int[] to a float[]. When passing arrays betwen methods, their types must match. There are ways to get around this using templates, but I highly doubt at this point you'd want to get into that.
I got rid of the arrayAverage and changed the float numbers to an int, i also tidied up the calculation per you said and low and behold the bloody thing works! Thank you for your help! the frustrating thing was that i actually had the same calculation before but i scraped the whole thing and started again thinking that that was one of my problems. So the reason for me getting a return for the average as 011014F1 was that i didnt call the function correctly?
|
0

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 :

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;

1 Comment

It's a waste of time to have arrayAverage = sum / size; inside of the loop, you can move that to the end just outside the loop.
-1
#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;
}

2 Comments

What's len(theArray) ? If you haven't coded in C++ for long, don't blindly post wrong codes
No you're not, you code doesn't compile and you're misleading OP and others saying there's something like len function, which infact there's no such function. I suggest you to delete this post or re-write the fixed code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.