# rrange the numbers into lowest to highest

Page 1 of 1

## 14 Replies - 455 Views - Last Post: 10 December 2012 - 07:58 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=303403&amp;s=73a73993e0cbc61d2f21195e8c861cf2&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jayveeeeeee

Reputation: -1
• Posts: 66
• Joined: 27-September 12

# rrange the numbers into lowest to highest

Posted 10 December 2012 - 02:37 AM

i cant figure out my error .actually my code have not error.just look at the picture

...here's my code
```#include<stdio.h>
#include<conio.h>
main()
{
const int Size = 12;
int contain[Size];
printf("\n enter  number:\n");
for(int i=0;i<Size;i++)
scanf("%d",& contain[i]);
{

int l , h;
for(l = 0 ; l<12-1; l++)
for(h=l+1 ; h<12; h++)
if(contain[l] < contain[h])
{ int temp;

temp = contain[l];
contain[l]= contain[h];
contain[h] = temp;

}

for(int k = 0 ; k<12; k++)
printf("\nAscending number %d\n\t", contain[k]);
printf("\n\nLowest is %d\t\Highest is %d \n\t" ,contain[l],contain[h]);
getch();
}  }

```

#### Attached image(s)

This post has been edited by baavgai: 10 December 2012 - 05:48 AM
Reason for edit:: tagged

Is This A Good Question/Topic? 0

## Replies To: rrange the numbers into lowest to highest

### #2 raghav.naganathan

• Perfectly Squared ;)

Reputation: 410
• Posts: 1,440
• Joined: 14-September 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 02:45 AM

Well, here are some suggestions for you.

2.Always use int main() instead of main() and do remember to return 0 at the end of the main().
3.Avoid unnecessary braces as I can see in lines after your scanf and in the end of your program as well.

I believe there is a logical error in your program regarding the part of ascending numbers.

Edit:One more thing...there is no way your print statement will print
'enter 12 number' as you have shown in your output!

regards,
Raghav

This post has been edited by raghav.naganathan: 10 December 2012 - 02:54 AM

### #3 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 03:42 AM

raghav.naganathan, on 10 December 2012 - 11:45 AM, said:

3.Avoid unnecessary braces as I can see in lines after your scanf and in the end of your program as well.

I don't think that is good for every case , you should use as many braces as you need in order to make it less prone to bugs and easier to understand.
Just make sure the spacing is good and you format your code so you can understand it with ease.

To the OP : What should your program do ? Please explain it in detail />

This post has been edited by TwoOfDiamonds: 10 December 2012 - 03:44 AM

### #4 jayveeeeeee

Reputation: -1
• Posts: 66
• Joined: 27-September 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 04:23 AM

i have to arrange the number into lowest to highest number and that can also print the lowest and highest numbers;

### #5 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 05:25 AM

Your comparison is wrong , your alogorithm sorts them from highest to lowest , you might want to change that from this piece of code :

```int l , h;
for(l = 0 ; l<12-1; l++)
for(h=l+1 ; h<12; h++)
if(contain[l] < contain[h])
{ int temp;

temp = contain[l];
contain[l]= contain[h];
contain[h] = temp;
}

```

focus on
```if(contain[l] < contain[h])
```
mostly . You might want to write 2 or 3 random numbers on a piece of paper and follow the algorithm step by step yourself.

This post has been edited by TwoOfDiamonds: 10 December 2012 - 05:26 AM

### #6 baavgai

• Dreaming Coder

Reputation: 6339
• Posts: 13,539
• Joined: 16-October 07

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 05:59 AM

raghav.naganathan, on 10 December 2012 - 04:45 AM, said:

3.Avoid unnecessary braces as I can see in lines after your scanf and in the end of your program as well.

I believe the intent here is use braces properly, which I couldn't agree with more. However, if it means don't use braces you can leave out, then I couldn't disagree more.

Rather, if your block is a one liner and you don't need braces; use them anyway. Always use braces to define your block and you'll never be surprised.

Also, choose an indent style. And, if you're using a book that tells you using Turbo-C is a good idea; it's lying to you.

Cleaned up:
```#include<stdio.h>
// #include<conio.h> never

// wrong
// main() {
int main() {
const int Size = 12;
int contain[Size];
printf("\n enter  number:\n");
for (int i = 0; i < Size; i++) { // brace here
scanf("%d", &contain[i]);
} // brace here

// no magic numbers
// for (l = 0; l < 12 - 1; l++) {
for (int l = 0; l < Size - 1; l++) {
for (int h = l + 1; h < Size; h++) {
if (contain[l] < contain[h]) {
int temp = contain[l];
contain[l] = contain[h];
contain[h] = temp;

}
}
}

for (int k = 0; k < Size; k++) {
printf("\nAscending number %d\n\t", contain[k]);
}
// forget l and h...
// printf("\n\nLowest is %d\t\Highest is %d \n\t", contain[l], contain[h]);
printf("\n\nLowest is %d\t\Highest is %d \n\t", contain[0], contain[Size-1]);

return 0;
}

```

### #7 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 06:03 AM

baavgai, on 10 December 2012 - 02:59 PM, said:

raghav.naganathan, on 10 December 2012 - 04:45 AM, said:

3.Avoid unnecessary braces as I can see in lines after your scanf and in the end of your program as well.

I believe the intent here is use braces properly, which I couldn't agree with more. However, if it means don't use braces you can leave out, then I couldn't disagree more.

Rather, if your block is a one liner and you don't need braces; use them anyway. Always use braces to define your block and you'll never be surprised.

Also, choose an indent style. And, if you're using a book that tells you using Turbo-C is a good idea; it's lying to you.

I considered the second case

### #8 jayveeeeeee

Reputation: -1
• Posts: 66
• Joined: 27-September 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:21 AM

i try your code by it is not working when i run it no output show

### #9 no2pencil

• Professor Snuggly Pants

Reputation: 5912
• Posts: 28,937
• Joined: 10-May 07

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:26 AM

** Renamed title to be more descriptive **

### #10 jayveeeeeee

Reputation: -1
• Posts: 66
• Joined: 27-September 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:39 AM

### #11 no2pencil

• Professor Snuggly Pants

Reputation: 5912
• Posts: 28,937
• Joined: 10-May 07

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:40 AM

Please provide your updated code from what you tried with baavgai's suggestion, along with more description than "it's not working".

### #12 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:41 AM

jayveeeeeee, on 10 December 2012 - 04:39 PM, said:

I gave you the answer . Just think for yourself a little , no one is going to give you the answer like that.

### #13 jayveeeeeee

Reputation: -1
• Posts: 66
• Joined: 27-September 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:55 AM

TwoOfDiamonds And Baavgai Thanks For All .i Know now what is the problem now .

### #14 TwoOfDiamonds

• D.I.C Regular

Reputation: 54
• Posts: 272
• Joined: 27-July 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:56 AM

jayveeeeeee, on 10 December 2012 - 04:55 PM, said:

TwoOfDiamonds And Baavgai Thanks For All .i Know now what is the problem now .

It was our pleasure

### #15 jayveeeeeee

Reputation: -1
• Posts: 66
• Joined: 27-September 12

## Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:58 AM

thanks to All Who Help me for my Assignment

TwoOfDiamonds can i add to become my friend