(C++) Write a function that takes an input parameter an int and returns nothing. It then generates an array of random numbers the length of the int parameter. It fills the array with random numbers between 0 and 50. It then prints out the array. Without creating a new array, the function then reverses the array and prints out the reversed array. So if the first array was: 22,10,8,5,3,18 The reversed array would be: 18,3,5,8,10,22 Note: the numbers in the array must be reversed. Don’t just print out the array backwards
#include <iostream> #include <cstdlib> #include <ctime> using namespace std; void randArray(int size){ int* arr = new int{size}; for(int i = 0;i<size;i++){ arr[i] = rand()%50; } for(int i = 0;i<size;i++){ cout<<arr[i]<<" "; } cout<<endl; int j = 0, k =size-1, t; while(j<k){ t = arr[j]; arr[j] = arr[k]; arr[k] = t; j++; k--; } for(int i = 0;i<size;i++){ cout<<arr[i]<<" "; } cout<<endl; } int main(){ randArray(5); return 0; }
Get Answers For Free
Most questions answered within 1 hours.