61 lines
No EOL
1.6 KiB
C++
61 lines
No EOL
1.6 KiB
C++
//Corey Williams
|
|
//COP3530 01Z
|
|
//Project 2
|
|
/*
|
|
Prompt the user for 5 integer values
|
|
Stores the elements in a vector
|
|
Displays the sorted elements
|
|
Displays the smallest element
|
|
Displays the largest element
|
|
Displays the total of all elements
|
|
Displays the average of all the elements
|
|
*/
|
|
|
|
#include <iostream>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
int main()
|
|
{
|
|
vector<int> userInputs;
|
|
int repFlag;
|
|
int sum = 0;
|
|
|
|
//Loops to collect 5 integers from user and pushes to vector
|
|
for (int i = 1; i < 6; i++) {
|
|
int x;
|
|
cout << "Input Integer " << i << ": ";
|
|
cin >> x;
|
|
userInputs.push_back(x);
|
|
}
|
|
|
|
//Iterates through vector to order from smallest to largest
|
|
//Repflag is used to repeat iteration through the vector until no new changes are made.
|
|
do {
|
|
repFlag = 0;
|
|
for (vector<int>::iterator i = userInputs.begin(); i + 1 != userInputs.end(); ++i) {
|
|
if (*i > *(i + 1)) {
|
|
int tmp = *i;
|
|
*i = *(i + 1);
|
|
*(i + 1) = tmp;
|
|
repFlag = 1;
|
|
}
|
|
}
|
|
} while (repFlag == 1);
|
|
|
|
//Iterates through sorted vector to print in ascending order, also collects sum for later use
|
|
cout << endl << "Ordered: ";
|
|
for (int i : userInputs) {
|
|
cout << i << " ";
|
|
sum += i;
|
|
}
|
|
cout << endl;
|
|
|
|
cout << "Smallest: " << userInputs.front() << endl;
|
|
cout << "Largest: " << userInputs.back() << endl;
|
|
cout << "Total: " << sum << endl;
|
|
cout << "Average: " << sum / 5 << endl;
|
|
|
|
return 0;
|
|
} |