pc-ip-12.c (1704B)
1 /* 2 * Author : Amit Dutta <amitdutta4255@gmail.com> 3 * Date : 05 Jan 2026 4 * Repo : https://github.com/notamitgamer/bsc 5 * License : MIT License (See the LICENSE file for details) 6 */ 7 8 /* 9 * Question 12: 10 * Write a program that reads 10 integers into an array (using pointers), and prints the array in ascending and descending order. 11 */ 12 13 #include <stdio.h> 14 15 void input(int *); 16 void bubble_sort(int[], int); 17 18 int main() 19 { 20 int arr[10], i, n = 10; 21 input(arr); 22 printf("\nGiven Array: "); 23 printf("["); 24 for (i = 0; i < n; i++) 25 { 26 printf("%d", arr[i]); 27 if (i < n - 1) 28 { 29 printf(", "); 30 } 31 } 32 printf("]"); 33 bubble_sort(arr, n); 34 printf("\nAscending Order: "); 35 printf("["); 36 for (i = 0; i < n; i++) 37 { 38 printf("%d", arr[i]); 39 if (i < n - 1) 40 { 41 printf(", "); 42 } 43 } 44 printf("]"); 45 printf("\nDescending Order: "); 46 printf("["); 47 for (i = n - 1; i >= 0; i--) 48 { 49 printf("%d", arr[i]); 50 if (i > 0) 51 { 52 printf(", "); 53 } 54 } 55 printf("]"); 56 return 0; 57 } 58 59 void input(int *arr) 60 { 61 int i; 62 for (i = 0; i < 10; i++) 63 { 64 printf("Enter element %d: ", i + 1); 65 scanf("%d", arr + i); 66 } 67 } 68 69 void bubble_sort(int arr[], int n) 70 { 71 int isSwaped = 1; 72 int i, j, temp; 73 for (i = 0; (i < n) && isSwaped; i++) 74 { 75 isSwaped = 0; 76 for (j = 0; j < n - 1 - i; j++) 77 { 78 if (arr[j] > arr[j + 1]) 79 { 80 temp = arr[j]; 81 arr[j] = arr[j + 1]; 82 arr[j + 1] = temp; 83 isSwaped = 1; 84 } 85 } 86 } 87 }