Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. To arrange n numbers in descending order ?

To arrange n numbers in descending order ?

Scheduled Pinned Locked Moved C / C++ / MFC
jsonquestion
6 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tarun Jha
    wrote on last edited by
    #1

    i tries but the rest lies here. :sigh:

    #include
    #include

    int main(){

    int num\[50\], i=0, largest, count =0;
    int k, j;
    
    printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
    
    do{
    
    	scanf("%d", &num\[i\]);
    	i++;
    	count++;
    
    }while(num\[i-1\]!=0);
    
    
    
    while(count>=0){
    
            largest = 0;
    	
    		for(j=0; j<=count-1; j++){
    		
    			if(num\[j\] > largest){
    
    			largest = num\[j\];
    				j=k;
    			}
    		}
    		
    		printf("%d", largest);
    		
    		for(j=k; j<=count-1; j++){
    			
    			num\[j\] = num\[j+1\];
    		}
    		
    		count = count - 2;
    }
    
    return 0;
    

    }

    J D L _ C 5 Replies Last reply
    0
    • T Tarun Jha

      i tries but the rest lies here. :sigh:

      #include
      #include

      int main(){

      int num\[50\], i=0, largest, count =0;
      int k, j;
      
      printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
      
      do{
      
      	scanf("%d", &num\[i\]);
      	i++;
      	count++;
      
      }while(num\[i-1\]!=0);
      
      
      
      while(count>=0){
      
              largest = 0;
      	
      		for(j=0; j<=count-1; j++){
      		
      			if(num\[j\] > largest){
      
      			largest = num\[j\];
      				j=k;
      			}
      		}
      		
      		printf("%d", largest);
      		
      		for(j=k; j<=count-1; j++){
      			
      			num\[j\] = num\[j+1\];
      		}
      		
      		count = count - 2;
      }
      
      return 0;
      

      }

      J Offline
      J Offline
      jeron1
      wrote on last edited by
      #2

      Maybe take a look at this[^] thread.

      "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

      1 Reply Last reply
      0
      • T Tarun Jha

        i tries but the rest lies here. :sigh:

        #include
        #include

        int main(){

        int num\[50\], i=0, largest, count =0;
        int k, j;
        
        printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
        
        do{
        
        	scanf("%d", &num\[i\]);
        	i++;
        	count++;
        
        }while(num\[i-1\]!=0);
        
        
        
        while(count>=0){
        
                largest = 0;
        	
        		for(j=0; j<=count-1; j++){
        		
        			if(num\[j\] > largest){
        
        			largest = num\[j\];
        				j=k;
        			}
        		}
        		
        		printf("%d", largest);
        		
        		for(j=k; j<=count-1; j++){
        			
        			num\[j\] = num\[j+1\];
        		}
        		
        		count = count - 2;
        }
        
        return 0;
        

        }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        So what's the problem? Can we assume you've stepped through the code, line by line, using the debugger (to see the value of each variable along the way)?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        1 Reply Last reply
        0
        • T Tarun Jha

          i tries but the rest lies here. :sigh:

          #include
          #include

          int main(){

          int num\[50\], i=0, largest, count =0;
          int k, j;
          
          printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
          
          do{
          
          	scanf("%d", &num\[i\]);
          	i++;
          	count++;
          
          }while(num\[i-1\]!=0);
          
          
          
          while(count>=0){
          
                  largest = 0;
          	
          		for(j=0; j<=count-1; j++){
          		
          			if(num\[j\] > largest){
          
          			largest = num\[j\];
          				j=k;
          			}
          		}
          		
          		printf("%d", largest);
          		
          		for(j=k; j<=count-1; j++){
          			
          			num\[j\] = num\[j+1\];
          		}
          		
          		count = count - 2;
          }
          
          return 0;
          

          }

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          You need to stop writing code for a while and spend more time thinking about the problem. What steps are needed to accomplish your goal? Write them down on paper, line by line to understand what needs to be done at each step. I already showed you how to find the largest number in a sequence and you are still making it more complicated. As you read in the integers in the first loop you can check for the largest as you store them, you do not need a second loop. You also need to look at the statement in the second loop:

          largest = num\[j\];
          	j=k;
          

          What is the value of k at this point? And in the final loop you are just moving numbers, with no regard to whether they are in any order.

          1 Reply Last reply
          0
          • T Tarun Jha

            i tries but the rest lies here. :sigh:

            #include
            #include

            int main(){

            int num\[50\], i=0, largest, count =0;
            int k, j;
            
            printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
            
            do{
            
            	scanf("%d", &num\[i\]);
            	i++;
            	count++;
            
            }while(num\[i-1\]!=0);
            
            
            
            while(count>=0){
            
                    largest = 0;
            	
            		for(j=0; j<=count-1; j++){
            		
            			if(num\[j\] > largest){
            
            			largest = num\[j\];
            				j=k;
            			}
            		}
            		
            		printf("%d", largest);
            		
            		for(j=k; j<=count-1; j++){
            			
            			num\[j\] = num\[j+1\];
            		}
            		
            		count = count - 2;
            }
            
            return 0;
            

            }

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            What you basically need to do is to sort the array in descending order. There are several sorting algorithms. This the first google search link I found for sorting - Sorting Arrays[^] Go through them, try them and understand them.

            «_Superman_»  _I love work. It gives me something to do between weekends.

            _Microsoft MVP (Visual C++) (October 2009 - September 2013)

            Polymorphism in C

            1 Reply Last reply
            0
            • T Tarun Jha

              i tries but the rest lies here. :sigh:

              #include
              #include

              int main(){

              int num\[50\], i=0, largest, count =0;
              int k, j;
              
              printf("Enter 0 to exit entering integers.\\n\\nEnter your integers:\\n");
              
              do{
              
              	scanf("%d", &num\[i\]);
              	i++;
              	count++;
              
              }while(num\[i-1\]!=0);
              
              
              
              while(count>=0){
              
                      largest = 0;
              	
              		for(j=0; j<=count-1; j++){
              		
              			if(num\[j\] > largest){
              
              			largest = num\[j\];
              				j=k;
              			}
              		}
              		
              		printf("%d", largest);
              		
              		for(j=k; j<=count-1; j++){
              			
              			num\[j\] = num\[j+1\];
              		}
              		
              		count = count - 2;
              }
              
              return 0;
              

              }

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              #include

              #define N 50

              int main()
              {
              int num[N], i=0, count =0;

              printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");

              do
              {
              if( scanf("%d", &num[i]) != 1) break;
              ++i;
              count++;

              } while ( i

              As an alternative, tou might use qsort:

              #include
              #include

              #define N 50

              int comp(const void * a, const void *b)
              {
              return *(int *)b - *(int*)a;
              }

              int main()
              {
              int num[N], i=0, count =0;

              printf("Enter 0 to exit entering integers.\n\nEnter your integers:\n");

              do
              {
              if( scanf("%d", &num[i]) != 1) break;
              ++i;
              count++;

              } while ( i

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups