By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,433 Members | 1,456 Online
Bytes IT Community
+ Ask a Question
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
Share this Question
Share on Google+
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
  1. // this program will read 10 numbers from a file
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     const int size = 10;
  11.  
  12.     int num[size], sum = 0, i, j, n=0, greatest, smallest;
  13.     float avg, total;
  14.     ifstream fin;
  15.  
  16.     fin.open("input.txt", ios::in);
  17.  
  18.     while (!fin.eof()){
  19.           fin >> num [n];
  20.           sum += num [n];
  21.           n++;}
  22.  
  23.     total = 10.0;
  24.  
  25.  
  26.     for ( j = 0; j <= 9; j++){
  27.  
  28.  
  29.         if ( num[0] > num [j])
  30.         greatest = num[0];
  31.  
  32.         else
  33.         greatest = num[j];
  34.  
  35.         if (num[0] < num [j])
  36.         smallest = num [0];
  37.  
  38.         else
  39.         smallest = num [j];}
  40.  
  41.     cout << "Reverse order: \n";
  42.     for ( i = 9; i >= 0; i--){
  43.  
  44.          cout << num[i];}
  45.     avg = sum / total;
  46.  
  47.     cout << "Greatest: " << greatest << endl;
  48.     cout << "Smallest: " << smallest << endl;
  49.     cout << "Sum: " << sum << endl;
  50.     cout << "Average: " << avg << endl;
  51.  
  52.     system ("PAUSE");     
  53.     return 0;
  54. }
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
  1. set smallest to 1001 and largest to -1
  2. for each number: read the number from the file
  3.   if number > largest, set largest to number
  4.   if number < smallest, set smallest to number
  5.  
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
  1. // this program will read 10 numbers from a file
  2.  
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     const int size = 10;
  11.  
  12.     int num[size], sum = 0, i, j, n=0, greatest, smallest;
  13.     float avg, total;
  14.     ifstream fin;
  15.  
  16.     fin.open("input.txt", ios::in);
  17.  
  18.     while (!fin.eof()){
  19.           fin >> num [n];
  20.           sum += num [n];
  21.  
  22.           greatest = num[0];
  23.           smallest = num[0];
  24.  
  25.           if (num[n] > num[0]){
  26.                      greatest = num[n];}
  27.  
  28.           if (num[n] < num[0]){
  29.                      smallest = num[n];}
  30.           n++;
  31.           }
  32.  
  33.     total = 10.0;
  34.  
  35.  
  36.  
  37.     cout << "Reverse order: \n";
  38.     for ( i = 9; i >= 0; i--){
  39.  
  40.          cout << num[i];}
  41.     avg = sum / total;
  42.  
  43.     cout << "\nGreatest: " << greatest << endl;
  44.     cout << "Smallest: " << smallest << endl;
  45.     cout << "Sum: " << sum << endl;
  46.     cout << "Average: " << avg << endl;
  47.  
  48.     system ("PAUSE");     
  49.     return 0;
  50. }
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[0] on every pass through the loop. The purpose of using num[0] 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
  1. for index = 0 to (maximumRange - 1)
  2.   read number from file
  3.   if index is 0, set greatest and smallest to number
  4.   else, compare number to greatest and smallest
  5.  
May 31 '07 #6

sicarie
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

Post your reply

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