2011 Exercise 1-1
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
to 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 + 6 – 2 + 6%3 = _____________________________
7
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 ()
{
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;
}
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 |
ü |
|
C++ evaluates equal precedence arithmetic
operators 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 |
|
ü |
A compiler converts a high-level language to
0s and 1s |
ü |
|
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: Control Statements and Algorithms
n
Given the following code
for (int m = 1; m<10; m++)
{
cout<< "X is 5";
}
·
Duplicate the output using a while loop
int counter = 0;
while (counter <10)
{
cout<<"X is 5";
counter = counter + 1)
}
·
Duplicate the output using a do while loop
int counter = 0;
do
{
cout<<"X is 5";
counter
= counter + 1)
}
while (counter <10)
n
Given that an array named MyArray has been declared and initialized with 100
random
integers.Write code below that will find and print the largest integer in
the array
int largest = MyArray[0];
for (int i = 1; i<100;i++)
{
if MyArray[i} > largest
{
largest = MyArray[i]
}
}
cout<<"The largest is: "<<largest<<endl;
n
Given that an array named MyArray has been declared and initialized with 100
random
integers.Write code below that will find and print the sum of the elements
in the array
int sum = 0;
for (int i = 0; i<100;i++)
{
sum = sum + MyArray[i];
}
cout<<"The largest is: "<<largest<<endl;
Part 5: Functions
This problem
involves writing a main function that will prompt the user to enter an angle
in degrees; the angle should be entered without a fractional component. It
will then pass this angle to a function named convertToRadians. Finally,
both the angle in degrees and the converted angle will be passed to a
function named printResults. The function printResults will print both
values, appropriately labeled. 2 functions in addition to the main will be
used.
Write code for
this problem in parts as explained below.
n
Write code that must be placed before the main in the space provided after
the 3 lines of
code listed below. Said
differently, write the function prototypes for the functions
convertToRadians and printResults.
#include "stdafx.h"
#include <iostream>
using namespace std;
double convertToRadians(int degrees);
void printResults(int degrees, double radians);
n
Write code below that will prompt the user to enter an angle (without
decimals) in degrees
and assign the value entered to a properly declared variable named degrees.
Assume this
code is being written in the main (you do not have to write the rest of the
main function...)
int degrees = 0;
cout <<"Please enter the angle in degrees as an integer"<<endl;
cin >>degrees;
n
Assume the following code is also being written in the main.
Write code below that will call a
function named convertToRadians and pass the above variable degrees to it.
Upon return
from the function the converted value is to be assigned to a properly
declared variable named
radians.
double radians = convertToRadians(degrees);
n
Assume the following code is also being written in the main.
Write code that will call a function named printResults and pass both
variables degrees and
radians to it.
printResults(degrees, radians);
n
Write the complete function convertToRadians as described above in the space
below. The
function will convert the angle from degrees to radians using the following
approximate
relationship. Radians = 3.1416*Degrees/180
It will then return this converted value to the
main.
double convertToRadians(int degrees)
{
return degrees*3.1416/180;
}
n
Write the complete function printResults as described above in the space
below. This
function will print, appropriately labeled, the angle in both degrees and
radians.
void printResults(int degrees, double radians)
{
cout <<"The angle in degrees is: "<<degrees<<endl;
cout <<"The angle converted to radians is: "<<radians<<endl;
}
Part 6: Math Functions and Logic Operators
Assume the following code has been written in the
main.
double pi = 3.1416;
double a = -431.7;
int z = 100;
Write code that will accomplish the following
n
Print the cosine of pi
cout<<cos(pi)<<endl;
n
Print the square root of pi
cout<<sqrt(pi);
n
Print z raised to the power 3
cout<<pow(z, 3);
Part 7: 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
n
Father of computer science_____________________________
Alan Turing
n
Designed the first computer ___________________________
Charles Babbage
n
A picture of __________________________________ appears on the first page of
this quiz.
George Boole