Practice Problems - Chapter 8
Introduction To High-Level Programming

SHO     1.   a. Explain why  ‘<<’ is called the insertion operator when used with cout, as in:

                 cout <<  “hello”;

   

       b. Explain why ‘>>’ is called the extraction operator when used with cin as in:

              cin >> Average;

  

ANS: 

a. << is called the insertion operator because it inserts information into the character output stream, cout. It takes information that is stored in computer memory, interprets that information, converts it into appropriate text characters, and inserts these characters into the character output stream.

 

b. >> is called the extraction operator because it extracts text characters from the character input stream cin. It takes text characters from cin,  converts them into the proper format and finally stores the information in one or more computer memory cells.

 In both these operators the direction of the arrows ‘>>’ or ‘<<’ indicates the direction of the flow of information.

 

     2.   2. Define and explain the following terms which have to do with C++ programs. Use an example for each term.

 

a. prologue comment

  

 

b. include directive

 

 c. using directive

  

d. main ( ) function

  

ANS: 

a. Comments enclosed in /* */ which appear at the beginning of a C++ program.

 

 

b. A directive which tells the compiler pre-processor to include information from a standard library or a user-written library when compiling the program. For example: #include <iostream> includes information about the input output library named iostream.

 

 

c.A directive which tells the compiler in which namespace to find a given identifier. For example: using std::cin tells the compiler to look for cin in the standard namespace

 

 

 

d. This is the place in the code where the program begins executing

 

     3.  3 a. What is an identifier ( in the context of a C++ program) ?

  b. What can identifiers look like in C++ ? In other words, what are the rules for forming a valid identifier in C++?

 

ANS: 

a. An identifier is a name given to something in a program.

 b.An identifier in C++ may be any combination of letters, digits, and the underscore symbol ( _ ), as long as it does not begin with a digit.

 

     4.   4. a. What is a constant ?  

    b. What is a variable ?

    c. Write the C++ declaration statement which would be used to declare that PI is a constant with the value of 3.14159

    d. Write the C++ declaration statement which would be used to declare that Circumference is a variable that is a real number.

 .ANS: 

a. A constant is a named memory location which has a value that, once set, cannot be changed throughout the program.

b. A variable is a  named memory location whose value may be changed many times throughout the program.

c. const double PI = 3.14159;

d. double Circumference;

 

     5.   5 a. What is a data type? 

  b. What are the four basic C++ data types we learned about in class?

 ANS: 

a). A data type specifies the possible values a data item may contain, and the set of operations for creating and manipulating data items of the specified type. 

b) int - integers; double - real numbers; char - single text characters; bool - logical values true and false.

 

6 a. What does the following declaration statement do? Draw a picture.

      double Fahrenheit[10] = {0.0};

b. What does this statement do ? Draw a new picture

    Fahrenheit[4] = 89.6;  

c. What does this statement do? Draw a picture.

    double WattHours[2][12] = 0.0;

d. Now what does this statement do? Draw a new picture.

   WattHours[1][5] = 967.8;

   

ANS: 

a. Creates an array of doubles called Fahrenheit with space for 10 values. Each element of the array has an initial value of 0.0  

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

 b. Sets the value of Fahrenheit[4] to 89.6 

0.0

0.0

0.0

0.0

89.6

0.0

0.0

0.0

0.0

0.0

 c. Creates a table of doubles called WattHours with 2 rows and 12 columns. Each element in the table is given an initial value of 0.0 

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

 d.Sets the value of WattHours[1][5] to 967.8 

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

0.0

967.8

0.0

0.0

0.0

0.0

0.0

0.0

 

 7.   What does each line or indicated group of lines in the following C++ program do? Explain in detail if indicated.

 

01 /*

02 circle04.cpp

03 */

 

04 #include <iostream>

05 using std::cin;

06 using std::cout;

07 using std::endl;

 

08 int main() {

 

09  const double PI = 3.14159;

10  double radius = 0.0, circumference = 0.0;

11  char c;

 

12  cout << "Please enter the radius " << '\n';

 

13  cin >> radius;

 

14  if (radius >= 0 ) {

15   circumference = 2 * PI * radius;

16   cout << "The circumference is: ";

17   cout << circumference << ".\n";}

18  else {

19    cout << "You entered a negative number." << endl;

20    cout << "Please enter a positive number next time." << endl;

21    }

 

22   return 0;

23 }

 

1-3      Prologue comments                                                            

 

4      A preprocessor directive which includes information about the iostream library


  This allows the program to use input and output streams       

  

5-7     tells the compiler to use the specified names (cin, cout, endl) from the std namespace    

  

8    defines the beginning of the main function. This is the point where the program begins        

 

9   declares the data item PI to be a constant double with an initial value of 3..14159    

 

10   declares the data items radius and circumference to be variables with initial value 0.0    

 

11   declares the data item c to be character variable     

 

12  an output statement. The insertion operator, <<, takes the quoted text and places it in the

output stream, cout.The information appears on the display screen 
 

 

13   an input statement. The extraction operator, >>, takes a white-space delimited string

