Currently working on coding a class which takes in an arbitrary list of integers and performs various functions on the list.
- Get max
- Get min
- Get average
- Amount of numbers in list
- Search for integer in list
So far, I've gotten the code below, but my max/min is not working. It gives a result of the 2nd integer entered in the list and only gives one result ie. I want it to give both the max and min. Have a long way to go yet but if I could that working it would be great help. Here's my code so far
Code:
#include <iostream>
using namespace std;
// prototype declarations
class list
{
public:
int sumArray(int integerArray[], int sizeOfloatArray);
int maxArray(int integerArray[], int sizeOfloatArray);
float avgArray(int integerArray[], int sizeOfloatArray);
void displayArray(int integerArray[], int sizeOfloatArray);
};
int main()
{
list l;
cout << "This program sums values entered by the user\n";
cout << "Terminate the loop by entering a negative number\n" << endl;
// store numbers into an array
int inputValues[128];
int numberOfValues;
int integerValue;
for(numberOfValues = 0; numberOfValues < 128; numberOfValues++)
{
// fetch another number
cout << "Enter integer: ";
cin >> integerValue;
// if it’s negative...
if (integerValue < 0)
{
// ...then exit
break;
}
// ... otherwise store the number
// into the storage array
inputValues[numberOfValues] = integerValue;
}
// now output the values and the sum of the values
l.displayArray(inputValues, numberOfValues);
cout << "The sum is " << l.sumArray(inputValues, numberOfValues) << endl;
cout << "The average is " << l.avgArray(inputValues, numberOfValues) << endl;
cout << "No. of elements: " << numberOfValues << endl;
cout << "The maximum is " << l.maxArray(inputValues, numberOfValues) << endl;
// wait until user is ready before terminating program
// to allow the user to see the program results
return 0;
}
// displayArray - display the members of an
// array of length sizeOfloatArray
void list::displayArray(int integerArray[], int sizeOfArray)
{
cout << "The value of the array is:" << endl;
for (int i = 0; i < sizeOfArray; i++)
{
cout << i << ": " << integerArray[i] << endl;
}
}
// sumArray - return the sum of the members of an
// integer array
int list::sumArray(int integerArray[], int sizeOfArray)
{
int sum = 0;
for (int i = 0; i < sizeOfArray; i++)
{
sum += integerArray[i];
}
return sum;
}
int list::maxArray(int integerArray[], int sizeOfArray)
{
int max = integerArray[0];
for (int i=0; i<sizeOfArray; i++){
if (integerArray[i] > max) {
max=integerArray[i];
}
return max;
}
}
/*int list::minArray(int integerArray[], int sizeOfArray)
{
int min = integerArray[0];
for (int i=0; i<sizeOfArray; i++){
if (integerArray[i] < min) {
min = integerArray[i];
}
return min;
}
}*/
float list::avgArray(int integerArray[], int sizeOfArray)
{
float sum = 0;
for (int i = 0; i < sizeOfArray; i++)
{
sum += integerArray[i];
}
return sum/sizeOfArray;
}