Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Exercise Solution

 

Part 1: Fill in the Blanks

 

n  The text subdivides computer languages into 3 general types. These are

 

     ·  ______________________________________              Machine Language

 

     ·  ______________________________________              Assembly Language

 

     ·  ______________________________________              High-Level Languages

 

n  #include <iostream> is called a  __________________________________________  Preprocessor directive  also accepted class or library or header

 

n  The expression using namespace std is used in all lab assignments.   std is a ___________________________ namespace

 

n  List the escape sequence for each of the following

 

     ·  New line: __________________________ \n

 

     ·  Horizontal tab: ______________________  \t

 

n  The following symbol(s)  indicate(s) that everything from there to the end of the line is o be ignored by the compiler ________________________________  //

 

n  The following symbol(s)  indicate(s) that everything between them is to be ignored by the compiler ______________________________________  /*     */

 

n  The symbol for the modulus operator is: _________________________  %

 

n  10/3 + 5 – 2 + 6%3 = _____________________________     6

 

n  C++ applies the operators in arithmetic expressions in a precise sequence determined by ______________________________ rules of operator precedence

 

n  A procedure for solving a problem in terms of the actions to execute and the order in which these actions execute is called a(an) ___________________________

     algorithm

 

n  The research of Bohm and Jacopini demonstrated that programs could be written without any goto statements. They also demonstrated that programs could be

     written in terms of only three control structures. These are the following. Note: List the general types, do not give examples such as if…

 

     ·  ____________________________________________  sequence

 

     ·  ____________________________________________  selection

 

     ·  ____________________________________________  iteration

 

n  A _______________________ structure chooses among alternative courses of action.    Selection

 

n  Changing the data type of a variable for the current operation only is known as ___________________  casting

 

n  Writing c /= 7 is shorthand for writing _____________________ c = c/7

 

n  The operator for and  (as in a and b) is _________________ &&

 

n  The operator for or (as in a or b) is _________________ ||

 

n  The operator that is used to place the contents of the memory location designated by the variable b into the memory location designated by the variable a is

      _________ =

 

n  The operator that is used to see if the contents of the memory location designated by the variable b is the same as the contents of the memory location designated by the

     variable a is _________  ==

 

n  The operator that has the highest precedence is ________________________ ( )

 

 

Part 2: Analysis of Code

 

n  Write the output of the following code on the line following the code

 

#include "stdafx.h"

#include<iostream>

using namespace std;

 

int main ()

{

      cout <<"This is an";

      cout <<" "<<"illustration of";

      cout <<" output in C++"<<endl;

      return 0;

}


__________________________________________________________________

This is an illustration of output in C++

 

 

n  Write the output of the following code on the line following the code

 

#include "stdafx.h"

#include<iostream>

using namespace std;

 

int main ()

{

      int a = 10;

      int b = 20;

      if (a = b)

            cout <<"This prints"<<endl;

      if (b > a)

            cout <<"This prints also"<<endl;

      return 0;

}

 

_________________________________________________________________

This prints

NOTE: when you set a=b, they are then equal. So, with the next if statement, b is clearly not > a, it is equal to a because of the above assignment.

 

n  The following code will print A Green Monkey ___________  times.  20

 

#include <iostream.h>

int main ()

{

for (int i = 2; i<6;i++)

   for (int j = 2; j<7; j++)

   {

       if (j % 2 = = 0)

               cout << endl;  

       cout << "A Green Monkey  ";

   }

   return 0;

}

NOTE: THIS CODE USES A PREVIOUS COMPILER - SHOULD HAVE BEEN WRITTEN AS FOLLOWS - SO PROBLEM WAS NOT GRADED

#include "stdafx.h"

#include <iostream>

using namespace std;

int main ()

{

for (int i = 2; i<6;i++)

   for (int j = 2; j<7; j++)

   {

       if (j%2 == 0)

             cout << endl;  

       cout << "A Green Monkey  ";

   }

   return 0;

}

OUTPUT

 

 

 

Part 3: True – False

 

Indicate whether the following statements are true or false by placing a check mark (√) in the appropriate column.

 

 

Statement

True

False

 

All variables must be given a type when they are declared

ü

 

All variables must be declared before they are used

ü

 

Declarations can appear almost anywhere in the body of a C++ function

ü

 

When applied to integers, the modulus operator will return the quotient

 

ü

Preprocessor directives end with a semicolon

 

ü

C++ is case sensitive

ü

 

When a new value is placed in a memory location, the old value is destroyed

ü

 

When a value is read from memory, the value originally in the memory location read from is destroyed

 

ü

C++ evaluates equal precedence arithmetic functions right to left

 

ü

Parentheses can be used to force the order of evaluation

ü

 

Extra spaces and blank lines are ignored in C++

ü

 

cin << is used to read input from the keyboard

 

ü

Valid identifiers must begin with a letter or underscore

ü

 

a++ is shorthand for a = a + 1

ü

 

In general, global variables should be avoided.

ü

 

Assume a and b are integers. The statement if (a =b) will always be true, regardless of the values of a and b.

ü

 

A compiler converts a high-level language to 0s and 1s

ü

 

Text between /*  and  */ is ignored

ü

 

An integer variable takes less space in memory than a variable declared as a double

ü

 

The variable age is stored in the same location as the variable Age

 

ü

 

 

Part 4: Writing Code Segments

 

n  The task is find the sum of the even integers between 1 and 101 and print the sum, appropriately labeled. Write code to do this using the while structure

 

        int sum = 0;

        int i = 1;

        while (i < 101)

        {

            if (i%2 == 0)

            {

                             sum = sum + i;

            }

                        i = i + 1;

        }

       cout <<"The sum is: "<<sum<<endl;

 

 

n  Write code using nested for loops that will duplicate the following      GAVE CREDIT FOR ANY REASONABLE ATTEMPT USING NESTED FOR LOOPS

 

 

NOTE: Only the part in green required by the problem

#include "stdafx.h"

#include <iostream>

using namespace std;

 

int main ()

{

    int rows, cols;

    for (rows = 1; rows<10;rows++)

    {

            for (cols = 1; cols<rows+1; cols++)

            {

                 cout <<"9";

            }

                 cout <<endl;

    }

    return 0;

}

 

 

n  Given the following code

 

for (int i = 0; i < 5; i++)

{

   cout<<"Still Printing"<<endl;

}

 

Duplicate the output using a do while structure

 

int counter = 1;

while (counter < 5)

{

     cout<<"Still Printing"<<endl; 

    counter = counter + 1; 

} 

 

 

Part 8: Extra Credit

 

n   ASCII stands for:_____________________________________________________    

     American Standard Code for Information Interchange         

 

n   There is a clock in the lab that runs backwards.

 

     ·  There was a similar clock in the office of this famous computer scientist.  This person is:_____________________________________ Grace Murray Hopper

 

     ·  This person wrote the first ___________________________ compiler

 

n   Designer and original developer of the C++ language__________________________________  Bjarne Stroustrup