# C++ Array Help

August 13, 2009 at 12:04:17
Specs: Windows Vista
 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.3456.82457.6234672345.65437625.3324365.6722435.23454717689.65187984656.416540.8546-95654.121564.654132112.121546.45000.54960.123984.4562184.456551.5465555.1234666.4567777.65120.00040.1200Can someone please explain to me how to do it?

See More: C++ Array Help

#1
August 13, 2009 at 13:26:33
 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?

Report •

#2
August 13, 2009 at 13:30:40
 Here is my code:#include #include #include 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 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.

Report •

#3
August 13, 2009 at 14:01:42
 Does that code actually compile?

Report •

Related Solutions

#4
August 13, 2009 at 14:07:54
 No, it doesn't. :(

Report •

#5
August 13, 2009 at 14:20:28
 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...

Report •