943,907 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4186
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jan 28th, 2009
0

Numbers in descending order(HELP)!

Expand Post »
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:

  1. #include<stdio.h>
  2. #include<conio.h>
  3. int main()
  4. {
  5. int a,b[10];
  6. for(a=1,a<=10,a++)
  7. {
  8. printf("Enter Number %d",a);
  9. scanf("%d",&b[a]);
  10. }
  11. for(b=0,b<=a,b--)
  12. {
  13. printf("The Numbers in Descending order:%d\n",b[a])
  14. }
  15. return 0;

Is The 2nd loop correct????
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Demonisya is offline Offline
23 posts
since Sep 2008
Jan 28th, 2009
0

Re: Numbers in descending order(HELP)!

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
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 28th, 2009
0

Re: Numbers in descending order(HELP)!

Firstly...
  1. for(b=0,b<=a,b--)
  2. {
  3. printf("The Numbers in Descending order:%d\n",b[a]);
  4. }
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.
Reputation Points: 485
Solved Threads: 88
Posting Pro
csurfer is offline Offline
564 posts
since Jan 2009
Jan 29th, 2009
0

Re: Numbers in descending order(HELP)!

What do i have to use in order to obtain the acsending order????
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Demonisya is offline Offline
23 posts
since Sep 2008
Jan 29th, 2009
0

Re: Numbers in descending order(HELP)!

A bubble sort but with the comparison flipped.
Reputation Points: 128
Solved Threads: 43
Posting Whiz
death_oclock is offline Offline
389 posts
since Apr 2006
Jan 30th, 2009
0

Re: Numbers in descending order(HELP)!

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
Reputation Points: 8
Solved Threads: 1
Newbie Poster
its.romi is offline Offline
17 posts
since Dec 2007
Jan 30th, 2009
0

Re: Numbers in descending order(HELP)!

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.... ^^
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Demonisya is offline Offline
23 posts
since Sep 2008
Jan 30th, 2009
-1

Re: Numbers in descending order(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
Reputation Points: 8
Solved Threads: 1
Newbie Poster
its.romi is offline Offline
17 posts
since Dec 2007
Jan 30th, 2009
1

Re: Numbers in descending order(HELP)!

Click to Expand / Collapse  Quote originally posted by its.romi ...
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
Last edited by Freaky_Chris; Jan 30th, 2009 at 6:20 am.
Reputation Points: 325
Solved Threads: 118
Master Poster
Freaky_Chris is offline Offline
702 posts
since Apr 2008
Jan 31st, 2009
0

Re: Numbers in descending order(HELP)!

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

  1.  
  2. n := length( A )
  3. do
  4. swapped := false
  5. n := n - 1
  6. for each i in 0 to n - 1 inclusive do:
  7. if A[ i ] > A[ i + 1 ] then
  8. swap( A[ i ], A[ i + 1 ] )
  9. swapped := true
  10. end if
  11. end for
  12. while swapped
  13. end procedure
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Demonisya is offline Offline
23 posts
since Sep 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Date function in C
Next Thread in C Forum Timeline: converting a 1D array into a 2D array in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC