Computing.Net > Forums > Programming > C++ Array Help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

C++ Array Help

Reply to Message Icon

Name: Puff
Date: August 13, 2009 at 12:04:17 Pacific
OS: Windows Vista
Subcategory: C/C++
Comment:

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.1200

Can someone please explain to me how to do it?



Sponsored Link
Ads by Google

Response Number 1
Name: tommycoolman
Date: August 13, 2009 at 13:26:33 Pacific
Reply:

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?


0

Response Number 2
Name: Puff
Date: August 13, 2009 at 13:30:40 Pacific
Reply:

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.


0

Response Number 3
Name: tommycoolman
Date: August 13, 2009 at 14:01:42 Pacific
Reply:

Does that code actually compile?


0

Response Number 4
Name: Puff
Date: August 13, 2009 at 14:07:54 Pacific
Reply:

No, it doesn't. :(


0

Response Number 5
Name: tommycoolman
Date: August 13, 2009 at 14:20:28 Pacific
Reply:

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...


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: C++ Array Help

C++ array help needed www.computing.net/answers/programming/c-array-help-needed/5479.html

c++ problem ( Help Me! ) www.computing.net/answers/programming/c-problem-help-me-/1364.html

C++ Array Pointer Parameter www.computing.net/answers/programming/c-array-pointer-parameter/6482.html