(Pointer, array and function) Write a C program that reads a float array with M
ID: 3845257 • Letter: #
Question
(Pointer, array and function) Write a C program that reads a float array with M elements (M is read from the user and must be even). Print the original array (2 decimal places per value). Split the original array into two, one contains first half and the other contains the second half (but in reverse order). Print the two arrays on the screen. Output example: Array size: 6 Element[0]: 23.1 Element[1]: 14.6 Element [2]: 10.3 Element[3]: 5.9 Element[4]: 22.4 Element[5]: 33.9 Array: 23.1 14.6 10.3 5.9 22.4 33.9 Array1: 23.1 14.6 10.3 Array 2: 33.9 22.4 5.9Explanation / Answer
#include <iostream>
using namespace std;
int main() {
// your code goes here
int m;
int i;
int j,k;
do{
cout<<"enter array size"<<endl;
cin>>m;
}while(m%2==0);
float arr[m];
int mid=m/2;
float arr2[mid];
float arr3[mid];
cout<<"enter elements in array:"<<endl;
for(i=0;i<m;i++){
cin>>arr[i];
cout<<arr[i]<<" ";
}
cout<<"first half array data:"<<endl;
for(j=0;j<mid;j++){
arr2[j]=arr[j];
cout<<arr2[j]<<" ";
}
cout<<"second half array data in reverse order:"<<endl;
for(k=m-1;k>=mid;k--){
arr3[k]=arr[k];
cout<<arr3[k]<<" ";
}
return 0;
}
output:
enter array size 10
enter elements in array:
1.2 2.3 3.1 4.6 5.3 6.2 7.1 8.1 9.2 10.1 first half array data:
1.2 2.3 3.1 4.6 5.3 second half array data in reverse order:
10.1 9.2 8.1 7.1 6.2
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.