C++ Please Write a program that asks the user for a file name. Assume the file c
ID: 3691036 • Letter: C
Question
C++ Please
Write a program that asks the user for a file name. Assume the file contains a series of
numbers, each written on a separate line. The program should read the contents of the
file into an array and then display the following data:
• The lowest number in the array
• The highest number in the array
• The total of the numbers in the array
• The average of the numbers in the array
If you have downloaded this book’s source code from the companion Web site, you
will find a file named numbers.txt in the Chapter 07 folder. You can use the file to
test the program. (The companion Web site is at www.pearsonhighered.com/gaddis .)
Explanation / Answer
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
int main()
{
int a[100];
int i=0,j=0, sum=0,av,min,max,odd=0,ev=0;
ifstream in("numbers.txt");
while(!in.eof())
{
in>>a[i];
sum=sum+a[i];
i++;
}
av=sum/i;
min=a[0];
j=0;
while(j<=i)
{
if(a[j]<min)
min=a[j];
j++;
}
//i=0;
max=a[0];
j=0;
while(j<=i)
{
if(a[j]>max)
max=a[j];
j++;
}
cout<<"Sum is"<<sum<<endl;
cout<<"Average is"<<av<<endl;
cout<<"Highest value is"<<max<<endl;
cout<<"Minimum number"<<min<<endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.