# how to print N numbers in descending order

Page 1 of 1

## 3 Replies - 12005 Views - Last Post: 20 February 2009 - 09:32 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=87907&amp;s=79d44b69e83a470b4ea100d1d2590423&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 yvsham

Reputation: 0
• Posts: 1
• Joined: 20-February 09

# how to print N numbers in descending order

Posted 20 February 2009 - 08:43 AM

#### Attached File(s)

Is This A Good Question/Topic? 0

## Replies To: how to print N numbers in descending order

### #2 PostulateMan

Reputation: 2
• Posts: 50
• Joined: 14-September 08

## Re: how to print N numbers in descending order

Posted 20 February 2009 - 08:52 AM

Hey yvsham. Looks like you've got the right idea, you just need to brush up on your for loop syntax. You currently have:

```for(i=0;i+1;i++)

```

The syntax for a for loop is:
for( (initializer list); (conditional); (iteration) )

You have the right idea in the first part. You initialize i to 0, but if you start as zero and count up you get ascending order.

The second statement in the for loop is used to check a condition. Right now you have i+1, which will always be true. Here you want to check for when to stop. Commonly, you can check if i < someNumber or that (hint hint) i >= 0.

The third part you use correctly, but again, you are set up to use the for loop in ascending order. For descending order the values get smaller. Since ++ adds one to i every time the loop iterates, your values will get larger.

Hope this helps!

### #3 pdkharkar

• D.I.C Regular

Reputation: 65
• Posts: 345
• Joined: 19-January 09

## Re: how to print N numbers in descending order

Posted 20 February 2009 - 09:23 AM

this is a bubble sort algorithm!!!
I hope this helps you with some minor changes..............

```for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++)
if (a[j+1] < a[j]) {  /* compare the two neighbors */
tmp = a[j];		 /* swap a[j] and a[j+1]	  */
a[j] = a[j+1];
a[j+1] = tmp;
}
}

```

### #4 pdkharkar

• D.I.C Regular

Reputation: 65
• Posts: 345
• Joined: 19-January 09

## Re: how to print N numbers in descending order

Posted 20 February 2009 - 09:32 AM

this is a bubble sort algorithm!!!
I hope this helps you with some minor changes..............

```for (i=0; i<n-1; i++) {
for (j=0; j<n-1-i; j++)
if (a[j+1] < a[j]) {  /* compare the two neighbors */
tmp = a[j];		 /* swap a[j] and a[j+1]	  */
a[j] = a[j+1];
a[j+1] = tmp;
}
}

```

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}