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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
//Function prototypes
void getInput(/* out */ int[],/* in */ int);
void getFileInput(/* out */ int[],/* in */ int);
void showOutput(/* out */ int[],/* in */ int);
int getTotal (/* out */ int[],/* in */ int);
int getLowest (/* out */ int[],/* in */ int);
int getHighest ( /* out */ int[],/* in */ int);
int getAverage ( /* out */ int[],/* in */ int);
int main()
{
const int SIZE = 10;
int numbers[SIZE], choice; //ID array declaration with size declaration of 10
//Array related processing done by the following function calls
//getInput(numbers, SIZE); //Two arguments, access to an array and size
getFileInput(numbers, SIZE);
showOutput(numbers, SIZE); //Two arguments, access to am array and size
cout << "\nTotal numbers: " << getTotal(numbers, SIZE) << endl;
cout << "\nSmallest number: " << getLowest(numbers, SIZE) << endl;
cout << "\nlargest number: " << getHighest(numbers, SIZE) << endl;
cout << "\nThe average of the numbers is: " << getAverage(numbers, SIZE) << endl;
return 0;
}
void getInput(/* out */ int someArray[],/* in */ int size)
{
//Input elements
cout << " Enter 10 numbers." << endl;
for(int count = 0; count < size; count++)
{
cout << "Number#" << (count+1) << ": ";
cin >> someArray[count]; //input to array a single vale using the subscript or index
}
}
void getFileInput(/* out */ int someArray[],/* in */ int size)
{
ifstream inData;
string fileName;
cout << "Enter a data file name: ";
cin >> fileName;
inData.open(fileName.c_str()); //Argument for runtime input of file name
// inData >> someArray[count]; //Priming need
/* while(inData)
{
inData >> someArray[count];
}
} */
for(int count = 0; count < size; count++)
{
inData >> someArray[count]; //input to array a single vale using the subscript or index
}
inData.close();
}
void showOutput(/* out */ int someArray[],/* in */ int size)
{
int temp;
cout << "\nList numbers below" << endl;
for(int count = 0; count < size; count++)
{
cout << "Number#" << (count+1) << ": " << someArray[count] << endl; //output to array a single vale using the subscript or index
for(int count = 0; count < size - 1; count++)
{
if(someArray[count] > someArray[count+1])
{
//we need to swap
temp = someArray[count];
someArray[count] = someArray[count+1];
someArray[count+1] = temp;
}
}
}
}
int getTotal (/* out */ int someArray[],/* in */ int size)
{
int total = 0;
//Sum of all elements
cout << "\nNumber report on 11/18/19" << endl;
for(int count = 0; count < size; count++)
{
total = total + someArray[count];
}
return total;
}
int getLowest (/* out */ int someArray[],/* in */ int size)
{
int least = 100000;
//Smallest catch of the day
least = someArray[0]; //Set first element value to least - temporary state
for(int count = 1; count < size; count++)
{
if (someArray[count] < least)
{
least = someArray[count]; //Swap values to store a smaller element
}
}
return least;
}
int getHighest (/* out */ int someArray[],/* in */ int size)
{
int most = 0;
//Largest catch of the day
most = someArray[0]; //Assign first element value to least - temporary state
for(int count = 1; count < size; count++)
{
if (someArray[count] > most)
{
most = someArray[count]; //Swap values to store a larger element
}
}
return most;
}
int getAverage (/* out */ int someArray[],/* in */ int size)
{
int total = 0, average;
//Average of all elements
for(int count = 0; count < size; count++)
{
total = total + someArray[count];
}
average = total / 10;
return average;
}
|