Hello all, I hate posting here with my questions, but I'm using this as a last resort as my head is spinning.
Background:
My assignment is to create a class that stores up to 10 integers in an array and defines 4 public methods:
setNumber - accepts integer values to store in array. Stored in next available element of array, returns true if there is room, returns false if not. ***NEED HELP ON THIS ONE***
clear - removes all values from the array so it can be reused.
***This seems really simple, but I cannot find anything to just clear all the numbers. Do I just set the size of the array back to 0?"
displayNumbers - self explanatory, think I'm good here but it is currently displaying garbage and then crashing.
getStats - determines largest, smallest, and average of values in array. Values are returned via reference parameters. Actually think i'm okay here but program is never getting that far so don't know for sure.
Also, think I have an issue with the constructor but I can't think of what else to do there.
Here's my implementation file:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#include "Array.h"
#include <iostream>
using namespace std;
Array::Array()
{
numbers[0];
}
void Array::setNumber()
{
for(int i = 0; i < 10; i++)
{
input >> numbers[i];
}
}
void Array::clear()
{
numbers[0];
}
void Array::displayNumbers()
{
for (int i = 0; i < 10; i++)
{
cout << "The numbers are " << numbers[i] << ", ";
}
}
void Array::getStats(int large, int small, int avg)
{
for (int i = 1; i < 10; i++)
{
if (numbers[i] > numbers[large])
{
large = i;
}
}
for (int index = 1; index < 10; index++)
{
if (numbers[index] < numbers[small])
{
small = index;
}
}
int numTotal;
for (int j = 0; j < 10; j++)
{
numTotal += numbers[j];
}
avg = numTotal / 10;
}
|
And here's my client code:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <iostream>
#include "Array.h"
using namespace std;
int main()
{
int largest;
int smallest;
int average;
int input;
char repeat = ' ';
Array myArray;
do
{
for(int count = 0; count < 10; count++)
{
cout << "Enter number " << count + 1 << ": ";
cin >> input;
myArray.setNumber();
}
myArray.displayNumbers();
myArray.getStats(largest, smallest, average);
cout << "The values range from " << smallest << " to " << largest <<
" with an average " << " value of " << average;
"Do you have another set of 10 numbers? (y or n): ";
cin >> repeat;
if (repeat == 'y' || repeat == 'Y')
{
myArray.clear();
}
}
while(repeat == 'y' || repeat == 'Y');
}
|