Write a program in c that reverses the order of the elements of an array. a) Sto
ID: 3930655 • Letter: W
Question
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 arrayExplanation / Answer
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 ", 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 ", a[i]);
}
return (0);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.