0% found this document useful (0 votes)
67 views

The Activity Aims:: Course Learning Outcomes (Clos)

This document describes an experiment on learning the structure of a basic C++ program. It introduces the typical structure which includes preprocessor directives, global declarations, a main function containing local variables and statements, and additional functions. It then provides code for a "Hello World" program as an example, and line-by-line explanations of what each part of the code is doing. A second experiment is described which modifies the first program to output additional text on separate lines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

The Activity Aims:: Course Learning Outcomes (Clos)

This document describes an experiment on learning the structure of a basic C++ program. It introduces the typical structure which includes preprocessor directives, global declarations, a main function containing local variables and statements, and additional functions. It then provides code for a "Hello World" program as an example, and line-by-line explanations of what each part of the code is doing. A second experiment is described which modifies the first program to output additional text on separate lines.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 16

BHER NIÑO T.

LAVARO
MEC191
EXPERIMENT NO. 1
Structure of a Program

OBJECTIVE:

The activity aims:


1. Familiarized with the structure of a program.
2. Compile and run a basic C++ program

LEARNING OUTCOMES (LOs) COURSE LEARNING OUTCOMES (CLOs)


At the end of the activity, the students should be able to: 1 2 3 4
1. Familiarized with the structure of a
program
2. Compile and run a basic C++ program

COURSE LEARNING OUTCOMES (CLOs)

1. Explain basic information technology concepts and concepts of programming languages


2. Use application software and the Internet properly
3. Use high-level programming languages by demonstrating proficiency in algorithm development;
4. Use the computer as a tool in engineering practice.

MATERIALS/EQUIPMENT NEEDED:

Desktop with C++ Compiler

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:

The form of a C Program:

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 layout of C Programs:

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:

1. Open Dev-C ++ Application.


2. Make a new source file.
Go to File > New > Source File

3. Encode the following program below on the workspace area.

Exp1a.cpp Source Code


1 // my first program in C++
2
3 #include <iostream>
4
5 int main()
6
7 {
8 std::cout << "Hello Engineer!";
9 }

4. Save your program with the file name of Exp1a.cpp.


Go to File > Save
5. Compile your program.
Go to Execute > Compile.

A successful compilation process must have 0


Errors and 0 Warnings like the figure on the
right.

6. Run your program.


Go to Execute > Run

7. Refer to and answer Q1 in the.


8. Modify the program by adding the following lines:

9. Refer to and answer Q2 and Q3 in the Questions Section.

DEBUNKING THE PROGRAM :

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.

Line 2 , 5 and 7 : Blank lines.

Blank lines have no effect on a program. They simply improve readability of the code.

Line 3: #include <iostream>

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.

Line 4: #include <conio.h>

A header file used mostly by MS-DOS compilers to provide console input or output.

Line 6: int main ()


This line initiates the declaration of a function. Essentially, a function is a group of code
statements which are given a name: in this case, this gives the name "main" to the group of code
statements that follow. Functions will be discussed in detail in a later chapter, but essentially, their
definition is introduced with a succession of a type (int), a name (main) and a pair of parentheses (
( ) ), optionally including parameters.
The function named main is a special function in all C++ programs; it is the function called when the
program is run. The execution of all C++ programs begins with the main function, regardless of where
the function is actually located within the code.

Lines 8 and 11: { and }

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.

Line 9: std::cout << "Hello Engineer!";

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.

Line 10: getch ();

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:

1. Make a new source file.

Click the New Source File Icon.

2. Encode the following program below.

Exp1b.cpp Source Code


1 // Exp1b.cpp
2
3 #include <iostream>
4 #include <conio.h>
5
6 int main()
7
8 { std::cout<< "National University \t";
9 std::cout<< "Engineering Department \n";
10 std::cout<< "Go Bulldogs! ";
11 getch();
12 }

3. Save your program with the file name of Exp1b.cpp.

Click the Save Icon or the Save All Icon


4. Compile and Run your program.

Click the Compile Icon then click the Run Icon .


OR

Just click the Compile and Run Icon .

5. Refer to and answer Q4 in the Questions Section.


6. Modify the program by adding \t after the word University in the program.
7. Modify the program by adding \n after the word Department in the program.
8. Refer to and answer Q5 and Q6 in the Questions Section.

DEBUNKING THE PROGRAM:

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 (\).

Here you have a list of the single character escape codes:

Escape code Description


\n newline
\r carriage return
\t tab
\v vertical tab
\b backspace
\f form feed (page feed)
\a alert (beep)
\' single quote (')
\" double quote (")
\? question mark (?)
\\ backslash (\)

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:

1. Make a new source file.


To use the shortcut key, press Ctrl + N .
2. Encode the following program below.

Exp1c.cpp Source Code


1 // Exp1c.cpp
2
3 #include <iostream>
4 #include <conio.h>
5
6 using namespace std;
7
8 int main()
9
10 { cout<< "National University \t";
11 cout<< "Engineering Department \n";
12 cout<< "Go Bulldogs! ";
13 getch();
14 }

3. Save your program with the file name of Exp1c.cpp.


To use the shortcut key, press Ctrl + S .
For saving your program in a different filename, press Ctrl + F12 .
4. Compile and Run your program.
To use the shortcut key, press F9 .
5. Refer to and answer Q7 to Q10 in the Questions Section.

DEBUNKING THE PROGRAM:

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:

using namespace std;


The above declaration allows all elements in the std namespace to be accessed in an unqualified manner
(without thestd:: prefix).

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

Q4. Write the output of the program below.


Q5. How does \t affects the output of the program?
-/t gave space in between national university and engineering department
Q6. How does \n affects the output of the program?
-\n ends the line at department and displace the go bulldogs under on a new line

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

Q9. List the ways of saving your source file.


- Ctrl S , file save as
Q10. List the ways of running your program.
- F11 , F10 , execute run, execute compile and run.
EXERCISES

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.

Exp1_ex2.cpp Sample Output


* ##### *
*** ### ***
***** # *****
******* *******
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

You might also like