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