Skip Navigation


Lecture 2

New Page 1

 APC Lecture #2


This chapter picks up in the problem-solving process at the point where the algorithm is converted into actual program code.  In order to do this the programmer must understand the rules and syntax of the language.  The beginning syntax of a simple C++ program utilizing basic input, arithmetic operations, and output is introduced, as well as C++ compiler implementation of the final program.




Chapter Objectives


After completing the tutorial, the student will be able to:


·         Code an algorithm into a program

·         Desk-check a program

·         Evaluate and modify a program

·         Differentiate between source code, object code, and executable code

·         Understand the components of a C++ program

·         Save, build, and execute a C++ program

·         Locate an error in a C++ program





More on the Problem-Solving Process


In the previous chapter the problem-solving process was taken up to step 3, which is desk-checking the algorithm.  At this point the reviewed algorithm is ready to be converted into actual program code.  Once the algorithm has been converted, the problem-solving process will continue with the program code being desk-checked, and then the program will be evaluated and modified if necessary. 


Just like the algorithm was desk-checked, the first draft of the program should also be desk-checked.  Once again, a set of sample data must be created, and a walk through of the code be conducted to verify the order and accuracy of steps.


Once the desk-check of the program has been conducted, one must evaluate and modify the program if necessary.  Even though a great deal of time was put into the problem-solving process up to this point, some details could have still been overlooked.  Therefore, based upon the desk checking that was performed once the program was coded, you may need to make program code changes or changes to the original algorithm.  This is why a great deal of time needs to be spent on the original algorithm.





Coding the Algorithm into a Program


The chapter continues to examine the problem introduced in the previous chapter.  Specifically, it details steps 4-6 in the problem-solving process.



Assigning Names, Data Types, and Initial Values to the IPO Items


In the next tutorial variables will be covered in great detail, however when coding the program from the IPO chart you will want to assign names, data types, and initial values the IPO items.  While most C++ programmers use lower case for names, if names are more than one word then the beginning of the second word is capitalized to stand out.  Remember that in most programming languages that variable names can consist of letters, numbers, and maybe an underscore, however no punctuation characters or spaces.  The actual rules for naming variables in C++ will be covered in the next tutorial.


After naming the item, a data type must be chosen.  The data type specifies the type of data that the item is permitted to store (for example, whether a number would have a decimal in it or be an integer).  While the actual data types that C++ permits will be covered at a later time, in this tutorial we will learn that the double type will contain numbers with decimals.  Therefore the only remaining step is to assign the variable an initial value, which is commonly called initializing the variable.  As you will learn in the next tutorial, items declared using the double data type typically are initialized to zero using the value 0.0.


Remember that there are three parts to declaring a variable in C++.  First determine the name of the variable, then the type, and finally assign an initial value to it.  The first item coded is the data type, which is set using the C++ keyword that corresponds to the data type.  In our example this will be float.  Secondly we state the variable name as discussed previously.  And finally we assign as initial value to the variable after the equal sign.  Also remember that in C++ the variable declaration ends with a semicolon.  The entire syntax would look like the following:


datatype  variableName = initialValue;


An example could be:


double length = 0.0;



Translating the Algorithm Steps into C++ Code


In C++, streams are used to perform standard input and output operations.  The standard output stream is called cout, which refers to the monitor.  On the other hand, the standard input stream is called cin, which refers to the keyboard.  When using cout, the insertion operator (<<) is used to separate the different data items in which you would like to display.  For example, the following statement would display Hello to the screen:


  cout << “Hello”;


While the cout statement uses the insertion operator (<<), the cin statement uses the extraction operator (>>).  For example, the following statement would use the cin stream to allow the user to enter a length:


cin >> length;



Note that by grouping together a cout and cin statement, the user can be prompted with a very specific message, and then enter the data into a variable specified by the programmer.  For example, the following statements are used in conjunction to prompt the user for their age and place the value entered into the variable age:


cout << “Please enter your age in years (example: 21):”;

cin >> age;


One additional item that could be used to better format your output would be a stream manipulator.  A stream manipulator allows the program to manipulate, or manage, the input and output stream characters in some way.  When outputting information to the computer screen, you could use the endl stream manipulator to advance the cursor to the next line on the screen (therefore creating a carriage return and line feed on the screen).  An example where you would like two different lines of text printed on separate lines would be as follows:


cout << “Line One” << endl;

cout << “Line Two” << endl;




Desk-checking the program


Even though it appears that after the program has been coded from the algorithm you are ready to execute it and evaluate, however you can save yourself many hours of grief by taking the time to desk-check the program.  Just like with the algorithm a sample set of data should be used (you could use the same one), and you should step through each program instruction similar to the way the computer will when the program is executed.  Then once the program has been executed, you may compare the results to that of the desk-check for accuracy.




