Hello Once Again..... I need you to help me with this program im working. My teacher asked me to create a program that could arrange the random numbers from lowest to highest.... but I can't follow up what's next in my program:

#include<stdio.h>
#include<conio.h>
int main()
{
int a,b[10];
for(a=1,a<=10,a++)
{
printf("Enter Number %d",a);
scanf("%d",&b[a]);
}
for(b=0,b<=a,b--)
{
printf("The Numbers in Descending order:%d\n",b[a])
}
return 0;

Is The 2nd loop correct????

your desciption indicates that you will be given a list of random numbers that will be in no particualr order. thus you cannot just loop through the array backwards. You will need to perform some form of sorting algorithm such as a Bubble Sort

Chris

Firstly...

for(b=0,b<=a,b--)
{
          printf("The Numbers in Descending order:%d\n",b[a]);
}

is wrong.As you can see you are starting b's count from 0 and doing a b-- operation.

Secondly...
You need some sorting mechanism to sort your arrray.The simplest to understand may be selection sort to start with.

Well, a simple logic for u is that
1. maintain loop which will run until a condition occurs ( that condition i will tell u a bit later)
2. now in that while loop read the first two number, if the 2nd one is greater than one than swap both number and place in the array again, do the same for 2nd and 3rd elements, and continue doing it until the end of the array.
3. The array will not be sorted on first iteration, we have to continue swapping the elements of array till it will have been sorted in descending order
4. Now lets talk about the condition of while loop, take a variable, and the value of this variable resets to 0 with the start of while loop, and the value of that variable, say x, gets incremented when there is a swapping occur in the whole list. So your while loop will stop if the value of variable x is 0 at the end of any iteration.

This is a slow and time taking algorithm, but its easy to understand. I will provide u the code if u need :)

Yeah...it really takes a long time to finish it.... I barely got the half of it.... anyways I got a fair grade of trying.... but sir still didn't gave us the answer... but I will try your advice on swapping the arrays.... but before I ask for your code I'll have to try my best on this one...... you can give me your code and I'll try to study It on my own..... I really appreciate your help.... ^^

Solution has been sent to you ... however i didnt compile the code, but it will work, i just forgot to set the boundries of array and hope that u will do it by using a nested while loop of if condition, when u try yourself.

Have a happy programming :)

Comments
Do read the forum rules.

Solution has been sent to you ... however i didnt compile the code, but it will work, i just forgot to set the boundries of array and hope that u will do it by using a nested while loop of if condition, when u try yourself.

Have a happy programming :)

Please don't PM code solutions. I suggest re-reading the forums rules too. Just to make sure you understand how we work when it comes to handing out code.

Secondly, code solutions are best in the thread they are the solution for. So in the future people are able to search for solutions to problems and find it. Rather than having to start a new topic, waste more peoples time purely because some goon wouldn't share a solution.

Chris

Comments
Yes.

I have read the bubble sort but may I ask you what does beside of n ( := ) do?? I got this example in wikipedia....

n := length( A )
  do
    swapped := false
    n := n - 1
    for each i in 0 to n - 1  inclusive do:
      if A[ i ] > A[ i + 1 ] then
        swap( A[ i ], A[ i + 1 ] )
        swapped := true
      end if
    end for
  while swapped
end procedure

The thing you have got here is an algorithm its not the actual code you need to look at it and code it on your own.

Here is a simple algorithm:

for i runs from 1 to ArrayLimit-1
{
    for j runs form 0 to ArrayLimit-i
    {
        compare Array[j] and Array[j+1]
        if Array[j] is greater  // Control Statement
        swap Array[j] and Array[j+1]
     }
}

I hope you can construct your code now.Above is for Sorting array in ascending order. Just by changing the control statement as

if Array[j+1] is greater

you can arrange it in descending order.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

And ya the ":" beside n indicates you need to assign that value to n
Ex:
It can be read as "Assign the length of array A to variable n" other statements also can be read in the same way...Its an algorithm.

It seems that the code is written in Pascal or it's just and algorithm. Well, := means equal to (=) in C.

the algorithm is same as i told u before.

This question has already been answered. Start a new discussion instead.