Question

In: Computer Science

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

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 >> ";
}

Solutions

Expert Solution

#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 >> ";
}


Related Solutions

C++ Plan and code a program utilizing one or more repetition structures to solve the following...
C++ Plan and code a program utilizing one or more repetition structures to solve the following problem: Develop a program to analyze one or more numbers entered by a user. The user may enter one or more numbers for analysis. A number is a multiple of 9 if the sum of its digits is evenly divisible by 9. Determine if a number is a multiple of 9 by adding together the individual digits of the number and determining if the...
Write a menu driven C++ program that prints the day number of the year , given...
Write a menu driven C++ program that prints the day number of the year , given the date in the form of month-day-year. For example , if the input is 1-1-2006 , then the day number is 1. If the input is 12-25- 2006 , the day number is 359. The program should check for a leap year. A year is leap if it is divisible by 4 but not divisible by 100. For example , 1992 , and 2008...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish...
In C++ Language English/Spanish Translation Program. Create a menu driven program that translates English to Spanish and Spanish to English. Your translation program should use arrays for this program. You will need to populate the arrays with the contents of the English and Spanish data files provided. The two files are ordered such that each word in the one file corresponds to the respective translation in the other (i.e.: the first word in the ENG.txt file corresponds to the first...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of...
C++ Programming Develop and submit an original implementation of a menu-driven program performing a number of tasks relating to student marks scored in a number of assessments including: displaying all marks, adding new student marks, calculating the mean score, median score, finding the minimum and maximum scores as well as displaying the average mark of a given student. The problem: Student marks are kept in a text file as a single column. Each student may have a different number of...
C++ ^ ^ Write a menu driven program to perform following operations using a map container...
C++ ^ ^ Write a menu driven program to perform following operations using a map container that stores the information about USA Population (In Million). @@@Menu@@@ 1. Add Information 2. Display Information 3. Update Information 4. Erase Information 5. Clear Information For example, Suppose the map initially contains following information (Use emplace() function to store the information) 2010, 309.33 2011, 311.58 2012, 313.87 2015, 320.74 2016, 323.07 The program should produce the desired output when a valid input is provided,...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End...
Write a menu-driven C++ program with two options: 1) Is it odd or even? 2) End Program.The user can only input a positive integer (0 does not count) and if the input is not a positive integer the program must state "Invalid Input." The program must determine whether the input is even or odd. The program must use functions and can only use the iostream and iomanip libraries. Note: The program must loop until the 2nd menu option is chosen.
No Global variables No goto statement No break outside switch Write a menu driven C program...
No Global variables No goto statement No break outside switch Write a menu driven C program using functions and switch. Feel free to use “Empty Outlines” template from Canvas to design the functions as needed to build the code. Make sure to submit your work through Canvas. You can show me your code running in class when you are done. The program shows following menu to the user repetitively until user selects option 3 to exit. Circle Triangle Exit Based...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program...
This is an exercise for a menu-driven program. Program should use shell functions. Write a program that displays the following menu: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a rectangle 3. Calculate the area of a triangle 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for the radius of the circle and then display the area. Use the following formula to calculate the circle’s area: ?...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First...
IN C++ ONLY... please do both parts of this task ..1 part- Menu driven program First load an array(lists) with the values 3, 4, 84, 5, 2, 47, and 7 in this order by using a function. Then sort the array(list) Use a function to display the menu. Create a program with a menu, with choices 1 for factorial (single value, which everyone will use 20!), 2 for median value , 3 for average, 4 for maximum, 5 for minimum,...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program...
Write a menu-driven program to test the three functions conver_tlength(), convert_width(), convert_volume() in a program. Program should allow the user to select one of the options according to whether lengths, weights or volume are to be converted, read the volue to be converted and the units, and then call the appropriate function to carry out the conversion In unit out unit I C (inch to centimeter 1 in = 2.4 cm) F C (feet to centimeter 1 ft = 30.4...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT