What is printed by the following program segment? int loopVar; for (loopVar = 0;
ID: 3907185 • Letter: W
Question
What is printed by the following program segment?
int loopVar;
for (loopVar = 0; loopVar < 6; loopVar++)
{
if (loopVar < 3)
cout << '*';
else
cout << '%';
}
2.
A function checkFactor accepts two integer parameters. It returns true if the second parameter is a factor of the first (that is, the second number multiplied by some other number is equal to the first number. In other words, the second parameter divides the first evenly), and false otherwise.
Write the function prototype, definition (header and body), and a sample call from the main program. You do not have to write the entire main program.
A function listFactors accepts one integer parameter. It prints out all factors of the parameter (that is all integers that divide the parameter with no remainder). For example, if the parameter is equal 6, the function prints out:
1, 2, 3, 6
Write the function prototype, definition (header and body), and a sample call from the main program. You do not have to write the entire main program.
4. Two Dimensional Arrays:
A. Given the following array: int twice[3][2];
Write the C++ code that sets all elements of the array to zero.
B. Show a drawn picture of the array filled in after the following code executes.
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
twice[i][j] = i + j +
Explanation / Answer
int loopVar;
for (loopVar = 0; loopVar < 6; loopVar++)
{
if (loopVar < 3)
cout << '*';
else
cout << '%';
}
The above code fragment runs a for loop for the value of loopVar from 0 to 5. If the value of loopVar at a particular iteration is less than 3, then '*' is printed, otherwise '%' is printed.
So, for the value of loopVar 0, 1 and 2, '*' is printed and for the value of loopVar 3, 4 and 5 '%' is printed.
Therefore, OUTPUT is:
***%%%
2. checkFactor()
Prototype
bool checkFactor(int, int);
Definition
bool checkFactor(int dividend, int divisor)
{
int remainder = dividend % divisor;
//% operator finds the remainder when 1st number is divided by the 2nd number
if(remainder == 0)
//If remainder is 0, it means dividend is evenly divided by the divisor, then return true;
return true;
else
//Otherwise, dividend is not evenly divided by the divisor, then return false
return false;
}
Function Call
if(checkFactor(6, 2))
cout<<"2 is a factor of 6.";
else
cout<<"2 is not a factor of 6.";
//The main function call is checkFactor(6, 2) only.
listFactors()
Prototype
void listFactors(int);
Definition
void listFactors(int dividend)
{
int divisor, remainder;
for(divisor = 1; divisor <= dividend; divisor++)
//For loop from 1 to dividend, then we will check each number if it is a factor or not
{
remainder = dividend % divisor;
//% operator finds the remainder when 1st number is divided by the 2nd number
if(remainder == 0)
//If remainder is 0, it means dividend is evenly divided by the divisor, then print the number(divisor)
cout<<divisor<<" ";
}
//For loop ends
}
We can also implement this function using checkFactor() as follows:
void listFactors(int dividend)
{
int divisor;
for(divisor = 1; divisor <= dividend; divisor++)
//For loop from 1 to dividend, then we will check each number if it is a factor or not
if(checkFactor(dividend, divisor))
//If divisor is a factor of dividend, then print the number(divisor)
cout<<divisor<<" ";
}
Function Call
listFactors(6);
4. A.
//Set all elements to 0
int i, j;
for(i = 0; i < 3; i++)
//Loop to traverse through 3 rows
for(j = 0; j < 2; j++)
//Loop to traverse through 2 columns
twice[i][j] = 0;
The next part is not clear as the last statement ends with i + j + and hence can not be answered. Kindly try to post complete and clear questions.
Kindly give a thumbs up, if found useful. Comment for queries. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.