Programing lanugaue is C++ Purpose of code: Plan and code a menu-driven modular program utilizing an array An...

80.2K

Verified Solution

Question

Programming

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 beempty too).Read the numbers from the input fileand store evennumbers in one arrayand odd numbers in another array.Create menuoptions to

Display count of even numbers, count of odd numbersand the sumvalues 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 outputresults to a file
Display X number of the highest values(for example, 3 highestvalues) in each array.Ask a user for the number of the highestvalues
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 userwants
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.Justcall it twice to determine the average of each array.The sameapplies to the rest of the menu options.
A user should be able to run the menu as many times as a userwants
Thoroughly test your program.Your grade partially depends on thequality 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 samepurpose but without the Vector function? Is is possible to make asmall edits or will I have to rewrite the whol code? Can someoneshow me how rewrite the code without a vector function?

#include
#include
#include
#include
#include
using namespace std;

void sort(vector& nums);
double getMedian(vector nums);
vector readFile(string filename);
void displayMenu();

int main()
{
string filename = \"input.txt\";
vector allNums = readFile(filename);
vector 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 = \" <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 = \" <break;
}
case 3:
{
cout << \"\nMedian of all the even numbers = \" <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 \" <break;
}
case 5:
{
sort(allEvens);
sort(allOdds);
int XEven, XOdd;
// EVEN
cout << \"\nHow many highest numbers do you want to displayfor the even numbers array? \";
cin >> XEven;
while (XEven < 1 || XEven > allEvens.size())
{
cout << \"Please enter a value between 1 & \" <cout << \"How many highest numbers do you want to display forthe 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 displayfor the odd numbers array? \";
cin >> XOdd;
while (XOdd < 1 || XOdd > allOdds.size())
{
cout << \"Please enter a value between 1 & \" <cout << \"How many highest numbers do you want to display forthe 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 forthe even numbers array? \";
cin >> XEven;
while (XEven < 1 || XEven > allEvens.size())
{
cout << \"Please enter a value between 1 & \" <cout << \"How many lowest numbers do you want to display forthe 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 forthe odd numbers array? \";
cin >> XOdd;
while (XOdd < 1 || XOdd > allOdds.size())
{
cout << \"Please enter a value between 1 & \" <cout << \"How many lowest numbers do you want to display forthe 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& 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 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 readFile(string filename)
{
vector 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 evennumbers, 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 thehighest values in each array\n6. Display X numbers of the lowestvalues in each array\n0. Exit\nYour selection >> \";
}

Answer & Explanation Solved by verified expert
3.7 Ratings (438 Votes)
include include include include include include using namespace std using stdifstream using stdcerr using    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students