Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help in writing a function to fill an array and find the max; my code is

ID: 3627679 • Letter: I

Question

I need help in writing a function to fill an array and find the max; my code is at the bottom:

#include <iostream>
#include <iomanip>
using namespace std;
void Square2DArray(int arr1[][15],int,int);
void Print2DArray(int arr1[][15],int,int);
void max_element(int arr1[][15],int,int);

int main()
{
//declare an array for whole numbers that may contain 20 rows and 15 columns
int rows, cols;
int arr1[20][15];
//getting number rows and columns from user
cout<<"Please enter the dimensions of the 2D array."<<endl;
cout<<"Please enter the number of rows you want to use"<<endl;
cin>>rows;

//confirming number of rows are withing bounds
while(rows <= 0 || rows > 20)
{
cout<<"The number of rows must be greater than 0 and less than or equal to 20"<<endl;
cout<<"Please enter the number of rows you want to use"<<endl;
cin>>rows;
}

cout<<"Please enter the number of columns you want to use"<<endl;
cin>>cols;

//confirming number ofcolumns are withing bounds
while(cols <= 0 || cols > 15)
{
cout<<"The number of rows must be greater than 0 and less than or equal to 15"<<endl;
cout<<"Please enter the number of rows you want to use"<<endl;
cin>>cols;
}

Square2DArray(arr1, rows, cols);
Print2DArray(arr1, rows, cols);
max_element(arr1, rows, cols);
system("pause");
//call function to fill array
//call function to print array
//call function to find max

return 0;
}

//function to print a 2D integer array of up to 15 columns
//function accepts the 2D array and the number of rows and columns used.
//function outputs the array
//Pre: Number of rows and columns of the 2D array have been entered and
// confirmed to be valid. The array has been declared and filled.
//Post: The 2D array has been printed to the screen
void Print2DArray(int arr1[][15], int r, int c)
{
int i = 0, j;
while (i < r)
{
j = 0;
while (j < c)
{
cout<<setw(5)<<arr1[i][j];
j++;
}
cout<<endl;
i++;
}
arr1[i][j];
}

void fill_it(int arr1[][15], int r, int c)
{
//Add comments and function to fill array
//Add comments and function to find the max in the array

Explanation / Answer

I didn't get what your square2dArray was supposed to do so I commented it out.

#include <iostream> #include <iomanip> using namespace std; //void Square2DArray(int arr1[][15],int,int); void Print2DArray(int arr1[][15],int,int); int max_element(int arr1[][15],int,int); void fill_it(int arr1[][15], int r, int c); int main() { //declare an array for whole numbers that may contain 20 rows and 15 columns int rows, cols; int arr1[20][15]; //getting number rows and columns from user cout<<"Please enter the dimensions of the 2D array."<<endl; cout<<"Please enter the number of rows you want to use"<<endl; cin>>rows; //confirming number of rows are withing bounds while(rows <= 0 || rows > 20) { cout<<"The number of rows must be greater than 0 and less than or equal to 20"<<endl; cout<<"Please enter the number of rows you want to use"<<endl; cin>>rows; } cout<<"Please enter the number of columns you want to use"<<endl; cin>>cols; //confirming number ofcolumns are withing bounds while(cols <= 0 || cols > 15) { cout<<"The number of columns must be greater than 0 and less than or equal to 15"<<endl; cout<<"Please enter the number of columns you want to use"<<endl; cin>>cols; } fill_it(arr1, rows, cols); // Square2DArray(arr1, rows, cols); Print2DArray(arr1, rows, cols); cout << "Max value is: " << max_element(arr1, rows, cols) << endl; system("pause"); //call function to fill array //call function to print array //call function to find max return 0; } //function to print a 2D integer array of up to 15 columns //function accepts the 2D array and the number of rows and columns used. //function outputs the array //Pre: Number of rows and columns of the 2D array have been entered and // confirmed to be valid. The array has been declared and filled. //Post: The 2D array has been printed to the screen void Print2DArray(int arr1[][15], int r, int c) { int i = 0, j; while (i < r) { j = 0; while (j < c) { cout<<setw(5)<<arr1[i][j]; j++; } cout<<endl; i++; } arr1[i][j]; } void fill_it(int arr1[][15], int r, int c) { for(int i=0;i<r;i++) for(int j=0;j<c;j++) //for each column in each row { cout<<"Please enter value for array element["<<i+1<<"]["<<j+1<<"]:"; //asking values cin>>arr1[i][j]; //filling values } } int max_element(int arr1[][15], int r, int c) { int max = arr1[0][0]; for(int i=0; i<r ; i++) for (int j=0; j<c ; j++) if (arr1[i][j] > max) max = arr1[i][j]; return max; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote