Jump to content

Simple Arrays Sorting Help?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
17 replies to this topic

#1
Puff

Puff

    Newbie

  • Members
  • Pip
  • 3 posts
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:
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



#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
C or C++? Usually sorting can be accomplished with a simple function call to one of the standard libraries.
Wow I changed my sig!

#3
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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?
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
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 .....

#6
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
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.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#7
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts

veda87 said:

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

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

#8
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
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!

#9
Puff

Puff

    Newbie

  • Members
  • Pip
  • 3 posts
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.

#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;
}


#10
brownhead

brownhead

    Programmer

  • Members
  • PipPipPipPip
  • 173 posts
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 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;
into 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.

#11
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts
I wouldn't use qsort. qsort reeks with C, and you want to do it C++ style, right? :)

Alright, first I'd change that cout group with the values array into a loop, that way you can put any amount of numbers into the loop and the program will still cout them.

    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;
to
    cout << "The values are: " << values[0];
    for( int num = 1; num < count; ++num) { // I used count since you already
                                            // seem to be using that variable
                                            // for counting the array size.
        cout << ", " << values[num];
    }
    cout << endl << endl;
Better yet, put the above code into a function, since you'll be using it twice.
void displayValues(std::vector<double>& vals)
{
    using namespace std;
    cout << vals[0];
    for (int iii = 1; iii < vals.size(); ++iii) {
        cout << ", " << vals[iii];
    }
    cout << endl << endl;
}
Then input the vector (see below) as the parameter, EG "displayValues(values);". Then you can use this for both your unsorted and your sorted display.

Second, in my opinion it's bad practice and leads to non-portable code to use system("pause"), not just that it's resource heavy. Use cin.get() instead.

Also, in the while loop, "buffer" doesn't need to be a 2-dimensional array, it only needs to be "buffer[256];".

Finally, the problem is you don't have any sort algorithm to work with here. I'd convert your values array into a std::vector, by replacing "double values[25];" with "vector<double> values;", then reserve the amount you want from the vector before the while loop using "values.reserve(25);". In your while loop, change "values[count] = atof(buffer);" to "values.push_back(atof(buffer));", and that should work splendidly. Then, when you want to sort the values, just use "sort (values.begin(), values.end());" and it should, generically, use ascending order. You can use sort to employ a different algorthm, but that's not necessary here.

To get everything to work you'll need to "#include <algorithm>" and "#include <vector>". Also, you can get rid of "#include <stdlib.h>".

And it's much more C++. :)

EDIT: OH, and one more thing, getline has a bug when using "\n" as a delimiter, which will put a random "0" in your vector array. To rectify this, use a simple "if" statement:
        double temp = atof(buffer);
        if (temp != 0) {
            values.push_back(temp);
            sum += temp;
            count++;
        }

Edited by ZekeDragon, 14 August 2009 - 02:35 PM.
See EDIT.

Wow I changed my sig!

#12
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
Zeke, why do you use iii for loops? Wouldn't i just work could enough?

Learning about arrays is kind of pointless if you don't know about loops. Since you can't really do anything cool without them.