Write a program to randomly fill an array of float of 10 elements (use the rand() function and make sure to use #include ) in main then define a function to find the maximum element in the array using pointers and return the max element to main then print it on screen.

Answer :

Answer:

#include <iostream>

#include <stdlib.h>

#include <time.h>

using namespace std;

// array fill and returnig array to the main()

float *arrayFill(float *arr) {

// srand() allows generate new random value everytime the program runs

srand(time(NULL));

for (int i = 0; i < 10; i++)

{

 // array fill with random number between 0 to 100

 arr[i] = (rand() % 100);

}

return arr;

}

// print array

void print(float* arr) {

cout << "Array:  ";

for (int i = 0; i < 10; i++)

{

 cout << arr[i] << "   ";

 

}

}

float maxNum(float* arr) {

float temp = arr[0];

for (int i = 1; i < 10; i++) {

 if (temp < arr[i]) {

  temp = arr[i];

 }

}

return temp;

}

int main() {

// creating dynamic array of elements 10 in heap memory

float *arrPtr = new float[10];

float result = 0;

// calling arrayFill()

arrPtr = arrayFill(arrPtr);

// calling print() to print array

print(arrPtr);

// calling maxNum() to find maximum number in the array

result = maxNum(arrPtr);

cout << "\nmaximum number: " << result;

delete[] arrPtr;

return 0;

}

Explanation:

please read inline comments inside the code section:)

Other Questions