Thread: Decending Sort Problem

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    18

    Exclamation Decending Sort Problem

    Im having a problem with the decending part of this bubble sort. Array a[] is sorted into acending
    Array b[] is sorted into decending

    The output is good for acending, but all 0 for decending.
    Heres my source!
    Code:
    //David Thurlby
    //CIS 150
    //Assignment#6-7
    
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #define SIZE 20
    
    int getRand();
    int sortA();
    int sortD();
    int printSorted();
    
    int a[SIZE];
    int b[SIZE];
    int count,swap1,swap2,hold;	
    
    int main()
    {
    
    	printf("Getting Random Numbers\n");
    	getRand(a[SIZE]);
    	for(count=0;count<SIZE;count++)
    	printf("%4d",a[count]);
    
    		
    	b[count]=a[count];
    
    	sortA(a[SIZE]);
    	sortD(b[SIZE]);
    	
    	printf("\n\n");
    	printf("Swaps for A= %d\n",swap1);
    	printf("Swaps for D= %d\n",swap2);
    	printf("Press Any Key To Continue");
    	getch();
    	system("cls");
    	printSorted();
    	return 0;
    }
    
    
    
    int getRand()//getting Random num
    {	
    
    	srand(time(NULL));
    	
    	for(count=0;count<=SIZE;count++)
    	{
    		a[count]=rand()%100;
    	}	
    	return 0;
    }
    
    
    int sortA()//sort Acending
    { 
    	for(swap1=1;swap1<=SIZE-1;swap1++)
    	{
    		for(count=0;count<=SIZE-2;count++)
    		{
    				if(a[count]>a[count+1])
    				{
    					hold=a[count];
    					a[count]=a[count+1];
    					a[count+1]=hold;
    				}
    		}
    	}
    		return 0;	
    }
    
    
    
    int sortD()//sort Decending
    {
    	for(swap2=1;swap2<=SIZE-1;swap2++)
    	{
    		for(count=0;count<=SIZE-2;count++)
    		{
    				if(b[count]<b[count+1])
    				{
    					hold=b[count];
    					b[count]=b[count+1];
    					b[count+1]=hold;
    				}
    		}
    	}
    		return 0;	
    }
    
    int printSorted()
    {
    	for(count=0;count<SIZE;count++)
    	printf("%d\n",a[count]);
    	getch();
    	system("cls");
    	for(count=0;count<SIZE;count++)
    	printf("%d\n",b[count]);
    
    	return 0;
    }
    thanks for any help you can give me!
    Dave

  2. #2
    dt02400
    Guest
    make following revison, still dont work!

    Code:
    //David Thurlby
    //CIS 150
    //Assignment#6-7
    
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #define SIZE 20
    
    int getRand();
    int sortA();
    int sortD();
    int printSorted();
    
    int a[SIZE];
    int b[SIZE];
    int count,swap1,swap2,hold;	
    
    int main()
    {
    
    	printf("Getting Random Numbers\n");
    	getRand(a[SIZE]);
    	for(count=0;count<SIZE;count++)
    	printf("%4d",a[count]);
    
    		
    
    	b[count]=a[count];
    	sortA(a[SIZE]);
    	sortD(b[SIZE]);
    	
    	printf("\n\n");
    	printf("Swaps for A= %d\n",swap1);
    	printf("Swaps for D= %d\n",swap2);
    	printf("Press Any Key To Continue");
    	getch();
    	system("cls");
    	printSorted();
    	return 0;
    }
    
    
    
    int getRand()//getting Random num
    {	
    
    	srand(time(NULL));
    	
    	for(count=0;count<=SIZE;count++)
    	{
    		a[count]=rand()%100;
    	}	
    	return 0;
    }
    
    
    int sortA()//sort Acending
    { 
    	for(swap1=1;swap1<=SIZE-1;swap1++)
    	{
    		for(count=0;count<=SIZE-2;count++)
    		{
    				if(a[count]>a[count+1])
    				{
    
    					a[count]=a[count+1];
    					hold=&a[count+1];
    				}
    		}
    	}
    		return 0;	
    }
    
    
    
    int sortD()//sort Decending
    {
    	for(swap2=1;swap2<=SIZE-1;swap2++)
    	{
    		for(count=0;count<=SIZE-2;count++)
    		{
    				if(b[count]<b[count+1])
    				{
    					b[count]=b[count+1];
    					hold=&b[count+1];
    				}
    		}
    	}
    		return 0;	
    }
    
    int printSorted()
    {
    	for(count=0;count<SIZE;count++)
    	printf("%d\n",a[count]);
    	getch();
    	system("cls");
    	for(count=0;count<SIZE;count++)
    	printf("%d\n",b[count]);
    
    	return 0;
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Try this:
    Code:
    #include <stdio.h>
    #include <time.h>
    #include <stdlib.h>
    #include <conio.h>
    
    #define SIZE 20
    
    int getRand();
    int sortA();
    int sortD();
    int printSorted();
    
    int a[SIZE];
    int b[SIZE];
    int count,swap1,swap2,hold;	
    
    int main()
    {
      
      printf("Getting Random Numbers\n");
      getRand();
      for(count=0;count<SIZE;count++)
        printf("%4d",a[count]);
      
      sortA(a[SIZE]);
      sortD(b[SIZE]);
      
      printf("\n\n");
      printf("Swaps for A= %d\n",swap1);
      printf("Swaps for D= %d\n",swap2);
      printf("Press Any Key To Continue");
      getch();
      system("cls");
      printSorted();
      return 0;
    }
    
    
    
    int getRand()//getting Random num
    {	
      
      srand(time(NULL));
      
      for(count=0;count<=SIZE;count++)
      {
        a[count]=b[count]=rand()%100;
      }	
      return 0;
    }
    
    
    int sortA()//sort Acending
    { 
      for(swap1=1;swap1<=SIZE-1;swap1++)
      {
        for(count=0;count<=SIZE-2;count++)
        {
          if(a[count]>a[count+1])
          {
            hold = a[count];
            a[count]=a[count+1];
            a[count+1] = hold;
          }
        }
      }
      return 0;	
    }
    
    
    
    int sortD()//sort Descending
    { 
      for(swap1=1;swap1<=SIZE-1;swap1++)
      {
        for(count=0;count<=SIZE-2;count++)
        {
          if(b[count]<b[count+1])
          {
            hold = b[count];
            b[count]=b[count+1];
            b[count+1] = hold;
          }
        }
      }
      return 0;	
    }
    
    int printSorted()
    {
      for(count=0;count<SIZE;count++)
        printf("%d\n",a[count]);
      getch();
      system("cls");
      for(count=0;count<SIZE;count++)
        printf("%d\n",b[count]);
      
      return 0;
    }
    -Prelude
    Last edited by Prelude; 10-26-2002 at 03:31 PM.
    My best code is written with the delete key.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >And Prelude's answer has a few array overruns left in it.
    I didn't have time to fix everything. This is an exercise left to the OP.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Oct 2002
    Posts
    12

    Smile

    this should work

    for(count=0;count<SIZE;count++)
    {
    a[count]=rand()%101+1;
    b[count]=a[count];
    printf("%4d",a[count]);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. heap sort problem
    By Cpro in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2008, 04:54 AM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Using Vectors (cont) - Sort Problem
    By clegs in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2007, 06:31 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. Sorting
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-10-2003, 05:21 PM