Assume that each aray in the problems below has exactly 20 columns. Write functi
ID: 3725298 • Letter: A
Question
Assume that each aray in the problems below has exactly 20 columns. Write functions that perform the following operations on 2D arrays:
Initialize an array with a series of random integer values from A to B where A and B are passed into the function. e.g. initialize(numbers, rows, cols, startval, endval);
Print out an array with any number of rows and 20 columns of integer values. The number of rows and cols are passed to the function.
Given an input range of integer values from M to N find and print to the screen all values that fall within that range along with their indices. For example: call the function to print out all values between 30 and 45.
Write ONE function that will compute and return the maximum, minimum and average values for a 2D array. (You will have to use three reference variables to return the three nmbers)
Write a main program that tests each function by calling it with several test cases. For example:
init(numbers, 10, 20, 30, 45) where the array is called numbers, it has 10 rows and 20 columns. The random numbers should be between 30 and 45.
Explanation / Answer
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include<ctime>
using namespace std;
int maximum(int **ar, int row, int col){
int max = ar[0][0];
int i=0, j=0;
for(i=0;i<row; i++){
for(j=0; j<col; j++){
if(max < ar[i][j])
max = ar[i][j];
}
}
return max;
}
int minimum(int **ar, int row, int col){
int min = ar[0][0];
int i=0, j=0;
for(i=0;i<row; i++){
for(j=0; j<col; j++){
if(min > ar[i][j])
min = ar[i][j];
}
}
return min;
}
void printArray(int **ar, int rows, int column){
int i=0, j=0;
for(i=0; i<rows; i++){
cout<<" ";
for(j=0; j<column; j++){
cout<<ar[i][j]<<" ";
}
}
}
float average(int **ar, int row, int col){
int sum =0, count = 0;
int i=0, j=0;
for(i=0;i<row; i++){
for(j=0; j<col; j++){
sum = sum + ar[i][j];
count = count+1;
}
}
return sum/count;
}
int** initialize(int numbers, int rows, int cols, int startval, int endval){
int** arr;
int i=0, j=0;
srand(time(0));
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
arr[i][j] = rand() % (endval - startval) + startval;
}
}
return arr;
}
int main()
{
int rows, number, column = 20;
int low, high;
int i=0, j=0;
cout<<" Enter number of rows: ";
cin>>rows;
cout<<" Enter number of rows to print: ";
cin>>number;
cout<<" Enter lower range of number to be generated: ";
cin>>low;
cout<<" Enter high range of number to be generated: ";
cin>>high;
int **ar = initialize(number, rows, column, low, high);
cout<<" Enter lower range of number to found: ";
cin>>low;
cout<<" Enter higher range of number to found: ";
cin>>high;
int count = 0;
for(i=0; i<rows; i++){
for(j=0; j<column; j++){
if (ar[i][j] >= low && ar[i][j] <= high)
count = count + 1;
}
}
printArray(ar, rows, column);
int max = maximum(ar, rows, column);
int min = minimum(ar, rows, column);
float avg = average(ar, rows, column);
cout<<" ";
cout<<" Maximum Element is: "<<max;
cout<<"Minimum element is: "<<min;
cout<<"Average of array element is "<<avg;
return 0;
}