454,433 Members | 1,456 Online Need help? Post your question and get tips & solutions from a community of 454,433 IT Pros & Developers. It's quick & easy.

# Read ints from file, reverse the order in the array.

 P: 3 alright this is my problem.....please help i need to write a program that reads 10 integers from a file into an array and then prints out the numbers in reverse order. It also prints out the greatest number, smallest number, and the average May 31 '07 #1
6 Replies

 100+ P: 147 alright this is my problem.....please help i need to write a program that reads 10 integers from a file into an array and then prints out the numbers in reverse order. It also prints out the greatest number, smallest number, and the average What part do you need help with? -Reading from a file? -Searching an array? -Reversing an array? Post the code for the parts you've written and somebody can help you figure out the rest. May 31 '07 #2

 P: 3 this is my code so far.....everything works except the part that is suppose to identify the smallest and greatest number Expand|Select|Wrap|Line Numbers // this program will read 10 numbers from a file   #include  #include    using namespace std;   int main() {     const int size = 10;       int num[size], sum = 0, i, j, n=0, greatest, smallest;     float avg, total;     ifstream fin;       fin.open("input.txt", ios::in);       while (!fin.eof()){           fin >> num [n];           sum += num [n];           n++;}       total = 10.0;         for ( j = 0; j <= 9; j++){             if ( num > num [j])         greatest = num;           else         greatest = num[j];           if (num < num [j])         smallest = num ;           else         smallest = num [j];}       cout << "Reverse order: \n";     for ( i = 9; i >= 0; i--){            cout << num[i];}     avg = sum / total;       cout << "Greatest: " << greatest << endl;     cout << "Smallest: " << smallest << endl;     cout << "Sum: " << sum << endl;     cout << "Average: " << avg << endl;       system ("PAUSE");          return 0; } May 31 '07 #3

 100+ P: 147 Okay. Since you have to read the numbers in one at a time, wouldn't it be easier just to find the smallest and largest numbers while you're reading them in? If the numbers are guaranteed to be within a certain range, such as greater than 0 and less than 1000, you can use something like: Expand|Select|Wrap|Line Numbers set smallest to 1001 and largest to -1 for each number: read the number from the file   if number > largest, set largest to number   if number < smallest, set smallest to number   If you don't know the range, you can simply set smallest and largest to the first number read, then compare subsequent input to those values. That way you save yourself the trouble of writing a second loop to compare the value of each number against all the others. May 31 '07 #4

 P: 3 here is my new code......however it still does not give me the correct numbers for greatest and smallest. for some reason the greatest happens to be the first number in my array and smallest is the last number. Expand|Select|Wrap|Line Numbers // this program will read 10 numbers from a file   #include  #include    using namespace std;   int main() {     const int size = 10;       int num[size], sum = 0, i, j, n=0, greatest, smallest;     float avg, total;     ifstream fin;       fin.open("input.txt", ios::in);       while (!fin.eof()){           fin >> num [n];           sum += num [n];             greatest = num;           smallest = num;             if (num[n] > num){                      greatest = num[n];}             if (num[n] < num){                      smallest = num[n];}           n++;           }       total = 10.0;           cout << "Reverse order: \n";     for ( i = 9; i >= 0; i--){            cout << num[i];}     avg = sum / total;       cout << "\nGreatest: " << greatest << endl;     cout << "Smallest: " << smallest << endl;     cout << "Sum: " << sum << endl;     cout << "Average: " << avg << endl;       system ("PAUSE");          return 0; } May 31 '07 #5

 100+ P: 147 here is my new code......however it still does not give me the correct numbers for greatest and smallest. for some reason the greatest happens to be the first number in my array and smallest is the last number. Right, because you're setting smallest and greatest to num on every pass through the loop. The purpose of using num as a seed value is so you'll have something to compare the rest of the numbers against, but if a greater or lesser number is later found, that's the number you want to remember and check the others against. Try rewriting your loop so that it only seeds greatest and smallest the first time through, along the lines of: Expand|Select|Wrap|Line Numbers for index = 0 to (maximumRange - 1)   read number from file   if index is 0, set greatest and smallest to number   else, compare number to greatest and smallest   May 31 '07 #6

 Expert Mod 2.5K+ P: 4,677 Hey everyone, I changed the title of the thread to hopefully better describe the issue and get more people looking at it/helping out. Let me know if you think it should be something else. May 31 '07 #7 