of characters from the input stream, cin, converts them to the data type of radius and stores

the result in the variable, radius                    

 

14 & 18     keywords if and else are part of the conditional statement "if ... else" . The syntax

for this statement is: if (boolean expression) statement1; else statement2; The boolean condition     

checks whether radius is >= 0.0    ;

 

15   An assignment statement. It sets the value of circumference to 2 x PI x radius       

 

16-17  Output statements which use the insertion operator << and the output stream, cout to display  

  a message  "The circumference is X." where X is the value of circumference . Along with line 15,

these lines form thr "then" clause of the "if" statement  

 

19-20   Output a message to the user informing him or her that they have entered a negative radius  

 

value and need to enter a positive number . These lines form the "else" clause of the "if" statement     

 

22    returns a value of 0 from main() to the operating system to specify normal program termination      

 

23     the "}" marks the end of the main() function      

 

.

 

     2.   8. The following pseudocode is supposed to compute the average of some

exam grades which are entered by the user. Write a C++ program which

implements the pseudocode program

 

Output “Please enter number of grades to average”

Input NumGrades

Set Value of Grade to 1

Set Value of Sum to 0

Set Value of Average to 1

Set Value of Count to 1

Set Value of Limit to NumGrades

While ( Count <= Limit )Do

   Output “Enter grade # “

   Output Value of Count

   Input Grade

   Set the Value of Sum to Sum + Grade

   Set the value of Count to Count + 1

Endwhile

Set the Value of Average to Sum / NumGrades

Output “The average of the grades is : “

Output the value of Average

Halt

.

 

ANS: 

/* average.cpp

This program computes the average value of a series og

grades entered by the user */

 

#include <iostream>

using std::cin;

using std::cout;

using std::endl;

 

int main(){

  int NumGrades;

  double Grade = 1.0;

  double Sum = 0.0;

  double Average = 1.0;

  int Count = 1;

  int Limit;

 

  cout << endl;

  cout << “Please enter number of grades to average :”;

  cin >> NumGrades;

 

  Limit = NumGrades;

 

  while ( Count <= Limit ) {

     cout << endl;

     cout << “Enter grade # “ << Count;

     cin >> Grade;

     Sum = Sum + 1;

     Count = Count + 1;

  } // end of while

 

 Average = Sum / NumGrades;

 

 cout << “The average of the grades is : “

      << Average << “ .” << endl;

} // end main

 

     3.   9 In class we saw that there are only three kinds of pseudocode statments: 1) sequential, 2) conditional and 3) iterative (looping). C++ has statements which correspond to the pseudocode versons.

 

a. What is the C++ statement we learned which performs sequential input? Give example.

 b. What is the C++ statement we learned which performs sequential output? Give example.

 c. What is the C++ statement we learned which performs a sequential ‘set the value of’ operation. Give example.

 d. What is the C++ statement we learned which is a conditional statement? Give example.

 e. What is the C++ statement we learned which is a looping statement?

 ANS: 

a. “cin >>” construct -- cin >> NumGrades;

 

b. “cout <<” construct -- cout << “hello world”;

 

c. Assignment Statement --  Circumference = PI * Diameter;

 

d. if .. else statement

 

if ( A > B )

   Max = A;

else

   Max = B;

 

e. while...do statement

 

while ( i <= count )

{

   sum = sum + i;

   i =  i +1;

}

 

ES     9  What are four advantages of a program written in a high-level programming language over a program written in assembly language?

 

ANS: 

1. The programmer does not need to manage the details of the movement of data items within  memory, or pay any attention to exactly where those items are stored.

 

2. The programmer can take a macroscopic view of tasks, thinking at a higher level of problem-solving.

 

3. Programs written in a high-level language will be portable, rather than machine specific.

 

4. Programming statements in a high-level language will be closer to standard Englisha nd will use standard mathematical notation

 

     2.   10 .The process of developing a program in a high-level language is a multi-stage process. It involves the use of many software tools such as an Assembler, a Text Editor, a Linker, a Compiler and a Loader.

 

a. Arrange these tools in the order in which they are used in the program development process.

  

b. Each of these tools takes as input one or possibly more than one file and outputs a different kind of file. Explain what each tool does. Include as part of your explanation the type of input file and output file for each tool.

 

c. Draw a flow-diagram of the high-level language software development process, including the above-mentioned tools and the files they process.

 

ANS: 

a. Text Editor, Compiler, Assembler, Linker, Loader

 

b.1. Text Editor is used to generate or edit the program as it is expressed in the particular high-level language. The Text editor outputs a text file of the program called the source file or source code. There is no input file to the text editor, unless the programmer is starting with a file which needs to be edited.

 

b.2. Compiler takes as input the source code and processes it into assembly code for the particular machine it is to be run on. The output file is a text file written in assembly code.

 b.3. Assembler takes the program written in assembly language and processes it into a machine language file called an object file.

b.4. Linker takes several object files and links them together to make a single executable machine language file called an executable file.

 b.5. Loader loads the executable file into the computer’s memory so it can be run by the operating system.

 

c. Diagram