Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Simple Arrays Sorting Help?

  1. #1
    Puff is offline Newbie
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Simple Arrays Sorting Help?

    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

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Simple Arrays Sorting Help?

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

  4. #3
    brownhead's Avatar
    brownhead is offline Programmer
    Join Date
    Apr 2009
    Posts
    172
    Rep Power
    12

    Re: Simple Arrays Sorting Help?

    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

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Simple Arrays Sorting Help?

    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

  6. #5
    veda87's Avatar
    veda87 is offline Programmer
    Join Date
    Aug 2009
    Posts
    126
    Rep Power
    0

    Re: Simple Arrays Sorting Help?

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

  7. #6
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Simple Arrays Sorting Help?

    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

  8. #7
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    Re: Simple Arrays Sorting Help?

    Quote Originally Posted by veda87 View Post
    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

  9. #8
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Simple Arrays Sorting Help?

    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!

  10. #9
    Puff is offline Newbie
    Join Date
    Aug 2009
    Posts
    3
    Rep Power
    0

    Re: Simple Arrays Sorting Help?

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

  11. #10
    brownhead's Avatar
    brownhead is offline Programmer
    Join Date
    Apr 2009
    Posts
    172
    Rep Power
    12

    Re: Simple Arrays Sorting Help?

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

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Sorting values from arrays
    By An Alien in forum Java Help
    Replies: 22
    Last Post: 02-21-2011, 10:02 AM
  2. Sorting Arrays
    By chili5 in forum Java Tutorials
    Replies: 11
    Last Post: 02-07-2010, 12:45 PM
  3. Need help sorting 2 arrays.
    By posto2012 in forum C and C++
    Replies: 3
    Last Post: 10-11-2009, 04:46 PM
  4. Help regarding the sorting of arrays with flags!
    By Yuriy M in forum C and C++
    Replies: 3
    Last Post: 10-12-2007, 08:30 PM
  5. Sorting Arrays
    By falco85 in forum PHP Development
    Replies: 5
    Last Post: 08-23-2006, 07:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts