For this lab you should declare a 2-D array that will contain upto 20 rows and 1
ID: 3614910 • Letter: F
Question
For this lab you should declare a 2-D array that will contain upto 20 rows and 15 columns. The template for this lab(shown below) contains code to prompt theuser to enter the number of rows and columns he/she would like touse, confirms the user’s entry is valid, and contains afunction to print the array. You are to add a function thatwill accept the 2D array and the rows and columns specified by theuser then fill the array so that each number in the array is (2 *the row index) plus (3 * the column index). For example a 3by 4 matrix would look like
0 3 6 9
2 5 8 11
4 7 10 13
Before you try to write any code, clearly define the purpose ofyour function, the necessary input for your function and theprocessing that your function will employ as comments before yourfunction.
Once your function has been written add the function callsand function prototypes for your function and for the function toprint the array.
Try the code with a 3 by 4 array and with other arrays toconfirm it works correctly. Fix any errors.
-------------------------------------------------------------------
The template:
//add intro comments
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//declare an array for whole numbers that maycontain 20 rows and 15 columns
int rows, cols;
//getting number rows and columns from user
cout<<"Please enter the dimensions of the2D array."<<endl;
cout<<"Please enter the number of rows youwant to use"<<endl;
cin>>rows;
//confirming number of rows are withingbounds
while(rows <= 0 || rows > 20)
{
cout<<"The number ofrows must be greater than 0 and less than or equal to20"<<endl;
cout<<"Please enter thenumber of rows you want to use"<<endl;
cin>>rows;
}
cout<<"Please enter the number of columnsyou want to use"<<endl;
cin>>cols;
//confirming number ofcolumns are withingbounds
while(cols <= 0 || cols > 15)
{
cout<<"The number ofrows must be greater than 0 and less than or equal to15"<<endl;
cout<<"Please enter thenumber of rows you want to use"<<endl;
cin>>cols;
}
//call function to fill array
//call function to print array
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 columnsused.
//function outputs the array
//Pre: Number of rows and columns of the 2D array have beenentered and
// confirmed to be valid. Thearray 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
Explanation / Answer
please rate - thanks #include #include using namespace std; void fill(int[][15],int,int); void Print2DArray(int[][15],int,int); int main() { //declare an array for whole numbers that may contain 20 rows and15 columns int rows, cols,a[20][15]; //getting number rows and columns from user coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.