DECISIONS

1. Modify the code given below adding curly brackets so that it will produce the given output when
iX is 0 and iY is 20. Do not move, add or delete any of the statements.

Code Output
if(iX<10)
if(iY>10)
cout << "***\n";
else
***
###
$$$

2. Given an integer variable iX, write a multi-way if else that will behave in the following way. If iX
is greater than or equal to 10, "High" will be output. If iX is greater than 0 and less than 10,
"Medium" will be output. If iX is less than or equal to 0, "Low" will be output.

3. Write an if else statement that will output to the screen "Hi" if the short integer siValue is greater
than or equal to five, and will print out "Bye" otherwise.

4. Write an if else statement that will check the value of a variable ldTest. If ldTest is less than zero,
a message will print to the screen stating that ldTest is less than zero. If ldTest is not less than
zero, a message will print to the screen that reads ldTest = xxx, where xxx is the value of ldTest.

5. Write the output produced by the following code segment.
int iA = 5, iB = 3;
if(iA < iB)
cout << "Hello" << endl;
cout << "GoodBye" << endl;

6. Write the output produced by the following code segment.
int iMark = 94, iTime = 82;
cout << iTime << endl;
iTime = iTime + 8;
cout << iTime << endl;
if(iTime >= iMark)
{
iTime++;
cout << iTime << endl;
}
else
{
iTime= iMark-iTime;
cout << iTime << endl;
}
cout << "Time: " << iTime << endl;

LOOPS

1. Given the code below, write the output.

int ii;
for(ii = 1; ii < 15; ii = ii + 3)
{
cout << ii << ", ";
}
cout << endl << "The end!";

2. Write a for loop that sums the odd integers from 1 to 15 inclusive. This value should be stored in
an integer variable name iVal.

3. Write a for loop that sums every third integer from 2 to 32 (2, 5, 8, 11...), inclusive, into an
accumulator variable that has an initial value of zero.

4. What is the primary difference between a while loop and a do while loop?
cout << "###\n";
cout << "$$$\n";

5. Write the output produced by the following code segment.

int iX = 4;
int iCount = 0;
while(iCount <= 3)
{
iX = 2 * iX;
cout << iX << endl;
iCount++;
}
cout << iCount;

6. Write the output produced by the following code segment.

int ii;
for(ii = 3; ii <= 9; ii = ii + 2)
cout << ii << endl;

7. Write a for loop that will output the numbers 6, 5, 4, 3, 2, 1, in that order, one per line.

8. Write an equiValent for loop for the following while loop.

int iValue = 6;
while(iValue < 33)
{
cout << iValue << endl;
iValue = iValue + 3;
}

FUNCTIONS

1. Assume we want to find the square of the integers between 1 and 10 and print. Fill in the blanks of
the C++ program below to make the code function as prescribed.

#include
int Square(int iNum1);
int main()
{
for(int iNum2 = 1; iNum2 <= ______; iNum2++)
{
cout << Square(iNum2) << endl;
}
return 0;
}
int Square(int iNum3)
{
return ______ * iNum3;
}

2. After the function MyFunction below is exectued, what value is returned from this function?
(assume iN = 4)

int MyFunction(int iN)
{
int iP = 1;
for(int iO = 2; iO <= iN; iO++)
{
iP = iP * iO;
}
return iP;
}

3. What is the difference between pass by value and pass by reference?
4. Write a function prototype that has the following characteristics:
  • The function is named FooBar
  • The function returns a character
  • The function takes as an argument a constant reference integer.

5.Write a function prototype that has the following characteristics:
  • The function is named MyFunction
  • The function does not return anything
  • The function takes as an argument an array of integers
  • The function takes as an argument an integer that specifies the number of elements in the array

6. What is the one function that every C++ program must have?

7. Define pre and post conditions of a function, and state the relationship between them.

8. What is the value of iy after the code below is executed?

int ix = 15, iw = 20;
int iy = Min(ix, iw);
int Min(int ia, int ib)
{
int ireturn;
if(ia < ib)
ireturn = ia;
else
ireturn = ib;
return ireturn;
}

See part I of these questions

Post a Comment Blogger

 
Top