In: Computer Science
Programing lanugaue is C++
Purpose of code:
Plan and code a menu-driven modular program utilizing an array
An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to
Display count of even numbers, count of odd numbersand the sum
values in each array
Determine the average of each array
Determine the median of each array
Sort each array in ascending order(use bubble sort) and output
results to a file
Display X number of the highest values(for example, 3 highest
values) in each array.Ask a user for the number of the highest
values
Display X number of the lowest values(for example, 7 lowest values)
in each array.Ask a user for the number of the lowest values
A user should be able to run many as many times as a user
wants
Notes :
Clearly label all output
Use switch statement to implement the menu
Watch put for array boundaries.C would not do it for you.
You only need to create one function to determine the average.Just
call it twice to determine the average of each array.The same
applies to the rest of the menu options.
A user should be able to run the menu as many times as a user
wants
Thoroughly test your program.Your grade partially depends on the
quality of your test data.
Must comply with the posted guidelinesand standards
Well document your code(comments)
Question: Is it possible to rewirte this code with the same purpose but without the Vector function? Is is possible to make a small edits or will I have to rewrite the whol code? Can someone show me how rewrite the code without a vector function?
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<iomanip>
using namespace std;
void sort(vector<int>& nums);
double getMedian(vector<int> nums);
vector<int> readFile(string filename);
void displayMenu();
int main()
{
string filename = "input.txt";
vector<int> allNums = readFile(filename);
vector<int> allEvens, allOdds;
for (int i = 0; i < allNums.size(); i++)
{
if (allNums[i] % 2 == 0)
allEvens.push_back(allNums[i]);
else
allOdds.push_back(allNums[i]);
}
int choice;
cout << setprecision(2) << fixed;
do
{
displayMenu();
cin >> choice;
switch (choice)
{
case 1:
{
int sumEven = 0, sumOdd = 0;
for (int i = 0; i < allEvens.size(); i++)
sumEven += allEvens[i];
for (int i = 0; i < allOdds.size(); i++)
sumOdd += allOdds[i];
cout << "\nCount of even numbers = " << allEvens.size()
<< endl << "Count of odd numbers = " <<
allOdds.size() << endl;
cout << "Sum of all even numbers = " << sumEven
<< endl << "Sum of all odd numbers = " << sumOdd
<< endl;
break;
}
case 2:
{
int sumEven = 0, sumOdd = 0;
for (int i = 0; i < allEvens.size(); i++)
sumEven += allEvens[i];
for (int i = 0; i < allOdds.size(); i++)
sumOdd += allOdds[i];
double avgEven = 0.0, avgOdd = 0.0;
avgEven = (double)sumEven / (double)allEvens.size();
avgOdd = (double)sumOdd / (double)allOdds.size();
cout << "\nAverage of all even numbers = " << avgEven
<< endl << "Average of all odd numbers = " <<
avgOdd << endl;
break;
}
case 3:
{
cout << "\nMedian of all the even numbers = " <<
getMedian(allEvens) << endl << "Median of all the even
numbers = " << getMedian(allOdds) << endl;
break;
}
case 4:
{
string outFileName;
cout << "\nEnter the output filename: ";
cin >> outFileName;
sort(allEvens);
sort(allOdds);
ofstream outFile(outFileName.c_str());
if (!outFile.is_open())
{
cout << "Error in opening " << outFileName << "!
Exiting..\n";
exit(EXIT_FAILURE);
}
outFile << "Sorted array of Evens:\n";
for (int i = 0; i < allEvens.size(); i++)
outFile << allEvens[i] << endl;
outFile << "\nSorted array of Odds:\n";
for (int i = 0; i < allOdds.size(); i++)
outFile << allOdds[i] << endl;
cout << "Sorted array successfully written to " <<
outFileName << ".\n";
break;
}
case 5:
{
sort(allEvens);
sort(allOdds);
int XEven, XOdd;
// EVEN
cout << "\nHow many highest numbers do you want to display
for the even numbers array? ";
cin >> XEven;
while (XEven < 1 || XEven > allEvens.size())
{
cout << "Please enter a value between 1 & " <<
allEvens.size() << "!\n";
cout << "How many highest numbers do you want to display for
the even numbers array? ";
cin >> XEven;
}
cout << XEven << " highest number(s) in the even array:
";
for (int i = XEven; i > 0; i--)
{
cout << allEvens[i] << " ";
}
// ODD
cout << "\nHow many highest numbers do you want to display
for the odd numbers array? ";
cin >> XOdd;
while (XOdd < 1 || XOdd > allOdds.size())
{
cout << "Please enter a value between 1 & " <<
allOdds.size() << "!\n";
cout << "How many highest numbers do you want to display for
the odd numbers array? ";
cin >> XOdd;
}
cout << XOdd << " highest number(s) in the odd array:
";
for (int i = XOdd; i > 0; i--)
{
cout << allOdds[i] << " ";
}
cout << endl;
break;
}
case 6:
{
sort(allEvens);
sort(allOdds);
int XEven, XOdd;
// EVEN
cout << "\nHow many lowest numbers do you want to display for
the even numbers array? ";
cin >> XEven;
while (XEven < 1 || XEven > allEvens.size())
{
cout << "Please enter a value between 1 & " <<
allEvens.size() << "!\n";
cout << "How many lowest numbers do you want to display for
the even numbers array? ";
cin >> XEven;
}
cout << XEven << " lowest number(s) in the even array:
";
for (int i = 0; i < XEven; i++)
{
cout << allEvens[i] << " ";
}
// ODD
cout << "\nHow many lowest numbers do you want to display for
the odd numbers array? ";
cin >> XOdd;
while (XOdd < 1 || XOdd > allOdds.size())
{
cout << "Please enter a value between 1 & " <<
allOdds.size() << "!\n";
cout << "How many lowest numbers do you want to display for
the odd numbers array? ";
cin >> XOdd;
}
cout << XOdd << " lowest number(s) in the odd array:
";
for (int i = 0; i < XOdd; i++)
{
cout << allOdds[i] << " ";
}
cout << endl;
break;
}
case 0:
{
cout << "\nThanks..Goodbye!\n";
exit(EXIT_SUCCESS);
}
default:
cout << "\nInvalid selection!\n";
}
cout << endl;
} while (choice != 0);
return 0;
}
void sort(vector<int>& nums)
{
int n = nums.size();
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (nums[j] > nums[j + 1])
{
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
double getMedian(vector<int> nums)
{
int n = nums.size();
sort(nums);
if (n % 2 == 0)
return (double)nums[n / 2];
return (double)(nums[(n - 1) / 2] + nums[n / 2]) / 2.0;
}
vector<int> readFile(string filename)
{
vector<int> allNums;
ifstream inFile(filename.c_str());
string line;
if (!inFile.is_open())
{
cout << "Error in opening " << filename << "!
Exiting..\n";
exit(EXIT_FAILURE);
}
while (getline(inFile, line))
{
allNums.push_back(stod(line));
}
inFile.close();
return allNums;
}
void displayMenu()
{
cout << "Choose from the following options:\n1. Count of even
numbers, count of odd numbers and sum values in each array\n2.
Average of each array\n3. Median of each array\n" <<
"4. Sort each array in ascending order\n5. Display X numbers of the
highest values in each array\n6. Display X numbers of the lowest
values in each array\n0. Exit\nYour selection >> ";
}
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<iomanip>
#include<cstdlib>
using namespace std;
using std::ifstream;
using std::cerr;
using std::cout;
using std::endl;
void sort(filename);
indata.open(filename);
indata.readFile(string filename);
void displayMenu();
int main()
{
string filename = "input.txt";
vector<int> allNums = readFile(filename);
vector<int> allEvens, allOdds;
for (int i = 0; i < allNums.size(); i++)
{
if (allNums[i] % 2 == 0)
allEvens.push_back(allNums[i]);
else
allOdds.push_back(allNums[i]);
}
int choice;
cout << setprecision(2) << fixed;
do
{
displayMenu();
cin >> choice;
switch (choice)
{
case 1:
{
int sumEven = 0, sumOdd = 0;
for (int i = 0; i < allEvens.size(); i++)
sumEven += allEvens[i];
for (int i = 0; i < allOdds.size(); i++)
sumOdd += allOdds[i];
cout << "\nCount of even numbers = " << allEvens.size()
<< endl << "Count of odd numbers = " <<
allOdds.size() << endl;
cout << "Sum of all even numbers = " << sumEven
<< endl << "Sum of all odd numbers = " << sumOdd
<< endl;
break;
}
case 2:
{
int sumEven = 0, sumOdd = 0;
for (int i = 0; i < allEvens.size(); i++)
sumEven += allEvens[i];
for (int i = 0; i < allOdds.size(); i++)
sumOdd += allOdds[i];
double avgEven = 0.0, avgOdd = 0.0;
avgEven = (double)sumEven / (double)allEvens.size();
avgOdd = (double)sumOdd / (double)allOdds.size();
cout << "\nAverage of all even numbers = " << avgEven
<< endl << "Average of all odd numbers = " <<
avgOdd << endl;
break;
}
case 3:
{
cout << "\nMedian of all the even numbers = " <<
getMedian(allEvens) << endl << "Median of all the even
numbers = " << getMedian(allOdds) << endl;
break;
}
case 4:
{
string outFileName;
cout << "\nEnter the output filename: ";
cin >> outFileName;
sort(allEvens);
sort(allOdds);
ofstream outFile(outFileName.c_str());
if (!outFile.is_open())
{
cout << "Error in opening " << outFileName << "!
Exiting..\n";
exit(EXIT_FAILURE);
}
outFile << "Sorted array of Evens:\n";
for (int i = 0; i < allEvens.size(); i++)
outFile << allEvens[i] << endl;
outFile << "\nSorted array of Odds:\n";
for (int i = 0; i < allOdds.size(); i++)
outFile << allOdds[i] << endl;
cout << "Sorted array successfully written to " <<
outFileName << ".\n";
break;
}
case 5:
{
sort(allEvens);
sort(allOdds);
int XEven, XOdd;
// EVEN
cout << "\nHow many highest numbers do you want to display
for the even numbers array? ";
cin >> XEven;
while (XEven < 1 || XEven > allEvens.size())
{
cout << "Please enter a value between 1 & " <<
allEvens.size() << "!\n";
cout << "How many highest numbers do you want to display for
the even numbers array? ";
cin >> XEven;
}
cout << XEven << " highest number(s) in the even array:
";
for (int i = XEven; i > 0; i--)
{
cout << allEvens[i] << " ";
}
// ODD
cout << "\nHow many highest numbers do you want to display
for the odd numbers array? ";
cin >> XOdd;
while (XOdd < 1 || XOdd > allOdds.size())
{
cout << "Please enter a value between 1 & " <<
allOdds.size() << "!\n";
cout << "How many highest numbers do you want to display for
the odd numbers array? ";
cin >> XOdd;
}
cout << XOdd << " highest number(s) in the odd array:
";
for (int i = XOdd; i > 0; i--)
{
cout << allOdds[i] << " ";
}
cout << endl;
break;
}
case 6:
{
sort(allEvens);
sort(allOdds);
int XEven, XOdd;
// EVEN
cout << "\nHow many lowest numbers do you want to display for
the even numbers array? ";
cin >> XEven;
while (XEven < 1 || XEven > allEvens.size())
{
cout << "Please enter a value between 1 & " <<
allEvens.size() << "!\n";
cout << "How many lowest numbers do you want to display for
the even numbers array? ";
cin >> XEven;
}
cout << XEven << " lowest number(s) in the even array:
";
for (int i = 0; i < XEven; i++)
{
cout << allEvens[i] << " ";
}
// ODD
cout << "\nHow many lowest numbers do you want to display for
the odd numbers array? ";
cin >> XOdd;
while (XOdd < 1 || XOdd > allOdds.size())
{
cout << "Please enter a value between 1 & " <<
allOdds.size() << "!\n";
cout << "How many lowest numbers do you want to display for
the odd numbers array? ";
cin >> XOdd;
}
cout << XOdd << " lowest number(s) in the odd array:
";
for (int i = 0; i < XOdd; i++)
{
cout << allOdds[i] << " ";
}
cout << endl;
break;
}
case 0:
{
cout << "\nThanks..Goodbye!\n";
exit(EXIT_SUCCESS);
}
default:
cout << "\nInvalid selection!\n";
}
cout << endl;
} while (choice != 0);
return 0;
}
void sort(vector<int>& nums)
{
int n = nums.size();
for (int i = 0; i < n - 1; i++)
{
for (int j = 0; j < n - i - 1; j++)
{
if (nums[j] > nums[j + 1])
{
int temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
}
double getMedian(vector<int> nums)
{
int n = nums.size();
sort(nums);
if (n % 2 == 0)
return (double)nums[n / 2];
return (double)(nums[(n - 1) / 2] + nums[n / 2]) / 2.0;
}
vector<int> readFile(string filename)
{
vector<int> allNums;
ifstream inFile(filename.c_str());
string line;
if (!inFile.is_open())
{
cout << "Error in opening " << filename << "!
Exiting..\n";
exit(EXIT_FAILURE);
}
while (getline(inFile, line))
{
allNums.push_back(stod(line));
}
inFile.close();
return allNums;
}
void displayMenu()
{
cout << "Choose from the following options:\n1. Count of even
numbers, count of odd numbers and sum values in each array\n2.
Average of each array\n3. Median of each array\n" <<
"4. Sort each array in ascending order\n5. Display X numbers of the
highest values in each array\n6. Display X numbers of the lowest
values in each array\n0. Exit\nYour selection >> ";
}