Skip to content

Instantly share code, notes, and snippets.

@gozeloglu
Last active October 10, 2018 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gozeloglu/046d7559393388b77a3a0c9e6c70ff0c to your computer and use it in GitHub Desktop.
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.
/*
* author: Gökhan Özeloğlu
*
* date: 10.10.2018
*
* code: reverse operation in C language
*
* email: gozeloglu@gmail.com
*/
#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