14 Replies - 317 Views - Last Post: 10 December 2012 - 07:58 AM Rate Topic: -----

#1 jayveeeeeee  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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)

  • Attached Image

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  Icon User is offline

  • Perfectly Squared ;)
  • member icon

Reputation: 408
  • View blog
  • 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.

1.Please use CODE tags along with your code...so that your code will be more easily readable.
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

Was This Post Helpful? 0
  • +
  • -

#3 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

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

Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 03:42 AM

View Postraghav.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

Was This Post Helpful? 0
  • +
  • -

#4 jayveeeeeee  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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;
Was This Post Helpful? 0
  • +
  • -

#5 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

Reputation: 54
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5761
  • View blog
  • Posts: 12,574
  • Joined: 16-October 07

Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 05:59 AM

View Postraghav.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;
}


Was This Post Helpful? 1
  • +
  • -

#7 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

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

Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 06:03 AM

View Postbaavgai, on 10 December 2012 - 02:59 PM, said:

View Postraghav.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 :)
Was This Post Helpful? 1
  • +
  • -

#8 jayveeeeeee  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5163
  • View blog
  • Posts: 26,834
  • 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 **
Was This Post Helpful? 0
  • +
  • -

#10 jayveeeeeee  Icon User is offline

  • D.I.C Head

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

Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:39 AM

please help me baavgai . i need your idea for my assignment ,,
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is online

  • Dic Head
  • member icon

Reputation: 5163
  • View blog
  • Posts: 26,834
  • 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".
Was This Post Helpful? 0
  • +
  • -

#12 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

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

Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:41 AM

View Postjayveeeeeee, on 10 December 2012 - 04:39 PM, said:

please help me baavgai . i need your idea for my assignment ,,


I gave you the answer . Just think for yourself a little , no one is going to give you the answer like that.
Was This Post Helpful? 0
  • +
  • -

#13 jayveeeeeee  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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 .
Was This Post Helpful? 0
  • +
  • -

#14 TwoOfDiamonds  Icon User is offline

  • D.I.C Regular

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

Re: rrange the numbers into lowest to highest

Posted 10 December 2012 - 07:56 AM

View Postjayveeeeeee, 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 :)
Was This Post Helpful? 0
  • +
  • -

#15 jayveeeeeee  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1