using C++,Write a program that asks the user for a file name, and then ask the u
ID: 3764490 • Letter: U
Question
using C++,Write a program that asks the user for a file name, and then ask the user for up to 100 input values. Write the user input values to the file. Then read the contents of the file back into an array, and then display the following data: lowest number highest number total of the numbers average of the numbers MODIFICATIONS: Write the array back to a NEW file, in reverse order. Call this file reverse.txt. Provide a "sentinel value" to allow the user to stop entering numbers. (if i only want 5 numbers, i should only have to enter 5 numbers, but if i want to enter 100, then i should be able to enter 100.) Once you have the program working, create functions that calculate each of the items above. Your function should take an array and the size of the array as input, and return the value calculated.
Explanation / Answer
#include <bits/stdc++.h>
using namespace std;
int smallestnumber(int arr[],int count){
int smlnum=INT_MAX;
for(int j=0;j<count;j++){
if(smlnum>arr[j]){
smlnum=arr[j];
}
}
return smlnum;
}
int highestnumber(int arr[],int count){
int maxnum=INT_MIN;
for(int j=0;j<count;j++){
if(maxnum<arr[j]){
maxnum=arr[j];
}
}
return maxnum;
}
int total(int arr[],int count){
int sum=0;
for(int j=0;j<count;j++){
sum+=arr[j];
}
return sum;
}
int main(){
string infile;
ofstream inf;
cout<<"Enter File name:";
cin>>infile;
int l=infile.length();
char c[infile.length()];
for(int i=0;i<l;i++){
c[i]=infile[i];
}
inf.open(c);
int count=0;
int sent_val=-1;
int inp;
cout<<"Enter a number:";
while(count<100){
cin>>inp;
if(inp==sent_val){
break;
}
else{
inf<<inp<<endl;
count++;
if(count<100){
cout<<"Enter another number:";
}
else{
cout<<"You have reached limit 100"<<endl;
}
}
}
inf.close();
int arr[count];
ifstream inf1;
inf1.open(c);
int n;
int i=0;
while(i<count){
inf1>>n;
arr[i]=n;
i++;
}
cout<<"smallest number = "<<smallestnumber(arr,count)<<endl;
cout<<"higest number ="<<highestnumber(arr,count)<<endl;
cout<<"total of numbers = "<<total(arr,count)<<endl;
cout<<"average of numbers = "<<total(arr,count)<<"/"<<count<<endl;
ofstream inf2;
inf2.open("reverse.txt");
for(int j=0;j<count;j++){
inf2<<arr[(count-j)-1]<<endl;
}
inf2.close();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.