Question

In: Computer Science

Please, use C++ for this question. (b) Write the definition of displayBox() function. The function will...

Please, use C++ for this question.

(b) Write the definition of displayBox() function. The function will display a matrix of integers from 1 to 4 such that the first row will contain four 1s, the second row will contain four 2s, the third row will contain four 3s and the fourth row will contain four 4s. Function must use nested loops to display each column and row heading as well as the numbers. The function has no parameters and no return value.

The output should look something like this:


         1    2    3    4        

-------------------------                    

1 |    1    1    1    1         

2 |    2   2    2    2                       

3 |    3    3    3    3                            

4 |    4    4    4    4                         

                                  

Use standard C++ stream I/O for output and manipulators for formatting. No main() or #includes are required.

Solutions

Expert Solution

complete program with driver code :

#include<iostream>
#include<iomanip>
using namespace std;

void displayBox()
{
        int width = 5;  
        /* setw(n) sets the output width to n including the space taken care by character after setw*/
        cout <<"  "<< setw(width) << "1" << setw(width) << "2" << setw(width) << "3" << setw(width) << "4";  // print header line
        cout<<endl;
        for (int i = 1; i <= (width * 5 + 2); i++)    /* loop to print '-'. no of '-' is equal to 2 + number of characters in header * width */
        {
                cout << "-";
        }
        cout << endl;
        for (int i = 1; i <= 4; i++)   /* loop to print rest of the matrix */
        {
                cout << i << "|";
                for (int j = 1; j <= 4; j++)
                {
                        cout << setw(width) << j;
                }
                cout << endl;
        }

}

int main()
{
        displayBox();
        return 0;
}

displayBox function :


void displayBox()
{
        int width = 5;  
        /* setw(n) sets the output width to n including the space taken care by character after setw*/
        cout <<"  "<< setw(width) << "1" << setw(width) << "2" << setw(width) << "3" << setw(width) << "4";  // print header line
        cout<<endl;
        for (int i = 1; i <= (width * 5 + 2); i++)    /* loop to print '-'. no of '-' is equal to 2 + number of characters in header * width */
        {
                cout << "-";
        }
        cout << endl;
        for (int i = 1; i <= 4; i++)   /* loop to print rest of the matrix */
        {
                cout << i << "|";
                for (int j = 1; j <= 4; j++)
                {
                        cout << setw(width) << j;
                }
                cout << endl;
        }

}

output screen shot


Related Solutions

Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
C++ (cpp) PLEASE Write a complete function (prototype and definition) that takes an integer array and...
C++ (cpp) PLEASE Write a complete function (prototype and definition) that takes an integer array and the array dimensions as an input. The function should then square each value of the array in-place. At the completion of the function return a Boolean which indicates the process has completed.
Write the following task in C++1) Write the definition of a function numOccurrences thatsearches...
Write the following task in C++1) Write the definition of a function numOccurrences that searches for a character in a character array and returns the number of times it occurs in the array. The function has three formal parameters: a char array array, an int variable arraySize representing the size of the array, and a character variable letter representing the character to be searched for in the array.2) Assume the array numbers is an int array of size 10 and...
Please question on DATABASE Function dependencey: i) S(A, B, C, D) with FD’s A --> B,...
Please question on DATABASE Function dependencey: i) S(A, B, C, D) with FD’s A --> B, B --> C, and B --> D. ii) T(A, B, C, D) with FD’s AB --> C, BC --> D, CD --> A, and AD --> B.
[PLEASE USE C++] Write a function to read values of a number of rows, number of...
[PLEASE USE C++] Write a function to read values of a number of rows, number of columns, 2 dimensional (2D) array elements and display the 2D array in a matrix form. Input 2 3 1 4 5 2 3 0 Where, First line of represents the number of rows. Second line of input represents the number of columns. Third line contains array elements of the 1st row and so on. Output 1 4 5 2 3 0 where There must...
Make a function definition in C for the following: void insert (double *b, int c, double...
Make a function definition in C for the following: void insert (double *b, int c, double s, int pos); //Insert value s at position pos in array. //needs: // c > 0, pos >= 0, and pos <= c-1. Elements b[0]...b[c-1] exist. //this will do: //Elements from indexes pos up to c-2 have been moved up to indexes pos+1 up to c-1. The value s has been copied into b[pos]. //Note: the data that was in b[c-1] when the function...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character...
Programming in C language (not C++) Write a function definition called PhoneType that takes one character argument/ parameter called "phone" and returns a double. When the variable argument phone contains the caracter a or A, print the word Apple and return 1099.99. When phone contains the caracter s or S print the word Samsung and return 999.99. When phone contains anything else, return 0.0.
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
Nodes Problems In C++, Your objective is to write the definition of the function Maximum() whose...
Nodes Problems In C++, Your objective is to write the definition of the function Maximum() whose header is double Maximum(Node* root) It returns the maximum value from the singly linked list referenced by root. If root is referencing an empty list, the function returns 0. Part B: Take home Your objective is to write the definition of the following functions the function EndAppend() whose header is void EndAppend(Node*& data,Node* addon) template It appends the linked list referenced by addon to...
For each question please give: a. a definition of the term; and b. discuss how the...
For each question please give: a. a definition of the term; and b. discuss how the concept assists us in understanding the changing nature of work and/or employment a) Bureaucratic control
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT