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 two-dimensional 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.
#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?
";
#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);
}