help with functions and array please
Hi Guys,
Very new with C++ so please bear with me ;-)
Been asked to create a SIMPLE program with an array [20] that holds values from 0 - 100. I then need to write functions to calculate:
- largest number in the array
- smallest number in the array
- the sum of all of the numbers in the array
- the mean of all of the numbers in the array
Now on my own I have started it off by splitting up the functions into four sections:
- void get_largest_number();
- void get_smallest_number();
- void get_sum_of_number();
- void get_mean_of_number();
Now in the get_largest_number() function I have created an array that increments by one in a FOR loop every time a number is entered then each number is added to the total each time it loops. I have so far got this far, however I’m not sure about my calculations for the:
- void get_smallest_number();
- void get_sum_of_number();
- void get_mean_of_number();
I’ve had a go, but don’t think it’s right – can anyone please advise where im wrong with these other functions (if they are wrong)?
Many Thanks
Cbo
Code:
/ values.h
// Determining the largest, smallest, sum and mean
#include <iostream.h>
class values
{
public:
void get_largest_number();
void get_smallest_number();
void get_sum_of_number();
void get_mean_of_number();
protected:
int sum = 0;
int mean = 0;
};
void values::get_largest_number(int a[])
{
largest_number = a[0];
for(i=1; i<20; i++)
{
if(a[i] > largest_number)
{
largest_number = a[i];
}
}
}
void values::get_smallest_number(int a[])
{
smallest_number = a[0];
for(i=1; i<20; i++)
{
if(a[i] < smallest_number)
{
smallest_number = a[i];
}
}
}
void values::get_sum_number()
{
total=0;
for ( int i = 0; i < 20; i++)
{ total +=[i];
}
sum = total;
}
void values::get_mean_number()
{
for ( int i = 0; i < 20; i++)
{ sum +=a[i];
}
mean = sum / 20;
}