205k views
0 votes
1. Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just one statement. Hint: Call FindMax() twice in an expression.

Sample program:
double FindMax(double num1, double num2) {
double maxVal = 0.0;
if (num1 > num2) { // if num1 is greater than num2,
maxVal = num1; // then num1 is the maxVal.
} else { // Otherwise,
maxVal = num2; // num2 is the maxVal.
}
return maxVal;
}

int main() {
double numA = 5.0;
double numB = 10.0;
double numY = 3.0;
double numZ = 7.0;
double maxSum = 0.0;

cout << "maxSum is: " << maxSum << endl;
return 0;
}
2. Define a function PrintFeetInchShort, with int parameters numFeet and numInches, that prints using ' and " shorthand. Ex: PrintFeetInchShort(5, 8) prints:
5' 8"
Hint: Use \" to print a double quote.
Sample program:
#include
using namespace std;

int main() {
PrintFeetInchShort(5, 8);
cout << endl;
return 0;
}
3. Complete the PrintTicTacToe function with char parameters horizChar and vertChar that prints a tic-tac-toe board with the characters as follows. End with newline. Ex: PrintTicTacToe('~', '!') prints:
x!x!x
~~~~~
x!x!x
~~~~~
x!x!x
Sample program:
#include
using namespace std;
void PrintTicTacToe(char horizChar, char vertChar) {

return;
}
int main() {
PrintTicTacToe('~', '!');
return 0;
}
4. Complete the PrintShape() function to print the following shape. End with newline.
Example output:
***
***
***
Sample program:
#include
using namespace std;
void PrintShape() {

return;
}
int main() {
PrintShape();
return 0;
}
5. Complete the function definition to print five asterisks ***** when called once (do NOT print a newline). Output for sample program:
**********
Sample program:
#include
using namespace std;
void PrintPattern() {

}
int main() {
PrintPattern();
PrintPattern();
cout << endl;
return 0;
}

User Rikonator
by
6.6k points

1 Answer

2 votes

Answer:

1)

maxSum = FindMax(numA, numB) + FindMax(numY, numZ);

2)

void PrintFeetInchShort(int numFeet , int numInches){

cout<<numFeet<<"'"<<numInches<<"\""; }

3)

void PrintTicTacToe(char horizChar, char vertChar){

for(int r=1;r<=3;r++) {

for(int c=1;c<=3;c++) {

cout<<"x";

if(c<=2) {

cout<<vertChar; } }

cout<<endl;

if(r != 3) {

for(int c = 1; c<=5; c++) {

cout<<horizChar; }

cout<<endl; } } }

4)

void PrintShape(){

for(int r=1;r<=3;r++){

for(int c=1;c<=3;c++){

cout<<"*"; }

cout<<endl;} }

5)

void PrintPattern(){

for(int r=1;r<=5;r++){

cout<<"*"; } }

Step-by-step explanation:

1.

In the statement, maxSum is a double type variable which is assigned the maximum of the two variables numA numB PLUS the maximum of the two variables numY numZ using which are found by calling the FindMax function. The FindMax() method is called twice in this statement one time to find the maximum of numA and numB and one time to find the maximum of numY numZ. When the FindMax() method is called by passing numA and numB as parameters to this method, then method finds if the value of numA is greater than that of numB or vice versa. When the FindMax() method is called by passing numY and numZ as parameters to this method, then method finds if the value of numY is greater than that of numZ or vice versa. The PLUS sign between the two method calls means that the resultant values returned by the FindMax() for both the calls are added and the result of addition is assigned to maxSum

2.

The above function takes two integer variables numFeet and numInches as its parameters. The cout statement is used to print the value of numFeet with a single quote at the end of that value and print the value of numInches with double quotes at the end of that value. Here \ backslash is used to use the double quotes. Backslash is used in order to include special characters in a string. In order to add double quotes with a string or a value, backslash is compulsory.

3.

The function PrintTicTacToe has two character type (char) parameters horizChar and vertChar.

The function has two loops; outer loop and an inner loop. The outer loop is used to iterate through the rows and inner loop is used to iterate through the columns. The inner loop iterates and prints a row of the character "x" followed by vertChar which contains a character ! . This means at each iteration x! is printed and it stops at the third "x" as the loop variable c iterates until the value of c remains less than or equal to 3. Notice that the last "x" in each row is not followed by a "!" because of if(c<=2) condition which prints ! only till the second column which means cout<<vertChar; statement only executes until the value of c remains less than or equal to 2. When the value exceeds 2 then the next if statement if(r != 3) executes which checks if the value of r is not equal to 3. If this condition is true then the loop inside this 2nd if statement executes which prints value of horizChar at each iteration and horizChar contains "~". c<=5; means that the loop will print "~" 5 times. After the first row of x!x!x and ~~~~~ are printed the outer loop executes for the next iteration and outer loop variable r is incremented to one to print the second row of x!x!x

~~~~~ using inner loop and two if conditions. This outer loop body executes 3 times.

4.

The function has two loops an inner and outer loop. Outer loop is used for rows and inner for columns. At each iteration of inner loop an asterisk is printed and the inner loop runs for 3 times which means three asterisks will print ***. The outer loop executes for 3 times which means total three rows of three *** asterisks are displayed each *** asterisks are printed with a new line using cout<<endl; statement.

5.

The function uses a loop for(int r=1;r<=5;r++) to print 5 asterisks ***** without printing a new line. The function PrintPattern() is called twice which means the pattern of 5 asterisks ***** is printed twice as: ********** without printing a new line between them.

1. Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just-example-1
1. Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just-example-2
1. Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just-example-3
1. Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just-example-4
1. Assign to maxSum the max of (numA, numB) PLUS the max of (numY, numZ). Use just-example-5
User Lockstock
by
6.9k points