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
function. (Do
NOT ask for help with code unless these comments are written, you may

Once
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

``````#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++;
}
}

2
Contributors
1
2
Views
8 Years
Discussion Span
Last Post by vmanes

You need to actually declare the array in main( ).

Your print function looks ok - you need one similar to that which fills in the array. It won't be much of a change from your print function.

I do, however, question the sample output provided

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

Shouldn't that be:
0 3 6 9
2 5 8 11
4 7 10 13

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.