Ascending order in C program

any number you input the output is ascending order

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 12478 Views - Last Post: 16 September 2009 - 06:21 PM Rate Topic: -----

#1 jessica89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 09

Ascending order in C program

Posted 16 September 2009 - 01:47 AM

Any number you input the output is ascending order.

This is my program


#include<stdio.h>
void main(main)
{
int first,second,third;

clrscr();

printf("Input first integer:");
scanf("%d",&first);
printf("Input second integer:");
scanf("%d",&second);
printf("Input third integer:");
scanf("%d",&third);

if(first>second)

if(second>third)

if(first>third)

printf("The number is ascending order:");
printf("%d,%d and %d");

getch();

}

This post has been edited by JackOfAllTrades: 12 January 2012 - 03:47 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Ascending order in C program

#2 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Ascending order in C program

Posted 16 September 2009 - 02:11 AM

You might try in the future making and organizing your code to be more readable.

#include<stdio.h>
void main(main)
{
  int first,second,third;

  clrscr();

  printf("Input first integer:");
  scanf("%d",&first);

  printf("Input second integer:");
  scanf("%d",&second);

  printf("Input third integer:");
  scanf("%d",&third);

  if(first>second)
  {
	if(second>third)
	{
	  if(first>third)
	  {
		printf("The number is ascending order:");
		printf("%d,%d and %d");
	  }/*else 1*/
	}/*else 2*/
  }/*else 3*/

} 


At the different else statements, you can printout error messages that will say "First number is smaller that the second! Not ascending!" and such.

Adding the braces to if statements organizes what is to execute if the statement is triggered. By adding them in there, only if it is in ascending order does it print out "The number is ascending order:" and the three numbers.

Make sense? I hope this got rid of whatever output errors you were having.
Was This Post Helpful? 0
  • +
  • -

#3 AntonWebsters  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 88
  • View blog
  • Posts: 428
  • Joined: 15-August 09

Re: Ascending order in C program

Posted 16 September 2009 - 02:31 AM

I don't know C, so I can't help you with it...but this would help you a bit if you understand C++.
#include <iostream>
using namespace std;
int main(void)
{
int first,second,third;

cout << "Enter first integer: ";
cin >> first;
cout << "Enter second integer: ";
cin >> second;
cout << "Enter third integer: ";
cin >> third;

if(first>second){
	int temp = first;
	first = second;
	second = temp;
}
if(second>third){
	int temp = second;
	second = third;
	third = temp;
}

if(first>third){
	int temp = first;
	first = third;
	third = temp;
}

cout << "The number is ascending order:" << endl;;
cout << first << " , " << second << " , " << third << endl;
return 0;
}



Take a look at the if statements, modify them and implement them inside your code.
Was This Post Helpful? 0
  • +
  • -

#4 jessica89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 09

Re: Ascending order in C program

Posted 16 September 2009 - 02:59 AM

#include<stdio.h>
void main(main)
{
  int first,second,third;

  clrscr();

  printf("Input first integer:");
  scanf("%d",&first);

  printf("Input second integer:");
  scanf("%d",&second);

  printf("Input third integer:");
  scanf("%d",&third);


if(first>second)
{
    int temp = first;
    first = second;
    second = temp;
}
if(second>third)
{
    int temp = second;
    second = third;
    third = temp;
}

if(first>third)
{
    int temp = first;
    first = third;
    third = temp;
}


  
        printf("The number is ascending order:");
        printf("%d,%d and %d");
      
getch();

}

Was This Post Helpful? 0
  • +
  • -

#5 jessica89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 09

Re: Ascending order in C program

Posted 16 September 2009 - 04:13 AM

I have an assignment that any number you input the output is ascending order

#include<stdio.h>

void main(main)
{
int first,second,third;

clrscr();

printf("Input first integer:");
scanf("%d",&first);

printf("Input second integer:");
scanf("%d",&second);

printf("Input third integer:");
scanf("%d",&third);


if(first>second)
{
int temp = first;
first = second;
second = temp;
}
if(second>third)
{
int temp = second;
second = third;
third = temp;
}

if(first>third)
{
int temp = first;
first = third;
third = temp;
}



printf("The number is ascending order:");
printf("%d,%d and %d");

getch();

} 


When i input,

first integer:3
second integer:2
third integer:1

My output is The number is ascending order:3,2 and 1

The output should be, The number is ascending order:1,2and 3

*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 16 September 2009 - 07:07 AM

Was This Post Helpful? 0
  • +
  • -

#6 Ter13  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 30
  • Joined: 05-September 09

Re: Ascending order in C program

Posted 16 September 2009 - 04:33 AM

You might want to use an array as a storage device instead.

http://mathbits.com/...ays/Sorting.htm

use code tags in the future please.

This post has been edited by Ter13: 16 September 2009 - 04:34 AM

Was This Post Helpful? 0
  • +
  • -

#7 Crunch  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 139
  • View blog
  • Posts: 1,222
  • Joined: 28-July 09

Re: Ascending order in C program

Posted 16 September 2009 - 04:35 AM

the last comparison should be


if(first>second)
{
int temp = first;
first = second;
second = temp;
}



Was This Post Helpful? 0
  • +
  • -

#8 HELPME!  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 70
  • Joined: 06-April 09

Re: Ascending order in C program

Posted 16 September 2009 - 04:47 AM

try using insertion sort

#include<stdio.h>

#define N 3

