Write a program in c that reverses the order of the elements

Write a program in c that reverses the order of the elements of an array.
a) Store the reversed elements in a new array
b) Store the reversed elements in the same array
Write a program in c that reverses the order of the elements of an array.
a) Store the reversed elements in a new array
b) Store the reversed elements in the same array
Write a program in c that reverses the order of the elements of an array.
a) Store the reversed elements in a new array
b) Store the reversed elements in the same array

Solution

a)

#include <stdio.h>

int main() {
int a[30], i, j, num,k;
int b[30];
printf(\"\ Enter no of elements : \");
scanf(\"%d\", &num);

//Read elements in an array
for (i = 0; i < num; i++) {
scanf(\"%d\", &a[i]);
}

j = i - 1; // j will Point to last Element
for (k = j,i=0;k>=0&&i<num; i++,k--) {
b[i]=a[k];
}
//Prints the Result after reversing in new array
printf(\"\ Result after reversal : \");
for (i = 0; i < num; i++) {
printf(\"%d \\t\", b[i]);
}

return (0);
}

b)

# include<stdio.h>

int main() {
int a[30], i, j, n, temp;

printf(\"\ Enter no of elements : \");
scanf(\"%d\", &n);

//Read elements in an array
for (i = 0; i < n; i++) {
scanf(\"%d\", &a[i]);
}

j = i - 1; // j will Point to last Element
i = 0; // i will be pointing to first element

while (i < j) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
i++; // increment i
j--; // decrement j
}

//Prints the Result after reversing in same array
printf(\"\ Result after reversal : \");
for (i = 0; i < n; i++) {
printf(\"%d \\t\", a[i]);
}

return (0);
}

 Write a program in c that reverses the order of the elements of an array. a) Store the reversed elements in a new array b) Store the reversed elements in the s
 Write a program in c that reverses the order of the elements of an array. a) Store the reversed elements in a new array b) Store the reversed elements in the s

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site