Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I cannot figure out how to do this C++ homework assignment where I am supposed to import a file and sort it into an array which will output the largest, smallest, and average of the numbers in the file. I have absolutely no idea how to do this, as the textbook gives no clue on how to do this. Here is the file with the numbers I have to import:
2.345
6.8245
7.623467
2345.6543
7625.33
24365.672
2435.23454
717689.651
87984
656.41654
0.8546
-95654.121
564.6541321
12.12
1546.4500
0.5496
0.123
984.456
2184.456
551.5465
555.1234
666.4567
777.6512
0.0004
0.1200Can someone please explain to me how to do it?

How far have you gotten? From what I know of programming texts, they wouldn't give you a problem like this without leading up to it in previous sections.
Are you having a problem opening the file, finding the smallest/largest/average, etc?

Here is my code:
#include <iostream>
#include <fstream>
#include <stdlib.h>
using namespace std;int main()
{
ifstream in_stream;
in_stream.open("doubles.txt");
double largestNum, smallestNum;
double values[26];
in_stream >> values[26];cout << "The largest number in the file is: ";
cout << max;
cout << endl;
double maximumValue()
{
double length = values.length( );
double max = values[0];for(int x = 1; x<length; x++)
{
if(values[x] > max)
max = values{x};
}
return max;
}cout << "The average of the numbers is: ";
cout << (values[0] + values[1] + values[2] + values[3] + values[4]
+ values[5] + values[6] + values[7] + values[8] + values[9]
+ values[10] + values[11] + values[12] + values[13]
+ values[14] + values[15] + values[16] + values[17]
+ values[18] + values[19] + values[20] + values[21]
+ values[22] + values[23] + values[24] + values[25])/25;
cout << endl;
system("pause");
return 0;
}I am having trouble finding the maximum and minimum.

I think you are over thinking the problem. You do not need a separate function to find minimum and maximum values. You could just use a loop and "if".
Besides, you put the function maximumValue() inside of the main function, when it should have been outside.
double values[26] should really be double values[25] since there are 25 numbers. Only with character arrays do you need +1 to store the "0" to end the string.
in_stream >> values[26] is not the way to read "doubles.txt" into your values[]. The simplest way to get the numbers out of the file is to open the file, use .eof and getline, then atof to convert the character arrays to numbers.
Look at this code.
http://www.tommycoolman.com/misc/pu...

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |