SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I didn't know where to post this...so here ya go. We have to write a "Sort the numbers" program for C++ class. The following program I wrote, works perfectly. Except for one thing. The first time you run the "Bubble" sort the lowest number is replaced with 0, and another number is randomly replaced. Everytime thereafter, if you run the "Bubble" sort it will work. But if you quit then try running it again and do a "Bubble" sort it will do the same thing. It doesn't make any since. I narrowed it down to the "Bubble" function I am pretty sure though. So if anyone could compile this and see whats wrong...it would be helpful. I am stumped. Also if no one knows what I am talking about, I can re-explain it. One more thing...does anyone know of any good C++ forums, that I might be able to get some better help at? Thanks in advance.

    Code:
    //Program: Sort
    //Date: 10.3.00
    
    #include <stdio.h>
    #include <windows.h>
    
    //Declare global variables
    int Numbers[10000], MaxNumbers=0, Count=0, PassCounter=0, SwitchCounter=0;
    
    //Declare functions
    void pick (void);
    void random (void);
    void exchange (void);
    void bubble (void);
    
    //Funtion main
    int main() {
    	//Declare local variables
    	int MainMenu=0, NumbersMenu=0;
    	
    	//Print header
    	printf ("-----Sorter-----\n");
    	printf ("  By author  \n");
    	
    	while (1) {
    		//Menu
    		while (1) {
    			printf ("\n1 Exchange Sort");
    			printf ("\n2 Bubble Sort");
    			printf ("\n3 Quit");
    			printf ("\n\nPlease make a selection: ");
    			scanf ("%d", &MainMenu);
    
    			//Check for valid choice
    			if (MainMenu != 1 && MainMenu != 2 && MainMenu != 3) {
    				printf ("\nInvalid choice. Please try again\n");
    			}
    			else {
    				break;
    			}
    		}
    		//Check for quit
    		if (MainMenu==3) {
    			printf ("\nGoodbye!\n\n\n");
    			return 0;
    		}
    
    		//How should we get the numbers to sort
    		while (1) {
    			printf ("\n1 Pick The Numbers");
    			printf ("\n2 Random Numbers");
    			printf ("\n\nPlease make a selection: ");
    			scanf ("%d", &NumbersMenu);
    
    			//Check for valid choice
    			if (NumbersMenu != 1 && NumbersMenu != 2) {
    				printf ("\nInvalid choice. Please try again\n");
    			}
    			else {
    				break;
    			}
    		}
    
    		//How many numbers should we get to sort    
    		printf ("\nHow many numbers should we sort? ");
    		scanf ("%d", &MaxNumbers);
    
    		//Get numbers
    		if (NumbersMenu==1) {
    			pick ();
    		}
    		else if (NumbersMenu==2) {
    			random ();
    		}
    	
       		//Print unsorted list
    		printf ("\nUnsorted list:\n");
    		for (Count=0; Count<MaxNumbers; Count++) {
    			printf ("%d\t", Numbers[Count]);
    		}
        
    		//Run sort
    		if (MainMenu==1) {
    			exchange();
    		}
    		else if (MainMenu==2) {
    			bubble();
    		}
    
    		//Print sorted list
    		printf ("\nSorted list:\n");
    		for (Count=0; Count<MaxNumbers; Count++) {
    			printf ("%d\t", Numbers[Count]);
    		}
     
    		//Print conclusion
    		printf ("\n%d switches were made durring %d passes to sort %d numbers\n", SwitchCounter, PassCounter, MaxNumbers);
    	}
    }
    
    void pick () {
    	//Get numbers from user
    	printf ("\n");		
    	for (Count=0; Count<MaxNumbers; Count++) {
    		printf ("Please enter number %d: ", Count+1);
    		scanf ("%d", &Numbers[Count]);
    	}
    }
    
    void random () {
        //Seed random number generator
        srand (GetTickCount());
        
        //Set random numbers
        for (Count=0; Count<MaxNumbers; Count++) {
            Numbers[Count]=rand()%100+1;
        }
    }
    
    void exchange () {
    	//Declare local variables
    	int X, Y, Tmp;
            
    	//Set counters
    	PassCounter=0;
    	SwitchCounter=0;
    
    	//Run exchange sort
        for (X=0; X<MaxNumbers; X++) {
            for (Y=X+1; Y<MaxNumbers; Y++) {
                if (Numbers[X] > Numbers[Y]) {
                    Tmp=Numbers[X];
                    Numbers[X]=Numbers[Y];
                    Numbers[Y]=Tmp;
                    SwitchCounter++;
                }
            }
    		PassCounter++;
        }
    }
    
    void bubble () {
       	//Declare local variables
    	int X, Y, Tmp, Flag;
        
    	//Set counters
    	PassCounter=0;
    	SwitchCounter=0;
    
    	//Run bubble sort
        while (1) {
    		Flag=0;
    		for (X=0; X<MaxNumbers; X++) {
    			Y=X+1;
    			if (Numbers[X] > Numbers[Y]) {
    				Tmp=Numbers[X];
    				Numbers[X]=Numbers[Y];
    				Numbers[Y]=Tmp;
    				SwitchCounter++;
    				Flag=1;
    			}
    		}
    		if (Flag!=1) {
    			break;
    		}
    		PassCounter++;
    	}
    }
    Last edited by HAWK; Oct 17, 2007 at 18:48.

  2. #2
    Hi there! Owen's Avatar
    Join Date
    Jan 2000
    Location
    CA
    Posts
    1,165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the bubble sort function:

    for (X=0; X<MaxNumbers; X++) {
    Y=X+1;
    if (Numbers[X] > Numbers[Y]) {
    Tmp=Numbers[X];
    Numbers[X]=Numbers[Y];
    Numbers[Y]=Tmp;
    SwitchCounter++;
    Flag=1;
    }
    look at the for loop. it should be X<MaxNumbers-1. Why? Well you're reading past the end of the last element of the array (not really since you made it 10000 elements, but simply past the last defined element.)

    Owen

  3. #3
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yea I changed it to X+1<MaxNumbers and it worked. But my head is not working right now.

    So my question: Is X+1<MaxNumbers = X<MaxNumbers-1?

    Thanks.

  4. #4
    SitePoint Enthusiast Daniel's Avatar
    Join Date
    Dec 1999
    Location
    Belfast
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, X+1<MaxNumbers == X<MaxNumbers-1. However, the second way is the conventional method of writing expressions in C.
    Daniel Irvine
    Aroze - Content management simplified.
    F-Free.Net - Free Stuff and Freebies!

  5. #5
    SitePoint Wizard jumpthru's Avatar
    Join Date
    Apr 2000
    Location
    Los Angeles, California
    Posts
    1,008
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay thanks.


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
  •