Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a c++ program that: 1. Has a main() 2. includes the proper header and nam

ID: 3896957 • Letter: C

Question

Create a c++ program that: 1. Has a main()

2. includes the proper header and namespace declarations

3.Create a program using the following requirements: a. Write a program that requests the following from the user: 1. Enter file name and calculate average of values 2. Exit the program Place this inside a while loop that requests the user's choice from a menu until the user selects 2 . If the user selects 2, exit the loop and the program. b. If the user selects 1 write a looping code segment that reads as input UP TO 20 integer scores from a file (see below) and calculates and outputs (displays) their average. 1. If the file contains fewer than 20 scores, the program should STILL output the correct average. 2. If the file contains more than 20 scores, the additional numbers should be ignored. The average of 20 scores should STILL be calculated and output. 3. If the file is empty, the appropriate action should be taken and output a message. c. Create the appropriate variables for summing and averaging values, and the file name. d. Request the input file name from the user, input the filename, open the file and test for file state. If the file does not open successfully, output the appropriate message and exit the program. e. Download and use each of the following input files to test your program: 1. has-20.txt. Contains exactly 20 integers to test a above. 2. has-14.txt. Contains only 14 integers to test a1 above. 3. has-25.txt. Contains 25 integers to test a2 above. Also try this with a file that has no data. f. Close the file after the calculations and before requesting input from the user in the menu g. Include any displays to pause the program.

has txt14.exe =

has txt20.exe =

has txt25.exe=

Explanation / Answer

PROGRAM

#include<iostream>

#include<fstream>

#include<cstdlib> // exit() function

using namespace std;

// Create and implement menu() function and return choice

int menu()

{

int ch;

cout<<" 1. Enter File Name and Calculate Average of Values";

cout<<" 2. Exit";

cout<<" Enter Choice: ";

cin>>ch; // read choice ch

return ch; // return choice

}

int main()

{

char fname[20]; // declare array of characters fname

int arr[50],num; // declare interger array arr and variable num

while(1) // create infinity while loop

{

X: int ch=menu(); // calling menu() with label X

switch(ch) // switch stament for check choice(ch)

{

case 1:{ ifstream in; // declare input file stream "in" Object

cout<<" Enter File name: ";

cin>>fname; // read filename "fname"

in.open(fname); // open file in read mode

if(!in) // check file is not available then, print message and exit from program

{

cout<<" File Not Found..";

exit(0);

}

if(in.peek()==EOF){ // check condition if the file contains empty

cout<<" File is Empty..Try Again ";

goto X; // then go to menu() label

}

  

// initialize 0 of integer variables

int i=0, count=0;

while(in>>num) // reading values(num) from file

{

arr[i++]=num; // store into array(arr)

count++; // counting number of elements

if(count==20) break; // check condition if counter =20 then exit from while loop

}

int sum=0; // initialize integer sum=0

if(count<=20) // check condition whether counter<=20 then

{

for(int j=0;j<count;j++) // create for loop until count

{

sum+=arr[j]; // calculate sum of elements

}

}

cout<<endl<<"Sum of Elements: "<<sum<<endl; // display sum of elements

double avg=(double)sum/count; // calculate average

cout<<" Average of Elements: "<<avg<<endl; // display average

break;

}

case 2: exit(0); break; // exit from the program

default: cout<<" You Enter Wrong choice..Try Again"<<endl; // invalid choice

}

}

return 0;

}

OUTPUT


1. Enter File Name and Calculate Average of Values
2. Exit
Enter Choice: 3

You Enter Wrong choice..Try Again

1. Enter File Name and Calculate Average of Values
2. Exit
Enter Choice: 1

Enter File name: f:\has-14.txt

Sum of Elements: 559

Average of Elements: 39.9286

1. Enter File Name and Calculate Average of Values
2. Exit
Enter Choice: 1

Enter File name: f:\has-20.txt

Sum of Elements: 714

Average of Elements: 35.7

1. Enter File Name and Calculate Average of Values
2. Exit
Enter Choice: 1

Enter File name: f:\has-25.txt

Sum of Elements: 714

Average of Elements: 35.7

1. Enter File Name and Calculate Average of Values
2. Exit
Enter Choice: 1

Enter File name: f:\empty.txt

File is Empty..Try Again

1. Enter File Name and Calculate Average of Values
2. Exit
Enter Choice: 2

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote