By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,836 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

Arrays

P: 6
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <iomanip>
  4. using namespace std;
  5. int main ()
  6. {
  7.     {
  8.          ifstream inputFile;
  9.          char fileName[20];
  10.          cout << "Enter the filename  ";
  11.          inputFile.open("numbers.txt");
  12.          cout<<"\n";
  13.          const int SIZE=100;
  14.          int numbers [SIZE];
  15.          int count =0;
  16.          while (count<SIZE && inputFile>>numbers[count])
  17.          count++;
  18.          cout<<setprecision(2)<<fixed<<showpoint<<endl;
  19.          cout<<"\nThese are all of the numbers in the file: ";
  20.          for(int index=0;index<count;index++)
  21.          cout<<numbers[index]<<" "<<endl;
  22.          inputFile.close();
  23.     }
  24.     int count_highest=0;
  25.     int greatest=0;
  26.     const int SIZE=100;
  27.     int numbers [SIZE];
  28.     greatest=numbers[0];
  29.     ifstream inputFile;
  30.     inputFile.open("numbers.txt");
  31.     while (count_highest<SIZE && inputFile>>numbers[SIZE])
  32.  
  33.     {
  34.                  count_highest++;            
  35.                  if(numbers[count_highest]>greatest)
  36.                  greatest=numbers[count_highest];
  37.     }
  38.     inputFile.close();
  39.     cout<<"\nThe highest number is: "<<greatest<<endl; 
  40.     inputFile.open("numbers.txt"); 
  41.     int count_lowest=0;
  42.     int lowest;
  43.     lowest = numbers[0];
  44.     while (count_lowest<SIZE && inputFile>>numbers[SIZE])
  45.     {
  46.          count_lowest++;
  47.          if (numbers[count_lowest] < lowest)
  48.          lowest = numbers[count_lowest];     
  49.     }
  50.     cout<<"The lowest numbers is: "<<lowest<<endl;
  51.     int tnum;
  52.     double average, sum = 0;
  53.     for(tnum = 0; tnum < SIZE; tnum++)
  54.             sum += numbers[tnum];
  55.     average = sum / SIZE;
  56.  
  57.     cout<<"The sum of numbers is "<<sum<<" and the average " <<average<<endl;    
  58.     cout<<"There are total "<<tnum<<" numbers in the file."<<endl;
  59.     system("pause");
  60.     return 0;
  61. }
  62.  
let me know if yall can do anything about this
Mar 11 '07 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,510
your loop looking for greatest exits when the inputFile>>numbers[SIZE] fails, i.e. you get an error condition. If you are going to use the same ifstream again you need to clear the error condition
Expand|Select|Wrap|Line Numbers
  1.     inputFile.close();
  2.     inputFile.clear();
  3.     inputFile.open("numbers.txt"); 
  4.  
the open() does not automatically do so.
I also think your algorithm for finding greatest and lowest is not correct but you can check this with test data
Mar 11 '07 #2

P: 6
une
the following dont have any effect in the loop, thats just where the loop takes numbers to compare
Expand|Select|Wrap|Line Numbers
  1. inputFile>>numbers[count] 
Mar 11 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.