The Program in C that reads a float array and reverse this array is given below:
#include <stdio.h>
void reverse_array(float arr[], int size) {
int i;
for (i = 0; i < size / 2; i++) {
float temp = arr[i];
arr[i] = arr[size - i - 1];
arr[size - i - 1] = temp;
}
}
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
float arr[n];
printf("Enter the elements of the array: ");
for (i = 0; i < n; i++) {
scanf("%f", &arr[i]);
}
reverse_array(arr, n);
printf("The reversed array is: ");
for (i = 0; i < n; i++) {
printf("%.2f ", arr[i]);
}
printf("\n");
return 0;
}
Output:
Enter the number of elements: 3
Enter the elements of the array: 99
66
100
The reversed array is: 100.00 66.00 99.00
Pro-Tips💡
This program prompts the user to enter the number of elements in an array, then uses a for loop to read the elements of the array.
After that, it calls a function named reverse_array()
that takes the array and its size as its arguments. In the function, it uses a for loop to swap the first element with the last element, the second element with the second-last element, and so on.
After the function call, it uses another for loop to print the reversed array. It is worth noting that the function reverse_array()
modifies the original array,
so if you wish to keep the original array you can copy the original array before passing it to the function.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.