[In C programming]Create a menu driven application that receives input from a te
ID: 3919050 • Letter: #
Question
[In C programming]Create a menu driven application that receives input from a textfile called “Numbers.txt” and loads the numbers into an array. It will also calculate the sum, save numbers to file, and allow the user to modify a number.
The application should have the following functions:
void ReadNumbers(string filepath, int numbers[], int length); - 10pts Define a function that sequentially reads each number from the textfile and loads them into the array.
void DisplayNumbers(int numbers[], int length); - 10pts Define a function that displays all the numbers stored in the array.
int CalculateSum(int numbers[], int length); - 10pts Define a function that calculates the sum of all numbers in the array and returns the sum.
void ChangeNumbers(int numbers[]); - 10pts Define a function that allows the user to modify one of the numbers in the array. ? Ask the user for the index of the number that needs to be changed ? Ask them for the input. ? Assign the new number to array at the index specified by the user
void WriteNumbers(string filepath, int numbers[], int length); - 10pts Define a function that writes each number from the array into the textfile. Write one number per line.
void DisplayMenu(); - 10pts Display the following text:
int main() should do the following (include comments explaining your code for 10 pts)
1. Declare all variables for sum and choice. – 3pts
2. Declare a named constant for the length of the array with an assigned value of 10 – 3pts
3. Declare variable for the filepath – 0pts
4. Declare an integer array for the numbers using the named constant as the size declarator. – 3pts
5. Call the ReadNumbers function to load the array – 3pts
6. Display the menu – 3pts
7. Get choice from the user -3pts
8. Clear the screen – 0pts
9. Based on the user’s choice, call the appropriate function. -6pts
a. case 1: Call DisplayNumbers function
b. case 2: Call CalculateSum fuction and display the sum
c. case 3: Call Display Numbers and ChangeNumbers functions
d. case 4: Call WriteNumbers
e. case 5: exit(1) 10. pause screen - system(“pause”) – 0pts 11. While choice does not equal 5, repeat steps 6 to 10 – 6pts
A CAwindowsksystem32icmd.exe TEST 3ARRAY PROCESSING TEST 1) Display numbers 2) Display sum 3) Change Number 5) Exit 33 Saye changes Enter choice [1-5]:Explanation / Answer
input file: Numbers.txt
---------------------
3
2
5
6
10
9
8
3
6
1
main.cpp
---------
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
void ReadNumbers(string filepath, int numbers[], int length);
void DisplayNumbers(int numbers[], int length);
int CalculateSum(int numbers[], int length);
void ChangeNumbers(int numbers[]);
void WriteNumbers(string filepath, int numbers[], int length);
void DisplayMenu();
int main(){
int sum, choice;
const int length = 10;
string filepath = "Numbers.txt";
int numbers[length];
ReadNumbers(filepath, numbers, length);
while(choice != 5){
DisplayMenu();
cout << "Enter your choice[1-5]: ";
cin >> choice;
switch(choice){
case 1:
DisplayNumbers(numbers, length);
break;
case 2:
cout << "Sum = " << CalculateSum(numbers, length);
break;
case 3:
ChangeNumbers(numbers);
break;
case 4:
WriteNumbers(filepath, numbers, length);
break;
}
cout << endl << endl;
system("pause");
}
}
void ReadNumbers(string filepath, int numbers[], int length){
ifstream infile(filepath.c_str());
if(infile.fail()){
cout << "ERROR: could not open file " << filepath << endl;
exit(1);
}
for(int i = 0; i < length; i++)
infile >> numbers[i];
infile.close();
}
void DisplayNumbers(int numbers[], int length){
cout << "The numbers in the array are " << endl;
for(int i = 0; i < length; i++)
cout << numbers[i] << endl;
}
int CalculateSum(int numbers[], int length){
int sum = 0;
for(int i = 0; i < length; i++)
sum += numbers[i];
return sum;
}
void ChangeNumbers(int numbers[]){
int index, num;
cout <<"Enter the index of number to be changed: ";
cin >> index;
cout << "Enter the new number: ";
cin >> num;
if(index >= 0 && index < 10)
numbers[index] = num;
}
void WriteNumbers(string filepath, int numbers[], int length){
ofstream outfile(filepath.c_str());
if(outfile.fail()){
cout << "ERROR: could not open file " << filepath << endl;
exit(1);
}
for(int i = 0; i < length; i++)
outfile << numbers[i] << endl;
outfile.close();
cout << "Numbers saved to file " << filepath << endl;
}
void DisplayMenu(){
cout << "-----------------------------" << endl;
cout << "1. Display numbers" << endl;
cout << "2. Display sum" << endl;
cout << "3. Change number " << endl;
cout << "4. Save changes" << endl;
cout << "5. Exit" << endl;
cout << "-----------------------------" << endl;
}
output
-----
-----------------------------
1. Display numbers
2. Display sum
3. Change number
4. Save changes
5. Exit
-----------------------------
Enter your choice[1-5]: 1
The numbers in the array are
3
2
5
6
3
9
8
3
6
1
-----------------------------
1. Display numbers
2. Display sum
3. Change number
4. Save changes
5. Exit
-----------------------------
Enter your choice[1-5]: 2
Sum = 46
-----------------------------
1. Display numbers
2. Display sum
3. Change number
4. Save changes
5. Exit
-----------------------------
Enter your choice[1-5]: 3
Enter the index of number to be changed: 4
Enter the new number: 10
-----------------------------
1. Display numbers
2. Display sum
3. Change number
4. Save changes
5. Exit
-----------------------------
Enter your choice[1-5]: 1
The numbers in the array are
3
2
5
6
10
9
8
3
6
1
-----------------------------
1. Display numbers
2. Display sum
3. Change number
4. Save changes
5. Exit
-----------------------------
Enter your choice[1-5]: 4
Numbers saved to file Numbers.txt
-----------------------------
1. Display numbers
2. Display sum
3. Change number
4. Save changes
5. Exit
-----------------------------
Enter your choice[1-5]: 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.