Thread: Arrays, descending orders

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    23

    Arrays, descending orders

    Hello,
    This is the program that I need to do:
    Write a program that will sort an array of 20 integer numbers into descending order. The results must be displayed on the screen
    in one column.

    I tried this, me totally confuse.
    I tried to do this program for one whole day, done flowchart... could not do it. Or it is me or the program.
    If someone could help.
    Thanks.


    Code:
    #include <stdio.h> #include <conio.h> #include <stdlib.h> int main() { int a[20]; int i, p, j, sum, lar,sma, an, lar1, lar2; /*i for the number and p for the association of th number*/ i=0; sum=0; lar=0; sma=100000; do{ printf(" Enter %d:",i); scanf("%d",&p); /*I use p as my input*/ a[i] = p; /*I use i for my Array*/ i=i+1; if(p>lar) lar=p; if(p<lar && !=lar) lar1=p; if (p<lar1 && !=lar1) lar2=p; }while(i<20); j=i; i=0; do{ printf("a[%d] = %d\n", i, a[i]); i=i+1; }while(i<j); /*I use J as my bonus*/ printf("\n\nThe sum of all this arrays is %d",sum); printf("\nThe largest number is %d",lar); printf("\nThe smalest number is %d",sma); getch(); }

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You should read in ALL your data, and then sort it. Look up Bubble Sort to get your first taste of sorting. How many extra variables do you plan on creating to do comparisons with? Really, don't do it that way.

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    Thanks,
    I did not understand, what you said. "taste of sort"
    Im very confuse. Brain gonna explode lol
    Can you guide me or something. What can I do?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What I meant was, there are many sorting algorithms, but for you Bubble Sort will do. If you've never dealt with sorting before, it will give you an idea of what is involved.
    This should get you started...
    Bubble Sort and Modified Bubble Sort - Cprogramming.com

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    "do you plan on creating to do comparisons with? Really, don't do it that way." I was planning to do more comparisons to finish the program. I read the sort things, they never teach me in class sort. Dont really understand it. I look at the code of the bubble sort, it confuse me more. lol. sorting algorithms able you to play with the arrays?

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    I don't know why the last bit is wrong. :|
    Everthing is in order except a number. The number is 84. :|

    Code:
    /* 	 Write a program that will sort an array of 20 integer numbers into
    descending and ascending order. The results must be displayed on the screen 
    in two side by side columns.
    
    
    */
    
    
    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    
    
    
    
    int main()
    {
    	int a[20] = {15, 21, 20, 10, 32, 41, 21, 32, 65, 69, 98, 54, 65, 58, 57, 69,57 ,14,15,84};
    	int j, i, temp;
    	int n=0;
    int x = 0;
    int y =1;
    
    
    while(n<20)
    {
     for(int x=0; x<n; x++)
    
    
    	{
    
    
    		for(int y=0; y<n-1; y++)
    
    
    		{
    
    
    			if(a[y]<a[y+1])
    
    
    			{
    
    
    				temp = a[y+1];
    
    
    				a[y+1] = a[y];
    
    
    				a[y] = temp;
    
    
    			}
    
    
    		}
    
    
    	}
    
    
    ++x;
    ++y;
    
    
     n++;
    }                  //go back and do 20 more loops with the if statement.
     
    
    
    	
    	i=0;
    	do{
            printf("a[%d] = %d\n", i, a[i]);
    		i=i+1;
    	}while(i<20);								/*I use J as my bonus*/
    
    
    	getch();
    
    
    }

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    What's with this while( n < 20 ) and n++ business? Just set n = 20, or better yet, if you're hard coding the array, use a #define. Look at what the code you copied is doing and the logic behind it. Why does the outer loop go up to the array_size -1? And why the inner loop goes up to the array_size -2. Then, you'll be able to answer why 84 got left behind.

    Don't re-declare variables in a loop, i.e. for( int i = ... In fact, don't declare variables in the middle of your code.

  8. #8
    Registered User
    Join Date
    Dec 2011
    Posts
    23
    -When I mix two program in one to give me this, I forgot to delect the while loop.
    -I have delected the int from the for loop.
    -I understood the if function of bubble sort, need to think about the two loop tomorrow.
    Thanks for the help, I got the output right .

    Have you got a good link or a good book to buy to improve my programming skill, because I got 1 month free and I want to learn a lot
    anyway Thanks for the help.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    C Book Recommendations

    Can't go wrong with Kernighan and Ritchie's The C Programming Language (although some syntax changes since last publish)

  10. #10
    Registered User
    Join Date
    Dec 2011
    Posts
    23

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing pin orders in a #define varaible?
    By graeme in forum C Programming
    Replies: 17
    Last Post: 03-31-2010, 03:33 PM
  2. Program crashing when user orders more than 13 arrays.
    By omnificient in forum C Programming
    Replies: 13
    Last Post: 01-02-2008, 07:10 AM
  3. Alphabetical orders using string
    By EdwardElric in forum C Programming
    Replies: 5
    Last Post: 10-01-2007, 01:17 AM
  4. Tab Orders (Child Controls)
    By (TNT) in forum Windows Programming
    Replies: 6
    Last Post: 07-14-2006, 07:11 AM
  5. Swicthing data orders...
    By Mars_Volta in forum C Programming
    Replies: 4
    Last Post: 04-25-2002, 09:40 PM