Arrays in a function

Hello everyone.

I'm a newbie doing a distance schooling in basic C++ and I've hit a bit of a block with my last assignment.

I am required to write a program able to take the numbers from a 10 element array and work out the arithmetic average. The header must begin with: float medel(int v[], int n) .

But I find myself at a total loss for how to be able to assign the array elements using this header block and least of all how to declare these into int main() .

I apologize the question may come across as immensely newbish but I'm finding myself at a loss as nothing in the book appears to reference anything close to this issue and the teachers, though I have means to contact them online, don't typically respond in the due time for the assignment.

Anyone up to giving me some pointers?
I'd start off by reading 10 numbers from input and storing them in an array.
Once you can do that, start coding your medel function. Loop over all the values of the array, adding each number to a cumulative variable. Then just divide that by n to get the average. Beware of integer division.
Then you can use medel like so:
 
float avg = medel( arr, 10 );

Obviously you should make 10 a named constant as a good programming practice and prefer to use doubles over floats, as it's the default floating point type.
Last edited on
My biggest difficulty putting this thing together is that 'n' also needs to be the size of the array, which must contain 10 elements, I'm not sure how to intelligently incorporate this into the function and then be able to declare it in main.

I suppose declaring is something of a challenge to me still.
arrays have to be declared with a constant anyway.

it looks like this..

int main()
{
const int size = 10;
int n = size; //not needed here but you might need it later, consider if you wanted to enter 5 values instead, n might not match size in all cases.
int arr[size]; //optionally, = {0} if you like the initialize everything approach.

// populate arr somehow. this is a quick way to get something in there.
for(int i = 0; i < size; i++)
arr[i] = i;

double ans = medel(arr, size); //one way
double ans = medel(arr, n); //better, if you did not use all 10 every time
double ans = medel(arr, 10); //terrible, but it works, do not do this.
cout << ans << endl;
return 0;
}


Oh, that makes sense.

This might be what I need, I've started working on it again with you and integralfx's advice. Hopefully I'll get it right.

Thanks for the help!
I remain having trouble translating the array itself into the average. I've made sense of similar functions looking at a built like this for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 
#include<iostream>

using namespace std;
int division(int,int);
main()
{
      int size=5;              //Array size
      int array[size];          //Declaring array
      int sum=0;                
      for(int i=0;i<size;i++)   //Loop which inputs arrays data and
                                //Calculates its sum
      {
              cout<<"Enter element number "<<i+1<<endl;
              cin>>array[i];
              sum=sum+array[i];
      }
      //Now calling division function to find the average...
      cout<<"Average of array elements is "<<division(sum,size);
      
}
int division(int sum,int size)
{
    return sum/size;
}


But the way this is meant to be built by the assignment seems nonsensical to me.
Last edited on
You've made a common mistake in assuming that the average is an integer. It usually isn't. Change line 5 to
double division(int, int);
and change lines 22-25 to:
1
2
3
4
double division(int sum, size)
{
    return (double)sum / size;
}

You may be wondering why the extra (double) at line 3 above. Without it, the program will do integer division of sum/size, producing an integer result, and the convert the integer result to a double to be returned. You want to do float-point division. By converting sum to a double, you force the compiler to convert size to double also, then divide the doubles, producing a double result.
Thanks for all the replies, its definitely helped me along and taught me some things I hadn't considered. You've all been a great help.

There's some parameters I've been given with this assignment that I can't adequately express due to my own lack of knowledge with C++. I'm doing a distance course and it turns out my teacher is on vacation which is... lovely. :p

Again, thanks everyone. I'll be able to put together the assignment even if it isn't by the given parameters, but that can't be helped as it stands.
Topic archived. No new replies allowed.