bsc

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

assignment-s-16.c (1621B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 21 Dec 2025
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* Write a program that reads 10 integers into an array (using pointers), and prints the
      9 array in ascending and descending order. */
     10 
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 
     14 #define true 1
     15 #define false 0
     16 
     17 void input_arr(int *, int);
     18 void print(int *, int);
     19 
     20 int main()
     21 {
     22     int arr[10];
     23     input_arr(arr, 10);
     24     print(arr, 10);
     25     return 0;
     26 }
     27 
     28 void input_arr(int *arr, int n)
     29 {
     30     int i;
     31     for (i = 0; i < n; i++)
     32     {
     33         printf("Enter element %d: ", i + 1);
     34         scanf("%d", arr + i);
     35     }
     36 }
     37 
     38 void print(int *arr, int n)
     39 {
     40     int i, j, isSwaped = true, backup;
     41     int *temp = (int *)malloc(n * sizeof(int));
     42     if (temp == NULL)
     43     {
     44         printf("\nMemory Allocation Failed.");
     45         return;
     46     }
     47     for (i = 0; i < n; i++)
     48     {
     49         *(temp + i) = *(arr + i);
     50     }
     51     for (i = 0; i < n - 1 && isSwaped == true; i++)
     52     {
     53         isSwaped = false;
     54         for (j = 0; j < n - i - 1; j++)
     55         {
     56             if (*(temp + j) > *(temp + j + 1))
     57             {
     58                 backup = *(temp + j);
     59                 *(temp + j) = *(temp + j + 1);
     60                 *(temp + j + 1) = backup;
     61                 isSwaped = true;
     62             }
     63         }
     64     }
     65     printf("\nAscending Order:");
     66     for (i = 0; i < n; i++)
     67     {
     68         printf(" %d", *(temp + i));
     69     }
     70     printf("\nDescending Order:");
     71     for (i = n - 1; i >= 0; i--)
     72     {
     73         printf(" %d", *(temp + i));
     74     }
     75     free(temp);
     76 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror