In: Computer Science
Inside the skeletal file you'll find a complete main function,
as well as the function headers for
the DispMatrix and InitMatrix functions. A 15x5 array of ints is
allocated in the main function,
without being initialized. Just to highlight the fact that the
array will contain undefined values, a
call is made to the DispMatrix function, which will display the 2D
array's contents (you should
just see a bunch of garbage values). The DispMatrix will receive as
arguments the base address
of the 2D array, the number of rows, and also arguments for the
height and width for displaying
the integer values. This function call will use the defined
constants DEFAULT_HEIGHT and
DEFAULT_WIDTH for those arguments, but that's just for the first
display of the 2D array, after
that the user at the keyboard will be able to provide their own
values.
Next, the InitMatrix function is called. This function will receive
the base address of the twodimensional array, in addition to the
number of rows in the array. Naturally, since the function
receives the base address of the caller's array, it knows where the
caller's argument is living in
memory, so it can go to that location and assign values to the
individual array elements for the
caller. What InitMatrix does is assign sequentially increasing
values to the matrix parameter,
beginning with the value zero. After this function returns back to
main, then the DispMatrix
function is called again, which writes the rows and columns of the
two-dimensional array to
stdout. However, before doing so, the main function prompts the
user to enter the horizontal
and vertical spacing for the matrix. These values are sent as
arguments to DispMatrix, which
then uses them to format the output.
Of course, a picture is worth a thousand words, but a sample
program run doesn't hurt either!
So to get a live, concrete feeling of how the program should behave
at runtime, be sure to run
the sample executable included in the starter kit, at least several
times, if not more. The better
you know what your target is, the more likely you are to write the
code to hit that target.
You can see some sample runs of the program at the top of the
source code file. Also, there's
helpful information in the function headers that will explain even
further what the parameters
mean, and thus what the caller is passing to each function.
Remember, in order to process two-dimensional arrays, you're going
to need to use nested loops.
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
// defined constants
const int DEFAULT_HEIGHT = 2;
const int DEFAULT_WIDTH = 15;
const int ROWS = 15;
const int COLS = 5;
// function prototypes
???
???
// ==== main
==================================================================
//
//
============================================================================
int main()
{
int myInts[ROWS][COLS];
int userHeight;
int userWidth;
// first display the undefined values in the 2D array
cout << "Here is the matrix with undefined values..."
<< endl;
DispMatrix(myInts, ROWS, DEFAULT_HEIGHT, DEFAULT_WIDTH);
// intialize the 2D array
InitMatrix(myInts, ROWS);
// get the field width from the user (test for failed
extraction...)
cout << "What field width do you want to use? ";
if (!(cin >> userWidth))
{
cerr << "Error reading width..." << endl;
exit(EXIT_FAILURE);
}
// get the row height from the user (test for failed
extraction...)
cout << "What row height do you want to use? ";
if (!(cin >> userHeight))
{
cerr << "Error reading height..." << endl;
exit(EXIT_FAILURE);
}
// display the 2D array
DispMatrix(myInts, ROWS, userHeight, userWidth);
return 0;
} // end of "main"
// ==== DispMatrix
============================================================
//
// This function writes the two-dimensional array parameter to
stdout in row /
// column format. It uses the height and width parameters to
provide row and
// column spacing, respectively.
//
// Input:
// matrix [IN] -- the base address of a two-dimensional array of
integers
// with COLS columns
//
// numRows [IN] -- the number of rows in the matrix parameter
//
// height [IN] -- the number of rows each one-dimensional array
should
// occupy
//
// height [IN] -- the number of rows each one-dimensional array in
the
// 'matrix' parameter should occupy
//
// width [IN] -- the field width for each integer displayed
//
// Output:
// Nothing
//
//
============================================================================
void DispMatrix( ??? )
{
???
} // end of "DispMatrix"
// ==== InitMatrix
============================================================
//
// This function initializes the two-dimensional array parameter
with
// sequentially increasing integer values, beginning with
zero.
//
// Input:
// matrix [OUT] -- the base address of a two-dimensional array of
integers
//
// numRows [IN] -- the number of rows in the matrix parameter
//
// Output:
// Nothing
//
//
//
============================================================================
void DispMatrix( ??? )
{
???
} // end of "DispMatrix"
// ==== InitMatrix
============================================================
//
// This function initializes the two-dimensional array parameter
with
// sequentially increasing integer values, beginning with
zero.
//
// Input:
// matrix [OUT] -- the base address of a two-dimensional array of
integers
//
// numRows [IN] -- the number of rows in the matrix parameter
//
// Output:
// Nothing
//
//
============================================================================
void InitMatrix( ??? )
{
???
} // end of "InitMatrix"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
// defined constants const
int DEFAULT_HEIGHT = 2; // cols
const int DEFAULT_WIDTH = 15; // rows
const int ROWS = 15;
const int COLS = 5;
// function prototypes ??? ??? //
//
void DispMatrix(int myInts[][COLS],int rows,int hight,int
width){
for(int i=0;i<width;i++){
for(int j=0;j<hight;j++)
cout<<myInts[i][j]<<" ";
cout<<endl;
}
}
void InitMatrix(int myInts[][COLS],int rows){
int counter = 0;
for(int i=0;i<rows;i++){
for(int j=0;j<COLS;j++){
myInts[i][j] = counter++;
}
}
}
//
//
============================================================================
int main()
{
int myInts[ROWS][COLS];
int userHeight; int userWidth;
cout << "Here is the matrix with undefined values..."
<< endl;
DispMatrix(myInts, ROWS, DEFAULT_HEIGHT, DEFAULT_WIDTH);
InitMatrix(myInts, ROWS);
// get the field width from the user (test for failed
extraction...
cout << "What field width do you want to use? ";
if (!(cin >> userWidth))
{ cerr << "Error reading width..." << endl;
exit(EXIT_FAILURE);
}
// get the field height from the user (test for failed
extraction...
cout << "What height width do you want to use? ";
if (!(cin >> userHeight))
{ cerr << "Error reading width..." << endl;
exit(EXIT_FAILURE);
}
DispMatrix(myInts,ROWS,userHeight,userWidth);
}
Activities Terminal - Oct 25 10:42 AM * 8 4. 10 4 0 0 KiB/s
Terminal Q Terminal Terminal [ cpp ] $ g++ display.cpp [ cpp ]
$
Comment