sorting array efficiently...can't find the error

This is a discussion on sorting array efficiently...can't find the error within the C++ Programming forums, part of the General Programming Boards category; I have a program to load and sort an array of 20 elements. The user enters however many doubles they ...

  1. #1
    Unregistered
    Guest

    Question sorting array efficiently...can't find the error

    I have a program to load and sort an array of 20 elements. The user enters however many doubles they want and then the array is sorted and displayed. I can't figure out how to get the sort to happen the fewest number of times - meaning if after 2 passes, the array is sorted, I want it to stop. What I have seems to make sense to me, but what happens right now is that the largest number gets dropped and 0.0 is placed in the array[0] element.

    Here it is..., Thanks in advance for suggestions.

    #include <iostream>
    #include <iomanip>

    using namespace std;

    int loadarray (double []);
    void display (double [], int);
    void sortarray(double [], int);

    void main ()
    {
    double array1 [20] = {0.0};
    int numgood;
    char junk;

    numgood = loadarray(array1);

    sortarray(array1, numgood);

    display(array1, numgood);


    }


    int loadarray (double array1[])
    { int num = 0;
    char response = 'Y';

    while (toupper(response) == 'Y')
    { cout << "\nEnter double #" << num + 1 << ": ";
    cin >> array1[num];
    num++;
    cout << "Enter another? Y/N: ";
    cin >> response;
    }

    return num;
    }

    void display (double array1[], int num)
    {
    cout << setw(10) << "Element" << setw(10) << "Value\n";
    cout << setiosflags ( ios::fixed | ios::showpoint ) << setprecision(2);
    for (int x = 0; x < num; x++)
    { cout << setw(10) << x + 1 << setw(10) << array1[x] << endl;
    }
    }

    void sortarray (double array1[], int num)
    {
    double temp;
    int change = 1;

    for (int pass = 0; pass < num - 1; pass++)
    { for (int i = 0; i < num - pass; i++)
    { if ( array1[i] > array1[i + 1])
    { temp = array1[i];
    array1[i] = array1[i+1];
    array1[i+1] = temp;
    change = 1;
    }
    else
    { change = 0;
    }
    }
    if (change == 0)
    break;
    }

    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The user enters however many doubles they want
    As long as it's 20 or less

    >I can't figure out how to get the sort to happen the fewest number of times
    When in doubt, use qsort:
    Code:
    void sortarray ( double array1[], int num ) 
    { 
      qsort ( array1, num, sizeof array1[0], cmp );
    }
    
    int cmp ( const void *x, const void *y )
    {
      if ( *(const double*)x < *(const double*)y )
        return -1;
      if ( *(const double*)x > *(const double*)y )
        return  1;
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    actually, i'm proud to say i just finished working on a binary recursive sort (with a little help of course ) it's on the board now under "function syntax error". in theory it can sort 1000 elements in 10 passes

    don't for get code tags next time

  4. #4
    Unregistered
    Guest
    What you are trying to do is called a bubble sort. It is probably the simplest sort you can code. The way I learned to do it is to work from the end of the array to the beginning like this...

    Code:
    void sortarray(double array1[], int num) 
    { 
    	double temp; 
    	int change; 
    
    	for (int pass = 0; pass < num; pass++) 
    	{
    		change = 0;		
    
    		for (int i = num-1; i; i--) 
    		{
    			if ( array1[i] < array1[i-1]) 
    			{ 
    				temp = array1[i];
    				array1[i] = array1[i-1]; 
    				array1[i-1] = temp; 
    				change = 1; 
    			} 
    		}
    		if (!change) break; 
    	} 
    }

  5. #5
    Unregistered
    Guest
    Thanks so much for your responses! Yes, I was trying to do a bubble sort - I finally got it to work but not sure if it was actually exiting the sort early or if it ran through each loop.

    And, I'm sorry about posting the code that way - I read the message about using the code tags right after I posted! Will know for next time.

    thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21