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.

#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();

}

In the future, please put all code samples inside of code-tags. The forum software does not retain indentation by default, so without the code tags the formatting is lost, making it hard to read the code.

Here is your code with suitable formatting (courtesy of AStyle):

#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();
}

Now that we can read the code, can you tell us what problems you are having with it? While I can see that it does not sort the values (I would recommend reading up on Sorting Algorithms), it would help if you could tell us in your own words what the problem is.

I know how arrays work. The problem is that I don't know how to put the numbers in order. Example if I input 20 number:
10, 2, 2, 3, 1, 2, 6 ,9 ,6 ,4 ,11 ,21 ,32 ,21 ,25 ,36 ,36, 35 ,25, 26

How could I get the answer to be in descending order by the program.

I know how arrays work. The problem is that I don't know how to put the numbers in order. Example if I input 20 number:
10, 2, 2, 3, 1, 2, 6 ,9 ,6 ,4 ,11 ,21 ,32 ,21 ,25 ,36 ,36, 35 ,25, 26

How could I get the answer to be in descending order by the program.

You could just do a bubble sort after the another number was inputted..

One easy way to do this is to use a bubble sort to sort the array in reverse order, then print out the array. A bubble sort is not very efficient, but it is easy and it will get the job done. Here is how a bubble sort works:

Fill array with 20 integers;
if(a[0] is less than a[1])
then swap 'em;
if(a[1] is less than a[2])
then swap 'em;
if (a[2] is less than a[3])
then swap 'em;
if(a[3] is less than a[4])
then swap 'em;
etc.; Just keep going through the whole array like this, and as you do, the array will become more ordered in descending order (9 8 7 6 5 4 etc). But you have to do this process over and over until all of the array is in perfect descending order. Then just print out the array. This really doesn't take very many lines of code if you use loops - something like this:

Fill array with 20 numbers;

if(a[0] < a[1])
   {
    x = a[1];
    a[1] = a[0];
    a[0] = x;        //there, the two have been swapped and the larger of the two is now in a[0].
   {

But all this has to be in a loop, so:

Fill array with 20 numbers;
int x = 0;
int y =1;

start:
if(a[x] < a[y])
  {
   temp = a[x];
   a[x] = a[y];
   a[y] = temp;    //the swap is completed.
  }
++x;
++y;
loop back around to "start" 20 times to do the whole array;

Now that we have gone through the whole array once, we need to do it again and again and again until all of the numbers are in perfect descending order. So we need a nested loop:

Fill array with 20 numbers;
int x = 0;
int y =1;

while(not done)
{

start:
if(a[x] < a[y])
  {
   temp = a[x];
   a[x] = a[y];
   a[y] = temp;    //the swap is completed.
  }
++x;
++y;
loop back around to "start" 20 times to do the whole array;

}                  //go back and do 20 more loops with the if statement.

print out array;

That's pretty much all there is to it, except that you will have to know when all of the numbers have been placed into descending order so that you can end the while loop - while(not done). Here is a hint: When all of the numbers are in descending order in the array, then in line 9 the if(condition) will always be false and the body of the if statement will always be skipped for the 20 times that it loops.

So if the array is sorted, the body of the if statement will never execute for the 20 loops, but if the array is not done being sorted then the body of that if statement will execute at least once during the 20 loops. So use that fact to terminate the while loop and print out the array.

I have done this from your instruction. I have understood the bubble sort, thanks for making everything in step. However did not understand the last bit.

The output that is giving me is
http://postimage.org/image/ht6f25z07/
which is false :(
The number 10 went to the bottom. Understood this.

#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)
{
 
start:
if(a[x] < a[y])
  {
   temp = a[x];
   a[x] = a[y];
   a[y] = temp;    //the swap is completed.
  }
++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();

}

I have done this from your instruction. I have understood the bubble sort, thanks for making everything in step. However did not understand the last bit.

#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)
    {
     
    start:
    if(a[x] < a[y])
    {
    temp = a[x];
    a[x] = a[y];
    a[y] = temp; //the swap is completed.
    }
    ++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();
     
    }

"Start:" in line 16 isn't really part of the code (it's just a label - it doesn't do anything). Also, in line 13 you have while(n < 20). The problem is, we don't know how many times the while loop needs to run - that all depends on how much sorting needs to be done on the numbers in the array. The last part that you didn't understand has to do with how you end the while loop. And also, you need two loops for the bubble sort, and you have only one - the while loop. Lines 17 - 24 have to loop also (20 times). So then you will have lines 17 - 24 in the inner loop which is within the while loop. I don't think you understand the bubble sort, so let me explain it again:

You want all of the numbers in the array to be ordered with the largest one to the smallest one. So, you start with the first two and ask the question: A[0] < a[1]? If so, then you need to reverse them. But if a[0] is already larger than a[1], then there is no need to reverse them (i.e. swap them). Then you go to the next one, a[1] < a[2]? And the same thing, if a[1] is less than a[2] then reverse them, otherwise let them be. And you go on through your entire array doing that for all the numbers. And you use a loop to do this - that's your inner loop.

Now once you have done that, your array is only partially in order. So you have to keep repeating that process until all numbers are in order. That is what the while loop is for - to keep repeating the other loop until all numbers are in order.

You need to be sure you understand how the bubble sort works before you can code it properly. So work the bubble sort out by hand with paper and pencil. Start with 5 random numbers and work the algorithm by hand on paper to get the 5 numbers sorted in descending order. Then you should be able to understand the coding:

while(?)                        //this is your outer loop;
{
  do 20 loops with that if statement: // this is your inner loop;

}

The while loop keeps going until all the numbers are sorted in order. How many times that is is unknown. Therefore, you can't have while(n < 20). 20 is for the inner loop. So how do you know when to stop the while loop? OK, here is that hint again: Remember that the if statement in line 17 loops 20 times. Now, if ALL of the numbers are done being sorted, then the body of that if statement will not execute, it will be skipped, for all 20 loops. But if all of the numbers are not in order yet, then the body of that if statement with be executed at least one time, because a[x] < a[y] will still prove true at least once, but never if the sorting is all done. So use some kind of detection for that to break out of the while loop.

Like I said, do the bubble sort by hand on paper with 5 numbers and then you will see how to code it. You MUST understand that first. If you have further questions, or you still don't understand, let us know and we'll see if we can explain it better.

#include <iostream>
#include <algorithm>
 
int main() {
  int array[] = { 23, 5, -10, 0, 0, 321, 1, 2, 99, 30 };
  int elements = sizeof(array) / sizeof(array[0]); 
  std::sort(array, array + elements);
  for (int i = 0; i < elements; ++i) 
     std::cout << array[i] << ' ';
}[B][/B]

use algorithm

Dakot: while I would agree in general that using the standard sorting function is the best solution for most cases, in this instance this is clearly a homework problem, and writing the sorting function is the main objective. Also, the OP seems to be using an older compiler (probably Turbo C++ 3.0), which almost certainly does not include the <algorithm> header and library.

As for the OP's problem, I would check out the description of bubble sort on Wikipedia, as it has a nice animation showing how the sorting takes place. That may help you visualize the solution better. Alternatively, you could try using Gnome Sort, which is a bit simpler but not so obvious as bubble sort (which is traditionally seen as a 'naive' algorithm, that is to say, it is one most people would stumble upon if asked to make up a way to sort things without help).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.