Solved
Need to know if this program using an array is correct.
Posted on 2006-11-07
Hi Experts!
I need to know if this program that I have made is correct according to the instructions given to me.
I want to know if I did something incorrectly or misunderstood the instructions.
Instructions:
Write a complete C++ program that will let a user input 8 integer values into an array. After entering the values display the following statistics: The third element in the array The average of all the numbers The difference between the maximum and the minimum numbers in the array.
First of all, I should note that I'm not getting the difference between the max and min numbers in the array. Why?
Program:
#include <iostream>
#include <iomanip>
using namespace std;
void readem(int * & number, int &quant)
{
quant = 8;
number = new int[quant];
for(int i=0; i<quant; i++)
{
cout << "\nEnter the value for number " << i+1 << "? ";
cin >> number[i];
}
return;
}
int sum(const int array[], int num)
{
int total=0;
for(int i=0; i < num; i++)
total += array[i];
return total;
}
void sortArray(int array[], int elems)
{
int temp, diff;
bool swap;
do
{
swap = false;
for (int count = 0; count < (elems - 1); count++)
{
if (array[count] > array[count +1])
{
temp = array [count];
array[count] = array[count+1];
array[count +1] = temp;
swap = true;
}
}
} while (swap);
cout << "\nThe third element is " << array[2];
diff = (array[8] - array[0]);
cout << "\nThe difference between the max and min numbers is: "<< diff;
}
int main()
{
int * number;
int num = 0;
int total;
readem(number, num);
total = sum(number, num);
sortArray(number, num);
cout << "\nAverage = " << float(total)/num << endl;
delete[] number;
return 0;
}