C program to reverse numbers in an array

Please add functions in the program given in the link below

1 Like

What do you want to do? I mean, what is the objective?

1 Like

Using functions and pointers write a c program to reverse the elements of an integer array,this is the question.But I was only used main function.please help.

Check this one. I have just edited your code. I have used another array to reverse the given array.

You can also reverse an array without any additional array by swapping the elements.


can you say why you used b[size-i-1]=a[i] *refer7*
why you used rev(arr,size)

in the program.Please explain that mathematically or logically.Thanks in advance.

for i=0 to size-1

b[size-i-1]=a[i]

This means,

b[size-1]=a[0], b[size-2]=a[1], b[size-3]=a[2], b[size-4]=a[3],… b[1]=a[size-2], b[0]=a[size-1]

Hare we are copying the elements of a[] to b[] in a reverse order. You should use a paper and dry run the code. I hope that will help you.

In the function rev() I have passed two parameters, the 1st one is the given array to reverse it and the second one is the size of the array.

1 Like

About 50viewers for this question,none other than you gived an answer.Thanks for your help.

No problem. You are always welcome :slight_smile:

1 Like

Ops sorry i am also trying this function, but unable to get expected results. trying again.

hey hi,
I have just used pointers and function to enter and reverse the array as you asked, not print the reverse of the array :). the code was not copying properly so i just copied reverse function, entring and main function you can easily figure out on your own.

    void reverse(int a){
	int i;
	int* ptr1;
	int* ptr2,*temp;
	temp = &br[0];
	ptr2 = &ar[a-1];
	ptr1 = &ar[0];
		while(ptr2>=ptr1){
		*temp = *ptr1;
		*ptr1 = *ptr2;
		*ptr2 = *temp;
		ptr1++;
		ptr2--;
		temp++;
       }

Algorithm to reverse an array by swapping elements. Let inputArray is an array of length N, and leftIndex and rightIndex are integer variables.

  • Initialize leftIndex and rightIndex with index of first and last element of inputArray respectively.
  • leftIndex = 0; and rightIndex = N - 1;.
  • Swap inputArray[leftIndex] and inputArray[rightIndex].
  • Now, Increment leftIndex (leftIndex++) and decrement rightIndex(rightIndex–).
  • Repeat last two steps, until leftIndex < rightIndex.

Check this C program to reverse the elements of array.

include

main()
{
int a[10],n,i,l,r,t;
printf(“Enter n value”);
scanf("%d",&n);
printf(“Enter elements in the array:”);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
l=0;
r=n-1;
for(i=0;i<n;i++)
{
if(l<r)
{
t=a[l];
a[l]=a[r];
a[r]=t;
}
l++;
r–;
}
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
}

#include<stdio.h>
void main()
{
int a[20],i,n;
printf(“Enter the size of your array: \n”);
scanf("%d",&n);
printf(“Enter the array elements :\n”);
for(i=0;i<n;i++)
scanf("%d"a[i])
printf(“The array in reversed order is:\n”);
for(i=n;i>=0;i–)
printf(" %d ",a[i]);

}

you just traverse half array (n/2) and swap(a[i],a[n-i]){1-indexing} and it’s done for 0-indexing a[n-1-i] instead of a[n-i].useful(upvote)