The Activity Aims:: Course Learning Outcomes (Clos)
The Activity Aims:: Course Learning Outcomes (Clos)
LAVARO
MEC191
EXPERIMENT NO. 1
Structure of a Program
OBJECTIVE:
MATERIALS/EQUIPMENT NEEDED:
SAFETY GUIDELINES:
1. Make sure you have both an adjustable table and chair so that ergonomic accommodations can be
made for each person using the computer.
2. The computer screen should be front and centre so neck turning is unnecessary.
3. Keep your lab space clean and organized.
4. Clean your lab bench and equipment, and lock the door before you leave the laboratory.
5. Never eat, drink, or smoke while working in the laboratory.
6. DO NOT TOUCH ANYTHING WITH WHICH YOU ARE NOT COMPLETELY FAMILIAR!!! It is
always better to ask questions to laboratory technicians or to your instructors than to risk harm to
yourself or damage to the equipment.
DISCUSSION:
All C programs will consist of at least one function, but it is usual (when your experience grows) to write a C
program that comprises several functions. The only function that has to be present is the function called main.
For more advanced programs the main function will act as a controling function calling other functions in their
turn to do the dirty work!
The main function is the first function that is called when your program executes.
C makes use of only 32 keywords which combine with the formal syntax to the form the C programming
language. Note that all keywords are written in lower case - C, like UNIX, uses upper and lowercase text to
mean different things. If you are not sure what to use then always use lowercase text in writing your C
programs. A keyword may not be used for any other purposes. For example, you cannot have a variable
called auto.
The general form of a C program is as follows (don't worry about what everything means at the moment -
things will be explained later):
preprocessor directives
global declarations
main()
{
local variables to function main ;
statements associated with function main ;
}
function1( )
{
local variables to function 1 ;
statements associated with function 1 ;
}
function2( )
{
local variables to function function 2 ;
statements associated with function 2 ;
}
.
.
.
etc
Note the use of the bracket set ( ) and { }. ( ) are used in conjunction with function names whereas { } are
used as to delimit the C statements that are associated with that function. Also note the semicolon - yes it is
there, but you might have missed it! a semicolon (;) is used to terminate C statements. C is a free format
language and long statements can be continued, without truncation, onto the next line. The semicolon informs
the C compiler that the end of the statement has been reached. Free format also means that you can add as
many spaces as you like to improve the look of your programs.
A very common mistake made by everyone, who is new to the C programming language, is to miss off the
semicolon. The C compiler will concatenate the various lines of the program together and then tries to
understand them - which it will not be able to do. The error message produced by the compiler will relate to
a line of you program which could be some distance from the initial mistake.
EXPERIMENT 1.1:
PROCEDURES:
If you followed the procedures correctly, you will have a program similar to the figure below.
Now let us examine this program line by line:
Line 1: // my first program in C++
Two slash signs indicate that the rest of the line is a comment inserted by the programmer but which
has no effect on the behavior of the program. Programmers use them to include short
explanations or observations concerning the code or program. In this case, it is a brief introductory
description of the program.
Blank lines have no effect on a program. They simply improve readability of the code.
Lines beginning with a hash sign (#) are directives read and interpreted by what is known as
the preprocessor. They are special lines interpreted before the compilation of the program itself
begins. In this case, the directive #include <iostream>, instructs the preprocessor to include a section
of standard C++ code, known as header iostream, that allows to perform standard input and output
operations, such as writing the output of this program (Hello World) to the screen.
A header file used mostly by MS-DOS compilers to provide console input or output.
The open brace ({) at line 8 indicates the beginning of main's function definition, and the
closing brace (}) at line 11, indicates its end. Everything between these braces is the function's
body that defines what happens when main is called. All functions use braces to indicate the
beginning and end of their definitions.
This line is a C++ statement. A statement is an expression that can actually produce some effect.
It is the meat of a program, specifying its actual behavior. Statements are executed in the same order
that they appear within a function's body.
This statement has three parts: First, std::cout, which identifies the standard character output
device (usually, this is the computer screen). Second, the insertion operator (<<), which indicates
that what follows is inserted intostd::cout. Finally, a sentence within quotes ("Hello world!"), is the
content inserted into the standard output.
A member function of conio.h that reads a character directly from the console without buffer, and
without echo.
Notice that the statement ends with a semicolon (;). This character marks the end of the statement, just
as the period ends a sentence in English. All C++ statements must end with a semicolon character. One of
the most common syntax errors in C++ is forgetting to end a statement with a semicolon.
EXPERIMENT 1.2
PROCEDURES:
Character and string literals can represent special characters that are difficult or impossible to express
otherwise in the source code of a program, like newline (\n) or tab (\t). These special characters are all of
them preceded by a backslash character (\).
For example:
‘\n’
‘\t’
“Left \t Right”
“one\ntwo\nthree”
Internally, computers represent characters as numerical codes: most typically, they use one extension of
the ASCII character encoding system. Characters can also be represented in literals using its numerical code
by writing a backslash character (\) followed by the code expressed as an octal (base-8) or hexadecimal
(base-16) number. For an octal value, the backslash is followed directly by the digits; while for hexadecimal,
an x character is inserted between the backslash and the hexadecimal digits themselves (for
example: \x20 or \x4A).
EXPERIMENT 1.3
PROCEDURES:
If you have seen C++ code before, you may have seen cout being used instead of std::cout. Both name the
same object: the first one uses its unqualified name (cout), while the second qualifies it directly within
the namespace std (as std::cout).
cout is part of the standard library, and all the elements in the standard C++ library are declared within what
is a called a namespace: the namespace std.
In order to refer to the elements in the std namespace a program shall either qualify each and every use of
elements of the library (as we have done by prefixing cout with std::), or introduce visibility of its components.
The most typical way to introduce visibility of these components is by means of using declarations:
Both ways of accessing the elements of the std namespace (explicit qualification and using declarations) are
valid in C++ and produce the exact same behavior. For simplicity, and to improve readability, the examples
in these tutorials will more often use this latter approach with using declarations, although note that explicit
qualification is the only way to guarantee that name collisions never happen.
QUESTIONS:
Experiment 1.1
Q1. Discuss what happened when you run the program on procedure 6?
- A window appeared and displayed the output text.
Q2. Explain the use of each of the added line in the program:
-Line 1 comment for readability line 2 for space line 3 iostream is a header used for basic input and
output, line 4 for space, line 5 main function is the first group of codes that the program will read
regardless of where it is placed ,line 6 for space, line 7 curly brace({) indicates the start of the program
line 8 cout is for displaying text, line 9 indicates the end of the program
Q3. Write the output of the program below.
Experiment 1.2
Experiment 1.3
Q7. Compare the program and outputs of Experiment 1.2 and 1.3.
- In experiment 1.2 using namespace std wasn’t declared before the main function thus having to
type std before every cout and in experiment 1.3 using name space allowing the programmer to
type std before every cout their output are the same.
Q8. List the ways of creating a new source file.
- File new source file , ctrl n
1. Create a program that will display your basic information Exp1_ex1.cpp Sample Output
and save it as Exp1_ex1.cpp. Masangkay, Pedrito X.
BsCpE – COE111
0921-447-8888
Lastname, Firstname, Middle Initial
Program – Section
Contact Number
Exp1_ex1.cpp
2. Create a program that will display the figure shown on the right and save it as Exp1_ex2.cpp.
3. Create a program that will display the full National University Hymn save it as Exp1_ex3.cpp. Write
the program below.
Exp1_ex3.cpp