Q:

Write a C Program to Sort Infinite Numbers in Ascending Order

0

Write a C Program to Sort Infinite Numbers in Ascending Order. Here’s simple Program to Sort Infinite Numbers in Ascending Order using pointers in C Programming Language.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

What are Pointers?


A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.

The general form of a pointer variable declaration is −

  • type *var-name;

Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.

The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.

 
 
 

PROGRAM : :

In this program we are reading infinite numbers and then arranging them in ascending order. Here infinite means, the program should read numbers until a particular number is entered to terminate the reading process.

In our case we are using -1 to stop the reading process. As we already don’t know how many numbers will be entered by the user so we are using concept of dynamic memory allocation to allocate memory for a number at the run time.


Below is the source code for C Program to Sort Infinite Numbers in Ascending Order using pointers which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :

/*  C Program to Sort Infinite Numbers in Ascending Order using pointers  */

#include<stdio.h>
#include<stdlib.h>

int main()
{
 int *p,*q,i=1,j,k,temp;  //q for storing address of 1st number
 printf("Enter infinite Numbers(-1 to stop reading) :: \n");

 printf("\nEnter %d Number :: ",i);
 p=(int*)malloc(sizeof(int));
 scanf("%d",&p[0]);

 while(p[i-1]!=-1) //read until -1 is entered
 {
  i++;

  p=(int*)realloc(p,sizeof(int)*i);
  q=p;
  printf("\nEnter %d Number :: ",i);
  scanf("%d",&p[i-1]);
 }

 p=q;

 //sorting numbers using bubble sort
 for(j=1;j<i;++j)
 {
  for(k=0;k<i-j-1;++k)
  {
   if(p[k]>p[k+1])
   {
    temp=p[k];
    p[k]=p[k+1];
    p[k+1]=temp;
   }
  }
 }

 printf("\n");

 printf("\nAfter Sorting, Numbers are :: ");
 for(j=0;j<i-1;++j)
 {
  printf(" %d",p[j]);
 }

 printf("\n");

 return 0;
}

Output  : :


/*  C Program to Sort Infinite Numbers in Ascending Order using pointers  */

Enter infinite Numbers(-1 to stop reading) ::

Enter 1 Number :: 3

Enter 2 Number :: 1

Enter 3 Number :: 5

Enter 4 Number :: 2

Enter 5 Number :: 4

Enter 6 Number :: 7

Enter 7 Number :: 6

Enter 8 Number :: 9

Enter 9 Number :: 8

Enter 10 Number :: 0

Enter 11 Number :: -1


After Sorting, Numbers are ::  0 1 2 3 4 5 6 7 8 9

Process returned 0

Above is the source code for C Program to Sort Infinite Numbers in Ascending Order using pointers which is successfully compiled and run on Windows System.The Output of the program is shown above .

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C Pointer Solved Programs – C Programming

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a C program to Swap two numbers using pointe... >>
<< C Program to Find Largest Number Using Dynamic Mem...