Reverse an Array in C
Q. Write a C program for Reverse an array.
#include
#include
void main()
{
int a[25],b[25], size, i,j;
printf("Enter number of elements in array: ");
scanf("%d", &size);
printf("Enter the integers: ");
for (i = 0; i < size; i++)
{
scanf("%d", &a[i]);
}
printf("Reverse of an array is ");
for(i=0,j=size-1;i <size;i++,j--)
{
b[j] = a[i];
}
for(i=0;i<size;i++)
{
printf("%d ", b[i]);
}
getch();
}
Q. Write an algorithm to Reverse an array in C.
1. Start
2. Input the size of the array `size`.
3. Initialize two arrays `a[25]` and `b[25]`.
4. Read `size` integers into array `a` from index `0` to `size-1`.
5. For `i` from `0` to `size - 1` and `j` from `size - 1` down to `0` (simultaneously):
- Assign `b[j] = a[i]`
- Increment `i` by 1
- Decrement `j` by 1
6. For `i` from `0` to `size - 1`:
- Print `b[i]` (elements of the reversed array)
7. End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com