ccplusplus.com
Learn C, C++ Concepts
Wednesday, October 5, 2011
sort numbers in ascending or descending order c
/************************************************************ * File : sort-ascending-descending-order.c * Author : Saurabh Gupta * Desc : c program to sort numbers in ascending order * c program to sort numbers in descending order * Source : http://saurabhgupta0527.blogspot.com/p/c.html * Created : PM 01:33 5 October 2011 * Note : ************************************************************/ #include <stdio.h> #include <stdlib.h> void sort(void); int nUserChoice, naElements[20], nTotalElem; int main() { printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf ("XXXXX ascending/descending sorting XXXXXX\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); printf("Enter the no of elements you will enter : "); scanf ("%d", &nTotalElem); printf("(1) Sort in ascending order.\n"); printf("(2) Sort in descending order.\n"); printf("Enter your choice : "); scanf ("%d",&nUserChoice); if (nUserChoice!=1 && nUserChoice!=2) { printf("Not a proper choice, please re-run the program !!!!\n"); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); exit(0); } sort(); printf ("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n"); return 0; } void sort(void) { int n, i, j, temp=0, min, k; printf ("Enter the <%d> elements : ", nTotalElem); for (i=1;i<=nTotalElem;i++) { scanf("%d",&naElements[i]); } for (i=1;i<=(nTotalElem-1);i++) { min=naElements[i]; k=i; for (j=(i+1);j<=nTotalElem;j++) { if (naElements[j]<min) { min=naElements[j]; k=j; } } temp = naElements[k]; naElements[k] = naElements[i]; naElements[i] = temp; } switch(nUserChoice) { case 1: printf("Elements in ascending order are : "); for (i=1;i<=nTotalElem;i++) { printf("%d ",naElements[i]); } printf ("\n"); break; case 2: printf("Elements in descending order are : "); for (i=nTotalElem;i>=1;i--) { printf("%d ",naElements[i]); } printf ("\n"); break; default: printf("ERROR"); } return; } /* * * [sgupta@rhel54x64 c]$ gcc sort-ascending-descending-order.c -o sort-ascending-descending-order [sgupta@rhel54x64 c]$ ./sort-ascending-descending-order XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX ascending/descending sorting XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter the no of elements you will enter : 5 (1) Sort in ascending order. (2) Sort in descending order. Enter your choice : 1 Enter the <5> elements : 2 4 10 8 1 Elements in ascending order are : 1 2 4 8 10 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 c]$ ./sort-ascending-descending-order XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXXXX ascending/descending sorting XXXXXX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Enter the no of elements you will enter : 3 (1) Sort in ascending order. (2) Sort in descending order. Enter your choice : 2 Enter the <2> elements : 2 5 3 Elements in descending order are : 5 3 2 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX [sgupta@rhel54x64 c]$ */
See also
Other popular tricky C Sample Codes and language Concept
.
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment