I could use some help with this problem Page 445 in starting out with c++ 7th ed
ID: 3656517 • Letter: I
Question
I could use some help with this problem Page 445 in starting out with c++ 7th edition. Thanks in advance. Number Analysis Program 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 06 folder. You can use the le to test the program. (The companion Web site is at www.pearsonhighered.com/gaddis.)Explanation / Answer
please rate - thanks
#include <iostream>
#include <fstream>
using namespace std;
int getNumbers(ifstream&,int[]);
int getSmall(int[],int);
int getBig(int[],int);
int getTot(int[],int);
int main()
{int max=20,total;
int numbers[max];
char filename[30];
cout<<"what is the name of the file you are using? ";
cin>>filename;
ifstream input;
input.open(filename); //open file
if(input.fail()) //is it ok?
{ cout<<"file did not open please check it ";
system("pause");
return 1;
}
max=getNumbers(input, numbers);
total= getTot(numbers,max);
cout << "The biggest number is "<<getBig(numbers,max)<<endl;
cout << "The smallest is " <<getSmall(numbers,max)<<endl;
cout << "The total of the numbers is "<<total<< endl;
cout << "The average of the numbers is "<<total/(double)max<<endl;
input.close();
system("pause");
return 0;
}
int getNumbers(ifstream &in, int numbers[])
{int i=0;
in>>numbers[i];
while(in)
{i++;
in>>numbers[i];
}
return i;
}
int getSmall(int array[],int max)
{int small,i;
small=array[0];
for(i=1;i<max;i++)
{if(array[i]<small)
small=array[i];
}
return small;
}
int getBig(int array[],int max)
{int big,i;
big=array[0];
for(i=1;i<max;i++)
{if(array[i]>big)
big=array[i];
}
return big;
}
int getTot(int array[],int max)
{int tot=0,i;
for(i=0;i<max;i++)
tot+=array[i];
return tot;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.