473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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 5315
scruggsy
147 100+
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
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
scruggsy
147 100+
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
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
scruggsy
147 100+
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
4,677 Expert Mod 4TB
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

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

Similar topics

3
by: deko | last post by:
It's nice to be able to generate an html table from a PHP array. I know how to do this, but the array in question is built from a file. The file in question can be very long, and I only want the...
2
by: Pete | last post by:
Hi Could someone kindly help with the C# equivilent of the following 4 lines of C code. I'm *really* struggling with this. ( Colours.dat contains 300 RGB values ) COLORREF Colours;
2
by: oyekomova | last post by:
I would like to know how to read a CSV file with a header ( n columns of float data) into an array without the header row.
3
by: Rupali12345 | last post by:
hi all I m trying to read an .dcm file using byte array.But i dont know actually how to read byte code from file.please give me any example of reading byte code. I search in google also...but in...
22
by: bela | last post by:
Hello, I am very new to JAVA. I would like to know, how to read *.csv file from java and how to save that data into an array. regards, bela
6
by: Question123 | last post by:
Hi friends How to read StreamReader in reverse direction?? My code is as below Dim client As System.Net.WebClient = New System.Net.WebClient Dim data As Stream =...
8
by: Question123 | last post by:
Hi Friends How to read excel worksheet on server in reverse direction??
0
by: jozule | last post by:
String arrayA = {"and","ant", "aeroplane", "apple", "augustine"}; System.out.println(""); i have a text file called "dictionaryA.txt", which i want to read into this array instead, how can i do...
0
by: Emily M | last post by:
As part of an assignment I'm supposed to declare an array and read numbers from a file but I'm stumped. I have no idea on how to do this! the file is called numbers.txt please help :) this is...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.