Computer Programming Lab 1
Computer Programming Lab 1
Introduction to C++
Lab Objectives:
Software Required:
Dev C++
A Typical Dev C++ Environment
Dev C ++ is the most famous and easy to use IDE for learning C++. It has all you need
for C++ development. IDE is nothing but Integrated Development Environment in
which one can develop, run, test and debug the program. The C++ Developing
Environment is a screen display with windows and pull-down menus. The program
listing, error messages and other information are displayed in separate windows.
The menus may be used to invoke all the operations necessary to develop the
program, including editing, compiling, debugging and program execution.
Figure 1
To start a new program, you need to click on New button and select
Source file from the menu. You can also open file menu and click “new” >
source file to open a new window to
write a program. A third method is to use the shortcut key Ctrl+N to open a
new program window. A window will appear on the screen where the program
can be written.
Computer Programming Lab 1
Program Structure
Any line starting with ‘//’ in a program, is known as a Comment, and it doesnot effect the
program execution. Comments are used to insert short explanations on what the program is
doing, and to increase the program readability.
Lines beginning with ‘#’ sign, are known as Preprocessor directives. i.e., they tell the processor
how to execute the code below.
In this case, the directive is telling the processor to include the header file
<iostream> (i.e., input output stream). This “iostream” is responsible for standard input and
output operations we do in the code. For example: writing the output of the code to the
screen.
Line 3: int main()
The function named ‘main’ is a special function in all C++ programs; it is the function called
when the program is executed. The execution of all C++ programs begins with the main
function, regardless of where the function is actually located within the code. Line 4 & 7:
Delimiters { }
‘{’ denotes the opening of the main function, and ‘}’ denote the closing. 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 5: Body
of main function (std::cout<<"Hello World!"; )
Notice that the statement ends with a semicolon (;). All C++ statements must end with a
semicolon character.
Statement terminator (;)
The return value of 0 indicates normal termination; while non-zero (typically 1) indicates
abnormal termination. C++ compiler will automatically insert a "return 0;" at the end of the
the main()function, thus this statement can be omitted.
Using “using namespace std;”
Namespaces allows us to group a set of global classes, objects and/or functions under a
name. If you specify using namespace std then you don't have to put std:: throughout
your code. The program will know to look in the std library to find the object. Namespace std
contains all the classes, objects and functions of the standard C++ library.
The names cout and endl belong to the std namespace. They can be referenced via fully
qualified name std::cout and std::endl, or simply as cout and endl with a "using namespace
std;"statement.
Save, Compile and Run
Note: First make a folder to save the source files. We can make this folder of any name and
locate it in any directory. For example: Make a new folder named “Executables” and save it
on the desktop. All the source files we make in Dev C++, will be saved in this folder. This is
done once.
To Save, Compile and Run the program, press the menu bar button . Dev C++ will show
an option to save the program first, Save the Source file in the folder we
created earlier. After saving the file the program will automatically compile and run to give
the following output.
Computer Programming Lab 1
A Program window may be closed in a number of different ways. You can click on the menu
bar button to close the source file, you can select close from the file menu, or
you can press the short cut key Ctrl + W.
To exit from the IDE, select Exit from the File Menu or press Alt+f4, or press the close button
on top right corner of the IDE
TASKS:
Task 3: Write a C++ program to print 'Hello' on screen and then print your name on a separate
line.
Task 5: Take your first and last name as separate inputs and then display them in one single
line.
Sample Pattern :
J a v v a
J aa v v aa
J J aaaaa V V aaaaa
J J a a V a a
TASK 7: Write a program to display the following output using a single cout statement.
Subject Marks
Mathematics 90
Computer 77
Chemistry 69
TASK 8: Write a C++ Program in Dev C++ to produce the following output.
Hello World!!
I am a Programmer!
Computer Programming Lab 1
TASK 9: Write a C++ program to print the following arrow on the screen using cout
statements:
***
*****
TASK 10: Write a program in C++ that displays the following output:
*****
***** *****
***** *****
***** *****
***** *****
***** *****
##############################
##############################
##############################
Computer Programming Lab 1
Question 2: Run the following program segment, and find errors. Then remove the errors and
execute the program.
Include <iostream>
Int main ()
{
Score Card
Viva Marks Report Marks Total Marks
Lab Rubrics