int main( void)
{
	int number[N], temp, wallIndex, index, i;
	
	printf("Input first integer:");
	scanf("%d",&number[0]);
	
	printf("Input second integer:");
	scanf("%d",&number[1]);
	
	printf("Input third integer:");
	scanf("%d",&number[2]);
	
	while ( wallIndex < N ) 
	{
		temp = number[wallIndex];
		index = wallIndex;
		
		while ( ( index > 0 ) && ( temp < number[index - 1] ) )
		{
			number[index] = number[index - 1];
			index--;
		}
		number[index] = temp;
		wallIndex++;
	}	
		
		
		
	printf("The number is ascending order:");
	for ( i=0; i<N; i++)
		printf("Number %d = %d\n", (i+1), number[i]);

} 


Was This Post Helpful? 0
  • +
  • -

#9 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5932
  • View blog
  • Posts: 12,855
  • Joined: 16-October 07

Re: Ascending order in C program

Posted 16 September 2009 - 09:28 AM

You're pretty close. Use a function for the swap so you don't make typing errors. For just entering three digits, ignore suggestions of complex sorts. You are essentially doing an in place insertion sort of three elements. You don't need a loop or an array, you just need to not be confused by your variables.

#include<stdio.h>

// make a function to swap number, much easier to read in your main
void swap(int *n1, int *n2) {
	int temp = *n1;
	*n1 = *n2;
	*n2 = temp;
}

int main() {
	int first,second,third;

	printf("Input first integer:");
	scanf("%d",&first);

	printf("Input second integer:");
	scanf("%d",&second);
	// start swaping now, avoid the rush
	if(second<first) { swap(&second, &first); }

	printf("Input third integer:");
	scanf("%d",&third);
	// find the right place for the value just entered and you're done
	if(third<second) { 
		swap(&third, &second);
		if(second<first) { swap(&second, &first); }
	}

	printf("The number is ascending order:");

	// don't forget to actually put the numbers in
	printf("%d,%d and %d\n", first,second,third);

	return 0;
}



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#10 jessica89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 09

Re: Ascending order in C program

Posted 16 September 2009 - 03:20 PM

This is my assignment need help.
Any number you input the output is ascending order.

This is my program

#include<stdio.h>
void main(main)
{
int first,second,third;

clrscr();

printf("Input first integer:");
scanf("%d",&first);
printf("Input second integer:");
scanf("%d",&second);
printf("Input third integer:");
scanf("%d",&third);

  if(first>second)
  {
	if(second>third)
	{
	  if(first>third)
	  {
		printf("The number is ascending order:");
		printf("%d,%d and %d");
	  }
	}
  }
getch();

}


*mod Edit: Added code tags: :code:

This post has been edited by NickDMax: 16 September 2009 - 03:40 PM

Was This Post Helpful? 0
  • +
  • -

#11 Mowgef  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 245
  • Joined: 01-May 09

Re: Ascending order in C program

Posted 16 September 2009 - 03:25 PM

Please don't continuously post this topic after you have made others.

http://www.dreaminco...topic126161.htm

http://www.dreaminco...topic126145.htm

This post has been edited by Mowgef: 16 September 2009 - 03:25 PM

Was This Post Helpful? 0
  • +
  • -

#12 Tyler401  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 16-September 09

Re: Ascending order in C program

Posted 16 September 2009 - 03:34 PM

#include <iostream>

using namespace std;

int main() {

int a,b,c;

cout << "Input first number.\n";
cin >> a;
cout << "Input second number.\n";
cin >> b;
cout << "Input third number.\n";
cin >> c;
cout << "\n";

cout << c << "\n" << b << "\n" << a <<"\n";
system("PAUSE");

return 0;
}



that will do what you want... kinda rinkidy-dink, but it works...
Was This Post Helpful? 0
  • +
  • -

#13 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Ascending order in C program

Posted 16 September 2009 - 03:43 PM

* Merged all three topics *
Was This Post Helpful? 0
  • +
  • -

#14 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Ascending order in C program

Posted 16 September 2009 - 04:02 PM

I don't get it -- your posts seem to go backwards after a point.. you make progress and then you revert back to your original code... this is not hard. If you can sort two items then you can sort thre 3 using this:

sort2( a, b ) // put a and b in order
sort2( b, c ) // see if c < b -- if it is that there is a swap...
sort2( a, b ) // so we need to check to see if a,b are still in order... (because there may have been a swap).


so putting it all together we have this:
#include<stdio.h>

void sort2(int *a, int *b) {
	if (*a > *b) {
		int temp = *b;
		*b = *a;
		*a = temp;
	}   
}

void sort3(int *a, int *b, int *c) {
	sort2(a, b);
	sort2(b, c);
	sort2(a, b);
}


int main() {
	int first,second,third;
	printf("Input first integer:");
	scanf("%d",&first);

	printf("Input second integer:");
	scanf("%d",&second);

	printf("Input third integer:");
	scanf("%d",&third);

	sort3(&first, &second, &third);

	printf("The number is ascending order: %d,%d and %d", first, second, third);

	return 0;

}


The first function sort2 just checks two inputs a and b and swaps them if a > b -- no mystery there.

The second function just used the first function to extend things to 3 numbers.
Was This Post Helpful? 0
  • +
  • -

#15 jessica89  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 16-September 09

Re: Ascending order in C program

Posted 16 September 2009 - 04:33 PM

#include<stdio.h>
void main(main)
{
  int temp,first,second,third;

  clrscr();

  printf("Input first integer:");
  scanf("%d",&first);

  printf("Input second integer:");
  scanf("%d",&second);

  printf("Input third integer:");
  scanf("%d",&third);


if(first>second)
{
    int temp = first;
    first = second;
    second = temp;
}
if(second>third)
{
    int temp = second;
    second = third;
    third = temp;
}

if(first>third)
{
    int temp = first;
    first = third;
    third = temp;
}


  
        printf("The number is ascending order:");
        printf("%d,%d and %d");
      
getch();

}


*** MOD EDIT: Added code tags. Please :code: ***

This post has been edited by JackOfAllTrades: 16 September 2009 - 05:47 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2