|
| 1 | + |
| 2 | +/* |
| 3 | + Humza KHawar |
| 4 | + Lab Task |
| 5 | + Reversing Arrays using Pointors |
| 6 | +*/ |
| 7 | + |
| 8 | +#include<stdio.h> |
| 9 | + |
| 10 | +void Input_Function(int*, int); //Function to take input using pointors |
| 11 | +void Reverser(int*, int); |
| 12 | +void Output_Function(int*, int); //Function to print output using pointors |
| 13 | + |
| 14 | + |
| 15 | +void main() |
| 16 | +{ |
| 17 | + //initiallization |
| 18 | + int arr_main[100] = { 0 }, numbers; |
| 19 | + |
| 20 | + //taking the number of elements from user |
| 21 | + printf("\nEnter Number of elements : "); |
| 22 | + scanf("%d", &numbers); |
| 23 | + //passing adress of array to input function |
| 24 | + Input_Function(arr_main, numbers); |
| 25 | + |
| 26 | + //passing adress of array to output function |
| 27 | + Output_Function(arr_main, numbers); |
| 28 | + |
| 29 | + |
| 30 | + printf("\nReversing Your Array"); |
| 31 | + //reversing the array |
| 32 | + Reverser(arr_main, numbers); |
| 33 | + Output_Function(arr_main, numbers); |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | + |
| 38 | +void Input_Function(int* arr,int n) { |
| 39 | + //loop to take input |
| 40 | + for (int i = 0; i < n; i++) |
| 41 | + { |
| 42 | + printf("\n Enter Number %d : ", i + 1); |
| 43 | + scanf("%d", arr + i); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | + |
| 48 | +void Output_Function(int* arr, int n) { |
| 49 | + //loop to print output input |
| 50 | + printf("\n Your Array is : "); |
| 51 | + for (int i = 0; i < n; i++) |
| 52 | + { |
| 53 | + printf(" %d ", *(arr + i)); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | +} |
| 58 | + |
| 59 | +void Reverser(int* arr, int n) |
| 60 | +{ //swapping first and last elements |
| 61 | + int* first = arr; |
| 62 | + int* last = arr + (n - 1); |
| 63 | + while (first < last) |
| 64 | + { |
| 65 | + int temp = *first; |
| 66 | + *first = *last; |
| 67 | + *last = temp; |
| 68 | + first++; |
| 69 | + last--; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | +} |
0 commit comments