Arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • une
    New Member
    • Jan 2007
    • 6

    #1

    Arrays

    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

    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;
    }
    let me know if yall can do anything about this
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    your loop looking for greatest exits when the inputFile>>numb ers[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
    Code:
        inputFile.close();
        inputFile.clear();
        inputFile.open("numbers.txt");
    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

    Comment

    • une
      New Member
      • Jan 2007
      • 6

      #3
      the following dont have any effect in the loop, thats just where the loop takes numbers to compare
      Code:
      inputFile>>numbers[count]

      Comment

      Working...