I have an assignment where I am supposed to get the values from a file and output them in ascending order. The problem is, I have no idea how to do this. Can someone please explain?
The values are:
Code: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
C or C++? Usually sorting can be accomplished with a simple function call to one of the standard libraries.
Wow I changed my sig!
Assuming C++, you'd want to use the ifstream class. For information on ifstream follow this link. You'll want to use the extraction operator with it, for information on using the extraction operator, follow this link.
Assuming C, you'll want to use fopen (information here) with fgets (information here). If your using fgets, the numbers will be returned as strings. To convert the strings to floating point values (numbers with a decimal point), use atof() (information here).
Hope this helps
There are several parts to this problem:
1) read the file into the array (or better yet, vector)
2) sort the array/vector
3) output the contents of the array/vector.
Which part are you having difficulty with? What code do you have so far?
i knew binary sort will do the needful, or even the easiest qsort() will do it.... but
can anyone tell me, how can we define the array size if we don't know no of elements in the file .....
You could scan through the file twice: once to get count the number of elements required, and a second to read it after you use new to acquire the space for the array.
If you don't know the number of items that should exist, I would use a vector instead. You could scan the file twice but what if you have a lot of items in the file? This is going to take a god awful long time.
Also, there is no binary sort. You are thinking "binary search". There is a method in the standard libraries called qsort that you can use. Otherwise you can write your own sorting algorithm.
I.e. a selection sort
The STL Sort algorithm is a bit cleaner when using vectors. And yes, when using vectors, it is actually easier to code resizing, but it's not without it's sacrifice of resources. It would be preferable to read the file in an intermittent manner, adding each number to a string object while you're loading it. That way, you can filter out the non-numbers from the string before processing, and as you're getting the numbers from the file, you keep count of how many numbers you get. Then create a Vector, reserve the amount of numbers counted, and apply them into the vector, then use the Sort algorithm and output it on an ofstream.![]()
Wow I changed my sig!
I am doing this in C++. Here is my code so far, I can't figure out how to output the array in ascending order though.
Code:#include <iostream> #include <fstream> #include <stdlib.h> using namespace std; int main() { double largestNum, smallestNum; double values[25]; double sum = 0; char buffer[25][256]; int count = 0; ifstream in_stream; in_stream.open("doubles.txt"); while(!in_stream.eof()) { in_stream.getline(buffer[count],256,'\n'); values[count] = atof(buffer[count]); sum += values[count]; count++; } cout << "The values are: " << 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] << "\n" << endl; cout << "The values from least to greatest are: \n"; cout << system("pause"); return 0; }
You'll want to use qsort, information on using it can be found here.
And you have apparently not been taught about for loops yet, which was a serious error by your teacher. Please read all the text concerning the for loop located here. It is necessary information.
Once you learn how to use the for loop you should be able to condense the codeinto one or two lines of code. Your teacher will want you to use a for loop for this and will probably be unhappy if you give her your assignment with the current block of code.Code:cout << "The values are: " << 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] << "\n" << endl;
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks