-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path04_rotate.c
162 lines (131 loc) · 4.5 KB
/
04_rotate.c
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// // Write a function to rotate an array by n position in d direction. The d is an indicative value for left or right. (For example, if array of size 5 is [32, 29, 40, 12, 70]; n is 2 and d is left, then the resulting array after left rotation 2 times is [40, 12, 70, 32, 29] )
// // Header Files
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
// // Functions Declarations (Prototypes)
void printArray(int[], int);
void inputArray(int[], int);
void rotateArray(int[], int, int, int);
void rotateTowardsLeft(int[], int, int);
void rotateTowardsRight(int[], int, int);
void swap(int *, int *);
// // Main Function Start
int main()
{
const int ARRAY_SIZE;
printf("\nHow Many Elements You Want to Enter => ");
scanf("%d", &ARRAY_SIZE);
// // Check for Invalid Array Size
if (ARRAY_SIZE < 1)
{
puts("\n!!! Invalid Input, Plz Correctly Specify Number of Elements...");
exit(0);
}
// // Declare Array of Variable size
int arr[ARRAY_SIZE], position;
char direction;
// // Input Elements
printf("\nEnter %d Elements => ", ARRAY_SIZE);
inputArray(arr, ARRAY_SIZE);
// // Input Position
printf("\nEnter N to Rotate Array by N Positions => ");
scanf("%d", &position);
// // Check Invalid Input for Position
if (position > ARRAY_SIZE || position < 0)
{
puts("\n!!! Invalid Input, Position Cannot be Greater than Size of Array and Less than 0");
exit(0);
}
// // Input Direction
printf("\nEnter Direction Towards Array Will be Rotated\nFor Left Press L or l\nFor Right Press R or r\n\nEnter Your Choice => ");
fflush(stdin);
scanf("%c", &direction);
// // Check Invalid Input for Direction
if (direction != 'L' && direction != 'l' && direction != 'R' && direction != 'r')
{
puts("\n!!! Invalid Input, Direction is not valid...");
exit(0);
}
// // Print Array
puts("\n\n>>>>>>>> Elements Before Rotating <<<<<<<<<");
printArray(arr, ARRAY_SIZE);
// // sort in descending order
rotateArray(arr, ARRAY_SIZE, position, direction);
// // Print Array
printf("\n\n>>>>>>>> Elements Before Rotating by %d Position Towards %s Direction <<<<<<<<<\n", position, direction == 'L' || direction == 'l' ? "Left" : "Right");
printArray(arr, ARRAY_SIZE);
putch('\n');
getch();
return 0;
}
// // Main Function End
// // Functions Definitions 👇👇
// // Function to Display Array Elements
void printArray(int arr[], int size)
{
putch('\n'); // // Add new line
for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
putch('\n'); // // Add new line
}
// // Function to Input Array Elements
void inputArray(int arr[], int size)
{
for (int i = 0; i < size; i++)
scanf("%d", &arr[i]);
}
// // Function to Rotate An Array by n position towards given Direction
void rotateArray(int arr[], int size, int position, int direction)
{
// // Rotate Towards Left Direction
if (direction == 'l' || direction == 'L')
rotateTowardsLeft(arr, size, position);
else // // Rotate Towards Right Direction
rotateTowardsRight(arr, size, position);
}
// // Function to Rotate An Array by n position towards Left
void rotateTowardsLeft(int arr[], int size, int position)
{
// // 1st Approach
int saveElements[position];
for (int i = 0; i < position; i++)
saveElements[i] = arr[i];
for (int i = 0; i < size - position; i++)
arr[i] = arr[i + position];
for (int i = 0; i < position; i++)
arr[i + size - position] = saveElements[i];
// // 2nd Approach
// // for (int j = 0; j < position; j++)
// // {
// // for (int i = 0; i < size - 1; i++)
// // {
// // swap(&arr[i], &arr[i + 1]);
// // }
// // }
}
// // Function to Rotate An Array by n position towards Right
void rotateTowardsRight(int arr[], int size, int position)
{
// // 1st Approach
int saveElements[position];
for (int i = size - position; i < size; i++)
saveElements[i + position - size] = arr[i];
for (int i = size - 1; i > position - 1; i--)
arr[i] = arr[i - position];
for (int i = 0; i < position; i++)
arr[i] = saveElements[i];
// // 2nd Approach
// // for (int j = 0; j < position; j++)
// // {
// // for (int i = size - 1; i > 0; i--)
// // {
// // swap(&arr[i], &arr[i - 1]);
// // }
// // }
}
// // Function to Swap values of Two Variables
void swap(int *a, int *b)
{
*a = (*a + *b) - (*b = *a);
}