Evaluating and modifying the program


Once the program is ready to be executed, it must be thoroughly tested to make sure it produces correct results.  If for any reason the program produces different results than the desk-check, the program should be debugged.  Debugging refers to the process of locating and removing any errors, commonly called bugs, in a program.  Program errors may be one of two types: syntax errors or logic errors. 


Syntax errors occur when you violate the rules of the language (the syntax of the language).  For example, grammatical errors such as misspellings and incorrect punctuation are considered syntax errors and are relatively easy to fix compared to logic errors.  Logic errors occur when the computer does what you told it to do vs. what you meant for it to do.  For example, if you wished to add two items together you would use the plus sign (+), however if you accidentally hit the asterisk (*) then the two items would be multiplied together.  While this would not be what you intended to occur, it would be syntactically correct and would produce inaccurate results.  It would then be up to you as the programmer to locate and fix the problem (debug the program).




Quick Quiz


1.        After naming the item in an IPO chart, a(n) __________  __________ must be chosen.


2.        There are __________ parts to declaring a variable in C++.



3.        A variable declaration ends with a(n) __________.


4.        __________ are used to perform standard input and output operations.


5.        A(n) __________  __________ allows the program to manipulate, or manage, the input and output stream characters in some manner. 


Creating a C++ Program


Origins of C++


C++ evolved from the procedure-oriented C programming language, which was developed in 1972 at Bell Laboratories by Dennis Ritchie.  In 1985, Bjarne Stroustrup added object-oriented features to the C language.  This enhanced version of the C language was named C++.  It is important to understand that C++ is a superset of C, which means that for the most part everything available in C is available in C++.  Therefore you could use C++ as a procedural-oriented language, as well as an object-oriented language.





To create and execute a C++ program, you need access to a text editor and a C++ compiler.  Both of these are combined in Microsoft Visual C++  into what is called IDE or Integrated Development Environment.  By having these two items integrated vs. separate, there is a great deal of time saved in the development, as well as the debugging part of the programming process.



Source code vs. Object code


In Microsoft Visual C++  the text editor is used to enter the C++ program itself, commonly called the source code.  You will then save the source code on the computer using the filename extension of .cpp (which stands for C plus plus).  This file containing the original program (source code) will be referred to as the source file.


In order for the computer to understand your program it must first be converted to machine language (remember from the previous tutorials that this is the only thing the computer understands).  In order for the computer to convert the source code to machine language it needs a compiler.  The compiler will perform this conversion.  Once you compile your program (source code), the outcome of this step is a set of code called the object code.  The object code will be saved in a file with the same filename as the source code, except the extension will be .obj instead of .cpp.  The file containing the object code will be referred to as the object file.


After the compiler creates the object file, another program called a linker should be invoked.  The linker combines the object file with other machine code necessary for the C++ program to run correctly.  The linker will produce an executable file, which is the code necessary to execute your program without compiling it again.  The executable file will have the same name as the source and object files, except the extension for it will be .exe.




Basic structure of a C++ program


The basic structure of a simple C++ program consists of the following:


·         Documentation about the program

·         #include directive(s)

·         main function


Every program should have internal documentation at the beginning identifying important information about the program, such as the name of the program, author, date, filename where the program is saved, limitations of the program, etc.  This information can save the programmer a great deal of time in the future when referencing the program.


The #include directive is a special instruction for the C++ compiler that tells the compiler to include the contents of another file.  Files that are included in programs are called include files or header files.  We will be creating our own header files in a later tutorial.  The #include directive provides a convenient way to merge the source code from one file with the source code in another file, without having to retype the code.  In C++, the iostream.h header file contains the instructions (source code) needed to handle input and output operations (such as cin and cout). 


Functions will be covered in great detail in later tutorials, however each C++ program has a primary function called main.  This is established with what is called a function header.  A function header begins with the type of function (we will cover more on this later) followed by main ().  After the function header the function code is enclosed in braces {}, marking the beginning and ending of the code.  Within this function you will begin to code the program as developed through the problem-solving process.


Note:  When keying in a C++ program please note that C++ is a case sensitive language.  For example, by capitalizing the I in the #include directive you will cause a syntax error, because C++ is expecting the I to be lower case.




Quick Quiz


1.        It is important to understand that C++ is a __________ of C, which means that for the most part everything available in C is available in C++. 


2.        IDE stands for __________  __________  __________.


3.        Once you compile your program (source code), the outcome of this step is a set of code called the __________  __________. 


4.        The __________ combines the object file with other machine code necessary for the C++ program to run correctly.


5.        The __________ directive is a special instruction for the C++ compiler that tells the compiler to include the contents of another file.

« go back|print