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

Goals: Using 2-D arrays and functions Problem: For this lab you should declare a

ID: 3633031 • Letter: G

Question

Goals: Using 2-D arrays and functions

Problem: For this lab you should declare a 2-D array that will contain up to 20 rows and 15 columns. The template for this lab that has been included below contains code to prompt the user to enter the number of rows and columns he/she would like to use, confirms the user’s entry is valid, and contains a function to print the array. You are to add 2 functions: one that will fill the array by prompting the user for numbers, and another that will find the largest number in the array. Both of these functions should have 3 arguments: the array, the number of rows, and the number of columns. For the array given below, the largest number is 20:
9 14 7 3
2 5 8 11
4 20 19 17

After you find the max, it should be sent back to main() and printed to the screen.

Before you try to write any code, clearly define the purpose of your function, the necessary input for your function and the processing that your function will employ as comments before your function.

Once your function has been written add the function calls and function prototypes for your functions and for the function to print the array.

Try the code with a 3 by 4 array and with other arrays to confirm it works correctly. Fix any errors.





//add intro comments




#include <iostream>
#include <iomanip>
using namespace std;

//add prototypes here


int main()
{
//declare an array for whole numbers that may contain 20 rows and 15 columns
int rows, cols;
//getting number of rows 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;
}

//getting number of columns from user
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;
}

//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++;
}
}

//Add comments and function to fill array


//Add comments and function to find the max in the array




Explanation / Answer

please rate - thanks


//add intro comments

//this program fills and prints a 2D array, and outputs the largest value in it

#include <iostream>
#include <iomanip>
using namespace std;

//add prototypes here
void Print2DArray(int [][15], int, int);
void fill2DArray(int [][15], int, int);
int max2DArray(int [][15], int, int);
int main()
{
//declare an array for whole numbers that may contain 20 rows and 15 columns
int rows, cols;
int array[20][15];
//getting number of rows 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;
}

//getting number of columns from user
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;
}

//call function to fill array
fill2DArray(array,rows,cols);
//call function to print array
Print2DArray(array,rows,cols);
//call function to find max
cout<<"The largest number in the array is: "<<max2DArray(array,rows,cols)<<endl;
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++;
}
}

//Add comments and function to fill array
//function to fill a 2D integer array of up to 15 columns
//function accepts the 2D array and the number of rows and columns used.
//function asks the user to input each element of the array 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 .
//Post: The 2D array has been filled
void fill2DArray(int arr1[][15], int r, int c)
{
int i = 0, j;
while (i < r)
{
j = 0;
while (j < c)
{cout<<"Enter element ["<<i<<"]["<<j<<"]: ";
cin>>arr1[i][j];
j++;
}
i++;
}
}

//Add comments and function to find the max in the array
//function to find the largest element in a 2D array of up to 15 columns
//function accepts the 2D array and the number of rows and columns used.
//function returns the largest element to the calling program
//this is done by comparing each element to an initial "largest element"
//and if the element is larger, it becomes the largest element
//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 largest element has been returned
int max2DArray(int arr1[][15], int r, int c)
{int max=arr1[0][0];
int i = 0, j;
while (i < r)
{
j = 0;
while (j < c)
{
if(arr1[i][j]>max)
    max=arr1[i][j];
j++;
}
cout<<endl;
i++;
}
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