Hey guys hipe you all doing good, I have a program that reads some numbers in a file, it needs to find the highest number, the lowst number, sum of numbers, and teh avferasge. I have coded the follwoing but it is not having the best reslutls. It finds the highest, average and sum but for lowest alwyas shows the first number in the file
let me know if yall can do anything about this
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int main ()
{
{
ifstream inputFile;
char fileName[20];
cout << "Enter the filename ";
inputFile.open("numbers.txt");
cout<<"\n";
const int SIZE=100;
int numbers [SIZE];
int count =0;
while (count<SIZE && inputFile>>numbers[count])
count++;
cout<<setprecision(2)<<fixed<<showpoint<<endl;
cout<<"\nThese are all of the numbers in the file: ";
for(int index=0;index<count;index++)
cout<<numbers[index]<<" "<<endl;
inputFile.close();
}
int count_highest=0;
int greatest=0;
const int SIZE=100;
int numbers [SIZE];
greatest=numbers[0];
ifstream inputFile;
inputFile.open("numbers.txt");
while (count_highest<SIZE && inputFile>>numbers[SIZE])
{
count_highest++;
if(numbers[count_highest]>greatest)
greatest=numbers[count_highest];
}
inputFile.close();
cout<<"\nThe highest number is: "<<greatest<<endl;
inputFile.open("numbers.txt");
int count_lowest=0;
int lowest;
lowest = numbers[0];
while (count_lowest<SIZE && inputFile>>numbers[SIZE])
{
count_lowest++;
if (numbers[count_lowest] < lowest)
lowest = numbers[count_lowest];
}
cout<<"The lowest numbers is: "<<lowest<<endl;
int tnum;
double average, sum = 0;
for(tnum = 0; tnum < SIZE; tnum++)
sum += numbers[tnum];
average = sum / SIZE;
cout<<"The sum of numbers is "<<sum<<" and the average " <<average<<endl;
cout<<"There are total "<<tnum<<" numbers in the file."<<endl;
system("pause");
return 0;
}
Comment