-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reverser.c
65 lines (51 loc) · 1.68 KB
/
Reverser.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
#include <stdio.h>
// Function to reverse the digits in each number
void reverseDigits(int *numbers, int size) {
for (int i = 0; i < size; i++) {
int reversedNumber = 0;
int originalNumber = numbers[i];
// Reverse the digits
while (originalNumber > 0) {
int digit = originalNumber % 10;
reversedNumber = reversedNumber * 10 + digit;
originalNumber /= 10;
}
// Update the array with reversed number
numbers[i] = reversedNumber;
}
}
// Function to reverse the order of numbers in the array
void reverseArray(int *numbers, int size) {
for (int i = 0; i < size / 2; i++) {
// Swap numbers at positions i and size - i - 1
int temp = numbers[i];
numbers[i] = numbers[size - i - 1];
numbers[size - i - 1] = temp;
}
}
int main() {
int size;
// Input: Get the size of the array from the user
scanf("%d", &size);
// Check if the size is valid
if (size <= 0 || size > 50) {
printf("Invalid size. Please enter a size between 1 and 50\n");
return 1;
}
// Declare an array to represent the encoded numbers
int encodedNumbers[size];
// Input: Get the elements of the array from the user
for (int i = 0; i < size; i++) {
scanf("%d", &encodedNumbers[i]);
}
// Call the reverseDigits function to decode the digits
reverseDigits(encodedNumbers, size);
// Call the reverseArray function to reverse the order of numbers
reverseArray(encodedNumbers, size);
// Display the decoded array
for (int i = 0; i < size; i++) {
printf("%d ", encodedNumbers[i]);
}
printf("\n");
return 0;
}