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

Lab Practice 1

Uploaded by

OPOKA ALBERT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

Lab Practice 1

Uploaded by

OPOKA ALBERT
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

LAB PRACTICE 1

C++ Hello World Program

C++ Hello World Program


Printing the message “Hello World” to the standard output is a classic program that everyone
writes when they start learning a programming language.

In this tutorial, we shall write a basic C++ program that prints a message to output.

First Program
The following is a C++ program that prints Hello World to standard console output.
main.cpp
// C++ program to display "Hello World"
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}
Name of the above C++ file is main.cpp . Extension of a C++ file is .cpp .
Let us go into the program and understand it line by line.

Note: Believe it or not, the detailed analysis and explanation of this example is long. We can go
into it right now, but we will be no wiser at this point as we first need to know what headers,
streams, objects, operators, and string literals are. Do not worry. We will get there.

A brief(ish) explanation

// C++ program to display “Hello World”:

This line is a comment line. A comment is used to display additional information about the
program. A comment does not contain any programming logic. When a comment is encountered
by a compiler, the compiler simply skips that line of code. Any line beginning with ‘//’ without
quotes OR in between /*…*/ in C++ is comment
1|Page
@myco…TCBE 2202 Computing for Civil Engineering
Line 1: #include <iostream>
An include statement. We are including iostream header file to use the functionalities in it. There
are many header files that come included with C++. You can include one header file using one
include statement. To include multiple header files in your program, write an include statement for
each one of them.
You can also create a header file of your own, in which you define some functionalities. And then
you can include that header in your program.

Line 2: using namespace std;

Using namespace statement. Namespaces is kind of grouping of variables under a single name. In
this statement we specified that we shall use the namespace named std . std has variables defined
like cout which we used in the program in line 5. If you are not writing this using namespace
statement, you have to write std::cout instead of cout . While going forward, you will see that
writing this namespace statement is useful to ease out the code.

For example:
using namespace std;
is used to provide standard (std) input and output namespaces. That is, after including this
statement, we do not need to write std:: before every cout and cin.

Line 3 – An empty line. You can write empty lines in your C++ program. This lets you have some
space between your statements and function. Just to increase the readability of program. Empty
lines or spaces are ignored by C++ compiler.

Line 4: int main() {


A function definition. We are defining a function named main that returns an int eger and accepts
no arguments. You will see main() in most of our C++ programs. This is because main() function
is kind of an entry point to the execution of a C++ program. We also have a flower brace that starts
the scope of this main() function.

int main(): This line is used to declare a function named “main” which returns data of integer
type. A function is a group of statements that are designed to perform a specific task. Execution
of every C++ program begins with the main() function, no matter where the function is located
in the program. So, every C++ program must have a main() function.

Main is a built-in Library functions. In simple terms, a function is a block of code that only runs
(performs a specific task) when it is called.

The main function is a special function. Every C++ program must contain a function named
main. It serves as the entry point for the program. The computer will start running the code from
the beginning of the main function. Main contains a block of code for starting to run the code.
2|Page
@myco…TCBE 2202 Computing for Civil Engineering
Line 5: cout << "Hello World!";
cout can be used to write an object or value to standard output.

Line 6: }
Ending flower bracket. This closes the scope of main function. Body of main() function, which
includes all the statements inside it, should be enclosed between flower braces.

Writing First C++ Program – Hello World Example:

// C++ program to display "Hello World"

// Header file for input output functions


#include <iostream>
using namespace std;

/* Main() function: where the


execution of program begins*/

int main()
{
// prints hello world

cout << "Hello World";

return 0;
}

Output
Hello World
Conclusion
In this C++ Tutorial, we learned how to write a basic hello world program in C++.

Exercise:
Writing a C++ Program to display your Name, Date of birth, Reg No, Student No., Phone No.
and Email address on separate lines. Note: The name should be in Capital Letters.

3|Page
@myco…TCBE 2202 Computing for Civil Engineering
Output via "cout << " – How to use endl (END-of-Line)
Note:
A special symbol called endl (END-of-Line) can be used to produce a newline. Whenever an
endl is printed, there is no visible output, but the cursor advances to the beginning (left-margin)
of the next line. A string, which is enclosed by a pair of double quotes, will be printed as it is,
including the white spaces and punctuation marks within the double quotes. Integers (such as 1,
2, 3) and floating-point numbers (such as 1.1, 2.2) can be printed too.
For Example:

cout << "hello" << " world, " << "again!" << endl;
cout << "hello," << endl << "one more time. " << endl << 5 << 4 << 3 << " " << 2.2 << " " << 1.1 << endl;

The output for the above two output statements is as follows where the underscore denotes the
final cursor position.
hello world, again!
hello,
one more time.
543 2.2 1.1
_

Beside the endl, you can also use '\n', which denotes a newline character, to advance the cursor to
the next line. Similarly, you could use '\t', which denote a tab character, to advance the cursor to
the next tab position. '\n' and '\t' are known as escape sequences representing ASCII codes
Hex 0A (line-feed) and Hex 09 (tab), respectively. For example,
cout << "hello world, again!\n";
cout << "\thello,\none\tmore\ttime.\n";

The output shall look like (the exact tab stop positions depend on your system's setting - eight
spaces is used here):
hello world, again!
hello,
one more time.
_

Notes : I strongly recommend that you use endl to print a newline, instead of '\n'. This is
because line delimiter is system dependent: Windows use "\r\n"; UNIX/Linux/Mac use '\n'.
The endl produces system-specific newline. Furthermore, endl guarantees that the output is
flushed; while '\n' does not.
4|Page
@myco…TCBE 2202 Computing for Civil Engineering
Exercises
1. Write programs called PrintPatternX.cpp (where X from A to D) to print EACH of the
following patterns. Use one "cout <<" for each line of outputs. End each line by printing an
"endl".
* * * * * * * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * * *
(a) (b) (c) (d)

2. Print the above patterns using ONE "cout <<" statement.

Hint:
Answer: 1(a)
#include <iostream>
using namespace std;
int main()
{
cout << "*****" << endl;
cout << " *****" << endl;
cout << "*****" << endl;
cout << " *****" << endl;
cout << "*****" << endl;
return 0;
}

Answer: 2(a)
#include <iostream>
using namespace std;

int main()
{
cout << "*****" << endl << " *****" << endl << "*****" << endl << " *****" << endl << "*****" << endl;

return 0;
}

5|Page
@myco…TCBE 2202 Computing for Civil Engineering
Programming Exercises
Exercise 3: Write a program to print a block E using asterisks (*), where the E is 7 characters high
and 5 characters wide.
Exercise 4: Write a program to print your name, National Identification Number, and date of birth.

Exercise 5: Write a program to compute the area and circumference of a rectangle 3 inches wide
by 5 inches long. What changes must be made to the program so it works for a rectangle 6.8 inches
wide by 2.3 inches long?

Exercise 6: Write a program to print "HELLO" in big block letters where each letter is 7
characters high and 5 characters wide.

Exercise 7: Write a program that converts Celsius to Fahrenheit.


9
F = C+32
5
4
Exercise 8: Write a program to calculate the volume of a sphere: 3 𝜋𝑟 3.

Exercise 9: Write a program to print out the perimeter of a rectangle given its height and width.
Perimeter = 2 (width + height)

Exercise 10: Print a checkerboard (8-by-8 grid). Each square should be 5-by-3 characters wide. A
2-by-2 example follows:

Note: First create a flowchart where possible to depict the logical flow of the program!

6|Page
@myco…TCBE 2202 Computing for Civil Engineering

You might also like