Difficulty Understanding User Defined Functions/Arrays

Had this assignment we did in class that I did't finish trying to figure it out. Calculate the average and find the min of five numbers entered by the user. I have to do it all in a User Define Fucntion and the only thing I can do in Main is call the User Defined Function.

This is what I have tried so far.

#include "stdafx.h"
#include <iostream>
using namespace std;

//int doesall ( int [ ], int, int); // Function prototype
int doesall (void);
int main ( )
{

cout << doesall() << endl;

return 0;
}

int doesall ( void)
{
// int vals
//int max = vals [0];
int max;
const int MAXG = 5;
int vals[MAXG];
max = vals[0];
for(int i=0;i<=MAXG;i++){
cout<<"Please enter five numbers: ";
cin>>vals[i];
if (max > vals [ i ])
max = vals [ i ];
return max;
}


// for (int i=1; i < numv; i++)


}

It says something about not initializing vals but haven't I? This should at least find the Min right?
Last edited on
first of all please use code tags!

second doesall has no return at the end.

vals is not initialised:

int vals[MAXG] = {0,};

this will initialise all elements to 0.

the first thing you need to do is get 5 numbers.
5! not 6;)

1
2
3
4
const int MAXG = 5;
for(int i=0;i<=MAXG;i++)
{
}


this will loop from 0 - 5, wich are 6 numbers.

once you get the numbers you can loop through the numbers again and determine the minimum and sum.

when the loop ends just divide the sum by 5 to get the average.

you have placed you return inside a forloop. a return statement stops the function at that very moment.So you for loop only runs once then stops and resturns the uninitialised variable max.
which gives any result possible;)

hope this helps.

When you have more questions i'm glad to help
Last edited on
Topic archived. No new replies allowed.