Question

In: Computer Science

c++ In this program ask the user what name they prefer for their file, and make...

c++

In this program ask the user what name they prefer for their file, and make a file, the file name should za file.

Get a number from the user and save it into the create file. should be more than 20 number in any order.

print out all 20 numbers in a sorted array by using for loop, last print out its total and average.

At last create a function in which you will call it from main and swap all the numbers in that function, only one function.

Solutions

Expert Solution

/* Below is your required code*/

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void swap(int *x, int *y)
{
int temp = *x;
*x = *y;
*y = temp;
}
int main()
{ string name;
cout<<"Enter file name to be created: ";
cin>>name;
fstream file;
file.open(name,ios::out);

if(!file)
{
   cout<<"Error!";
   return 0;
}
else{
int x[25];
int n=25;
cout<<"Enter array.";
for(int count = 0; count < n; count ++){
cin>>x[count];
file << x[count] << " " ;
}
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (x[j] > x[j+1])
swap(&x[j], &x[j+1]);
cout<<"Sorted Array!\n";
float total,avg;
for (i = 0; i < n; i++){
total+=x[i];
cout<<x[i]<<" ";
}
cout<<"\n Total = "<<total;
avg=total/n;
cout<<"\n Average = "<<avg;
  
file.close();
}
return 0;
}


Related Solutions

Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint:...
Write a program that prompts the user for a file name, make sure the file exists...
Write a program that prompts the user for a file name, make sure the file exists and then reads through the file, counts the number of times each word appears and outputs the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try again (hint: use the try/except...
Tail of a File, C++ Program. write a program that asks the user for the name...
Tail of a File, C++ Program. write a program that asks the user for the name of a text file. The program should display the last 10 lines, or all lines if less than 10. The program should do this using seekg Here is what I have so far. #include<iostream> #include<fstream> #include<string> using namespace std; class File { private:    fstream file;    string name; public:    int countlines();    void printlines(); }; int File::countlines() {    int total =...
In Python write a program that prompts the user for a file name, make sure the...
In Python write a program that prompts the user for a file name, make sure the file exists and if it does reads through the file, count the number of times each word appears and then output the word count in a sorted order from high to low. The program should: Display a message stating its goal Prompt the user to enter a file name Check that the file can be opened and if not ask the user to try...
Write a program using C language that -ask the user to enter their name or any...
Write a program using C language that -ask the user to enter their name or any other string (must be able to handle multiple word strings) - capture the epoch time in seconds and the corresponding nanoseconds - ask the user to type in again what they entered previously - capture the epoch time in seconds and the corresponding nanoseconds -perform the appropriate mathematical calculations to see how long it took in seconds and nanoseconds (should show to 9 decimal...
Script 3: Ask the user for a file's name If the file exists, ask them if...
Script 3: Ask the user for a file's name If the file exists, ask them if they would like to (C)opy, (M)ove, or (D)elete it by choosing C, M, or D If the user chooses C, ask for the destination directory and move it there If the user chooses M, ask for the destination directory and move it there If the user chooses D, delete the file. Ensure that the user enters only C, M, or D, warning them about...
File IO Java question • Ask the user to specify the file name to open for...
File IO Java question • Ask the user to specify the file name to open for reading • Get the number of data M (M<= N) he wants to read from file • Read M numbers from the file and store them in an array • Compute the average and display the numbers and average.
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
Ask the user for their name Create a file called "daily.dat" within the home directory Ask...
Ask the user for their name Create a file called "daily.dat" within the home directory Ask the user for a line of text that the system should save to the file Add to the file the line of text entered by the user Ask the user for a second line of text that the system should save to the file Append at the end of the file the line of text entered by the user Create a copy of the...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT