bsc

Comprehensive codebase and cou...
Log | Files | Refs | Activity | README | LICENSE

root / semester_1 / practice-c / pc-ip-12.c

pc-ip-12.c (1513B)


      1 /*
      2  * Question 12:
      3  * Write a program that reads 10 integers into an array (using pointers), and prints the array in ascending and descending order.
      4  */
      5 
      6 #include <stdio.h>
      7 
      8 void input(int *);
      9 void bubble_sort(int[], int);
     10 
     11 int main()
     12 {
     13     int arr[10], i, n = 10;
     14     input(arr);
     15     printf("\nGiven Array: ");
     16     printf("[");
     17     for (i = 0; i < n; i++)
     18     {
     19         printf("%d", arr[i]);
     20         if (i < n - 1)
     21         {
     22             printf(", ");
     23         }
     24     }
     25     printf("]");
     26     bubble_sort(arr, n);
     27     printf("\nAscending Order: ");
     28     printf("[");
     29     for (i = 0; i < n; i++)
     30     {
     31         printf("%d", arr[i]);
     32         if (i < n - 1)
     33         {
     34             printf(", ");
     35         }
     36     }
     37     printf("]");
     38     printf("\nDescending Order: ");
     39     printf("[");
     40     for (i = n - 1; i >= 0; i--)
     41     {
     42         printf("%d", arr[i]);
     43         if (i > 0)
     44         {
     45             printf(", ");
     46         }
     47     }
     48     printf("]");
     49     return 0;
     50 }
     51 
     52 void input(int *arr)
     53 {
     54     int i;
     55     for (i = 0; i < 10; i++)
     56     {
     57         printf("Enter element %d: ", i + 1);
     58         scanf("%d", arr + i);
     59     }
     60 }
     61 
     62 void bubble_sort(int arr[], int n)
     63 {
     64     int isSwaped = 1;
     65     int i, j, temp;
     66     for (i = 0; (i < n) && isSwaped; i++)
     67     {
     68         isSwaped = 0;
     69         for (j = 0; j < n - 1 - i; j++)
     70         {
     71             if (arr[j] > arr[j + 1])
     72             {
     73                 temp = arr[j];
     74                 arr[j] = arr[j + 1];
     75                 arr[j + 1] = temp;
     76                 isSwaped = 1;
     77             }
     78         }
     79     }
     80 }
© notamitgamer • Site Built: 2026-09-05 01:53:16 UTC • git-mirror commit: c170d72 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror