Lab1 (GB) Computing
Lab1 (GB) Computing
1. Invoke the editor, create a simple C++ program file and save that file.
2. Retrieve the file mentioned above and modify it.
3. Enter a C++ program and execute it.
Equipment/Apparatus:
Step 1. The following document is a simple program in the C++ programming language.
Using the editor in your particular software development environment, type the
program as it appears here and save it for future reference. Be careful to include all of
the punctuation marks and the braces.
// This is our first C++ program.
#include <iostream>
int
main()
{
cout << "What is your favorite\n";
cout << "flavor of ice cream?\n";
return
0; }
Step 2. Retrieve the program from Step 1 and compile it. If the compiler finds errors
(which would be typing errors), correct them and try again. Execute the final, correct
program. What happens?
Step 3. In case you didn't have any typing errors on your first attempt, we'll introduce
some now. Change the word cout in the source program to couts and try to compile the
altered version. How does your compiler inform you of this error?
Compiler inform that Error: ‘couts’ was not declared in this scope.
Step 4. Correct the error introduced in Step 3, and then remove the semicolon from the
end of the line
cout << "What is your favorite\n";
Try to compile this altered version. How does your compiler respond?
is a comment. Comments have a variety of uses. They can be inserted to clarify a section of
a program that might otherwise be hard to understand. They can also be used to give
information about the creation of a program such as the date created and by whom.
The statement
#include <iostream>
in our example program is an example of a preprocessing directive. These directives cause
the source program to be modified before the compiling process begins and are
signified by beginning with a # symbol. The directive in our example causes a copy of
the standard header file named iostream to be inserted at the beginning of the source code
when compilation occurs. This file contains information that the compiler will need to
perform its task. In particular, our example refers to the special object cout (which we will
explain shortly) through which information can be sent to the monitor screen. Our
program does not contain the details of cout but merely communicates with the object
by means of the operator <<. The file ios tream contains the information needed by the
C++ compiler to create the required link between our program and the system library.
Names of standard header files are enclosed by angle brackets, < and >, as in our
example. Names of nonstandard header files, such as those you may write yourself, are
enclosed in quotation marks, as in
#include "HomeMade . h"
A C++ program consists of units called functions. (We will learn about functions
shortly.) Every program contains at least one function called main. Execution of a C++
program always begins in the function main. That is, the function main represents the
beginning of the program even though the function may appear much later in the written
program. The line
int main()
in our example indicates the beginning of the function main. This opening line of a
function is known as a function header. We'll learn more about function headers in
later laboratory sessions. For now we note that such a header consists of three parts: a
return type (in our example int), the name of the function (in our example main), and a
parameter list (in our example an empty parenthetical expression). The fact that the
function header in our example has a return type of int indicates that the operating system
should expect to receive a numeric (integer) value when our program terminates. This value is
used to report whether our program executed successfully. We'll return to this point shortly.
The actual "meat" of a function is placed between braces. Immediately following the
opening brace is the declarative part of the function. It is here the terminology relating to that
particular function is introduced. Our example program is so simple that it does not contain
a declarative part. Instead, the function main in our example consists of only a procedural
part—the part containing the instructions to be followed when the function is executed.
The procedural part of a function always follows the declarative part.
Statements in a C++ program are terminated by semicolons. Although not mandatory, it is
customary to place each statement on a separate line and to use indentation to help the
reader identify related portions of the program.
Each statement in the procedural part of our example program uses the predefined
object cout and the operator <<. The object cout is just one of many standard units that
are so commonly used in programs that C++ provides them for your use in standard C++
libraries. You will learn more about cout in later laboratory sessions. For now we need
merely note that the statement.
The program expects primary-expression before ‘<<’ token. The “c” in cout refers to
“character” and “out” means output. Cout means character output and cout is used with
the “<<” in order to display a stream of characters.
Experiment 1.3
Step1. Insert the following lines into the procedural part of the function main in Experiment
1.1. Explain how the output produced by each of these statements differs from the
others.
cout << "Chocolate, butterscotch, strawberry, vanilla?\n";
cout << "Chocolate, butterscotch,\nstrawberry, vanilla?";
cout << "Chocolate, butterscotch\n\nstrawberry, vanilla?"
The first line statement is ending with “\n” at the end of the statement and
results a newline is introduced after the statement. While the “\n” is inserted in
the middle of the second statement and results the statement behind “\n” is in
the newline. The third statement is connecting with the back of second
statement because there is no new line at second statement. There is two “\n” in
the middle of the third statement and it results two blank lines in between.
Experiment 1.4
Insert the following lines into the procedural part of the function main in Experiment 1.1. What
rule can you derive about the placement of comments?
cout << "Send money quick!\n"; // To Mom!
cout << "Send money quick!\n" // To Mom! );
cout << "Send // To Mom! money quick!\n");
“//” is a forward slash, which is a function to write comments to the statements. Anything can
be written in the comments, but C++ program does not run them. The first statement with the
semicolon is placed before the forward slash which is correct. The semicolon is after the
comment and causing the program cannot recognize it. Hence, the semicolon needs to be
placed in front of the comment. For the third statement, the forward slash is placed inside the
double quote, which means it is not a comment rather a statement to be displayed on the
screen.
Experiment 1.5
Step 1. Insert the following lines into the procedural part of the function main in Experiment
1.1. What rule can you derive about printing sentences containing quotation marks?
cout << "Oh, I love to program in C++. . . \n";
cout << "Oh, I love to "program" in C++. . . \n";
cout << "Oh, I love to ""program"" in C++. . . \n";
cout << "Oh, I love to \"program\" in C++. . . \n";
The compiler shows the error expected ‘;’ before ‘program’
```````````````````````````````````````````````````````````
`````````````````````````````````````````````;
This compiler shows the double quotation because it pair with backslash.
Backslash mark is used as a delimiter to begin a two-character escape sequence. When a backslash is
placed in front of a group of characters, it tells the compiler to escape from the way these characters
are normally interpreted. The combination of a backslash and these characters is called an escape
sequence.
Post-Laboratory Problems
1.1. Design an algorithm and write a program that prints the message
My name is xyz,
I like EE209 Computing for Engineers.
b. on two line
(
Celsius (TC) Recall that TC =( TF−32 )∗ 9( ))
5
1.1. Design an algorithm and write a program that prints the message
My name is xyz,
I like EE209 Computing for Engineers.
1.3. Design an algorithm and Write a program that converts degree Fahrenheit (TF) to
(
degrees Celsius (TC) Recall that TC=( TF−32 )∗ 9
5
( ))
Choose the suitable variables type.
The program should ask the user to enter the degree Celsius (TC).
Your program should also output your name and student ID number at
the top.
Sample Output: