Last active
October 10, 2018 20:58
-
-
Save gozeloglu/046d7559393388b77a3a0c9e6c70ff0c to your computer and use it in GitHub Desktop.
Reverse operation is being implemented in C language with using function and pointers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* author: Gökhan Özeloğlu | |
* | |
* date: 10.10.2018 | |
* | |
* code: reverse operation in C language | |
* | |
* email: [email protected] | |
*/ | |
#include <stdio.h> | |
int* reverse_array(int a, int *array) | |
{ | |
int count = a / 2; | |
for (int i = 0; i < count; i++) | |
{ | |
int tmp = array[i]; // Stores in temp variable to not losing after assign operation | |
array[i] = array[a-i-1]; // Assigns the last element to first element | |
array[a-i-1] = tmp; // Assigns the temp element to last element | |
} | |
return array; | |
} | |
// Prints the array | |
void print_array(int a, int *array) | |
{ | |
printf("["); | |
for (int i = 0; i < a; ++i) | |
{ | |
if (i == a-1) { | |
printf("%d", array[i]); | |
} else { | |
printf("%d, ", array[i]); | |
} | |
} | |
printf("]\n"); | |
} | |
int main(int argc, char const *argv[]) | |
{ | |
printf("Array size: "); | |
int a; | |
scanf("%d", &a); | |
int array[a]; | |
printf("Elements: "); | |
for (int i = 0; i < a; i++) | |
{ | |
scanf("%d", &array[i]); | |
} | |
printf("Original array: "); | |
print_array(a, array); | |
printf("Reversed array: "); | |
reverse_array(a, array); | |
print_array(a, array); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment