Write a function called read floats(int n) that reads n float values from the user into an array and return the array back to the caller. Recall that a regular array will get deallocated when the function returns. (C++ the simplier the better)
#include <iostream> using namespace std; float *read_floats(int n); int main() { float *arr = read_floats(5); for (int i = 0; i < 5; ++i) { cout << arr[i] << " "; } cout << endl; delete[] arr; return 0; } float *read_floats(int n) { cout << "Enter " << n << " numbers: "; // ask for n numbers float *arr = new float[n]; // create a dynamic array, so that it can be returned for (int i = 0; i < n; ++i) { // loop n times cin >> arr[i]; // read and store a number } return arr; // return the array }
Get Answers For Free
Most questions answered within 1 hours.