Skip to content

Instantly share code, notes, and snippets.

@naemazam
Last active March 7, 2022 09:31
Show Gist options
  • Save naemazam/e595ff41bed440bba18e151589f88205 to your computer and use it in GitHub Desktop.
Save naemazam/e595ff41bed440bba18e151589f88205 to your computer and use it in GitHub Desktop.
define a function reverse() to reverse array elements and take input then call reverse() in main().
// ALGORITHM:
STEP 1: START
STEP 2: INITIALIZE arr[] = {1, 2, 3, 4, 5}
STEP 3: length= sizeof(arr)/sizeof(arr[0])
STEP 4: PRINT "Original Array:"
STEP 5: REPEAT STEP 6 and STEP 7 UNTIL i<length
STEP 6: PRINT arr[i]
STEP 7: i=i+1
STEP 8: PRINT new line.
STEP 9: PRINT "Array in reverse order"
STEP 10: SET i=length-1. REPEAT STEP 11 and STEP 12 UNTIL i>=0
STEP 11: PRINT a[i]
STEP 12: i=i-1
STEP 13: RETURN 0.
STEP 14: END
//
#include<stdio.h>
/* Function to reverse arr[] from start to end*/
void rvereseArray(int arr[], int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
/* Utility that prints out an array on a line */
void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
/* Driver function to test above functions */
int main()
{
int arr[10];
printf("please Input 10 values");
for(int i = 0; i < 10; ++i) {
scanf("%d", &arr[i]);
}
int n = sizeof(arr) / sizeof(arr[0]);
printf("Input Value:\n");
printArray(arr, n);
rvereseArray(arr, 0, n-1);
printf("Reversed array is \n");
printArray(arr, n);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment