Question

In: Computer Science

Inside the skeletal file you'll find a complete main function, as well as the function headers...

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? ";

Solutions

Expert Solution

#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);
}


Related Solutions

Inside the skeletal file you'll find a complete main function, as well as the function headers...
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...
Complete the function main in file median.c to implement the computation of the median of a...
Complete the function main in file median.c to implement the computation of the median of a sequence of integers read from stdin. The program should use realloc to allow an arbitrary number of integers to be read into a dynamically allocated array. Every time realloc is called, let the new size of the array be twice the old size plus 1. Look at function readLongLine in the slides for Chapter 6 for an example of how to use realloc. The...
Using C++ 1. Create a main function in a main.cpp file. The main function should look...
Using C++ 1. Create a main function in a main.cpp file. The main function should look as follows int main() {return 0;} 2. Create an array. 3. Ask user to enter numbers in size of your array. 4. Take the numbers and store them in your array. 5. Go through your array and add all the numbers. 6. Calculate the average of the numbers. 7. Display the numbers, sum and average.
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
Create and complete a function M-file that will receive an input, ? , and output the...
Create and complete a function M-file that will receive an input, ? , and output the corresponding conversion to radians within the range of a complete circle. For each 30? increment, the output should be displayed as a string, (i.e. pi/2 , pi/4 ,etc.), and any other degree to be displayed numerically. I'm using Matlab, explanations are appreciated.
7. Inside an earthworm where can you find the gregarines. What is the function of this...
7. Inside an earthworm where can you find the gregarines. What is the function of this organ? 8. Young trophozoites seem to have cilia which they shed later. What are these tails? 9. What is the best way this organism is transmitted from one organism to another? 10. The gametocytes are __________________ and sporozoites are ______________ (haploid diploid) 11. Who are the hosts for plasmodium and Trypanosoma gambiense? 12. Discuss the life cycle of the malarial parasite.
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are...
Complete the three empty methods (remove(), find(), and contains()) in the ShoppingListArrayList.java file. These methods are already implemented in the ShoppingListArray class. Complete the ShoppingListArrayListTest.java (For many tests, we now just throw a false statement which will cause the test cases to fail. You need to complete them.) ShoppingListArrayList package Shopping; import DataStructures.*; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Scanner; /** * @version Spring 2019 * @author Paul Franklin, Kyle Kiefer */ public class ShoppingListArrayList implements ShoppingListADT { private ArrayList<Grocery> shoppingList;...
C++ If you tried to compile a source file that doesn't contain a main() function, would...
C++ If you tried to compile a source file that doesn't contain a main() function, would this error be detected by the preprocessor, the compiler, or the linker? Briefly justify your answer. Hint: think about how the files that don't have main() get processed in a project with multiple files. Alternatively, try it out and look at the error message you get.
Why would you use "using namespace" inside of your function instead of the above main?- Give...
Why would you use "using namespace" inside of your function instead of the above main?- Give examples
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT