Declare a 2-D array that will contain up to 20 rows
and 15 columns. The template for this lab that has been
posted on ANGEL 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 a function that will accept the 2D array and the
rows and
columns specified by the user then fill the array so that each number in
the
array is (2 * the row index) plus (3 * the column index).
For example a 3 by 4 matrix would look like
0 4 5 9
1 5 8 11
4 7 10 13
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. (Do
NOT ask for help with code unless these comments are written, you may
ask for
help in answering the questions.)
Once
your function has been written add the function calls and function
prototypes for
your function and for the function to print the array.
below is what I tried so far
and I need help on part that I commented, thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//declare an array for whole numbers that may contain 20 rows and 15 columns
int rows, cols;
//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;
}
//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 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
| |