Computer Programming C Laboratory: Pointers C Programs

Write a C program to sort a given number using pointer (Using Pointer and Array)

Using Pointers and Arrays - C Programming

Write a C program to sort a given number using pointer.

* C Program to sort a given number using pointer */

#include <stdio.h>

main()

{

int j,k;

int arr[5]; /*arr[5] is a array elements */

int *a;  /* *a is a pointer variable */

a=arr;

for(j=0;j<5;j++)

{

       printf("Enter the number %d="j+1);

       scanf("%d", &arr[j]);

} /* for */

printf("\n Before sorting");

for(j=0;j<5;j++)

         printf("%d\n", arr[j]);

a=arr;

for(j=0;j<5;j++)

for(k=0;k<5‒j−1;k++)

      If (*(arr+k) > *(arr+k+1))

swap (arr+k, arr+k+1); /* function call */

printf ("After sorting");

for (j=0; j<5; j++)

            printf ("%d\n", arr[j]);

} /* main */

swap (b,c)

int *b, *c;   /* *b, *c is a point variables */

{

        int temp;

        temp=*b;

        *b=*c;

        *c=temp;

} /* Swap */

OUTPUT

Enter the number=8

Enter the number=5

Enter the number=7

Enter the number=4

Enter the number=6

Before sorting  8 5 7 4 6

After sorting 4 5 6 7 8

EXPLANATION: The variable arr is assigned to pointer variable a. The body of for loop j and k checks the condition (*(arr+k)>*(arr+k+i). If the condition is true the program control transfer to swap() function. The swap function interchanges the value using third variable temp then control transfer to main program. Finally using for loop j the sorted output is printed.


Computer Programming C Laboratory: Pointers C Programs : Tag: C Programming : - Write a C program to sort a given number using pointer (Using Pointer and Array)


Computer Programming C Laboratory: Pointers C Programs



Under Subject


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation