Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

Computer Programming Lab 1

The document outlines a lab introduction to C++ programming, detailing objectives such as installation of the C++ environment, compiling and running programs, and understanding C++ syntax. It provides instructions on using the Dev C++ IDE, including opening new files, writing code, and executing programs, along with sample tasks and questions for practice. Additionally, it includes a rubric for evaluating performance in the lab activities.

Uploaded by

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

Computer Programming Lab 1

The document outlines a lab introduction to C++ programming, detailing objectives such as installation of the C++ environment, compiling and running programs, and understanding C++ syntax. It provides instructions on using the Dev C++ IDE, including opening new files, writing code, and executing programs, along with sample tasks and questions for practice. Additionally, it includes a rubric for evaluating performance in the lab activities.

Uploaded by

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

Computer Programming Lab 1

Introduction to C++

Lab Objectives:

1. Installation of C++ environment


2. Compile and Run C++ Program
3. Understand C++ syntax
4. Get basic concepts of Dev C++
5. Using Dev environment for C++ programs.
6. Learn the functionality of Basic Menus of Dev C++.
7. Learn to write and execute a C++ Program in Dev C++.
8. Learn the functionality of program.

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.

Invoking the Dev C++ IDE


The default directory of Dev C++ compiler is C:\Program Files (x86)\Dev-Cpp. So
to invoke the IDE from the windows you need to double click the Devcpp
icon in the directory C\Program Files (x86)\Dev-Cpp. The alternate approach
is double click the DevC+

+ icon on the desktop.


Computer Programming Lab 1

Figure 1

Opening New Window in Dev C++:

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

Let us look at the various parts of the above program −

Line 1: //my first program in C++

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.

Line 2: #include <iostream>

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!"; )

This s ta te me n t has three parts: First , s t d : : c o u t , which i dentifies the


standard character output device (usually, this is the computer screen). Second, the insertion
operator (<<), which indicates that what follows is inserted into std::cout. Finally, a sentence
within quotes ("Hello world!"), is the content inserted into the standard output.
Computer Programming Lab 1

Notice that the statement ends with a semicolon (;). All C++ statements must end with a
semicolon character.
Statement terminator (;)

A programming statement must be terminated by a semi-colon (;), just like an English


sentence ends with a period.
Line 6: return 0;

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

Exiting Dev C++ IDE

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 1: Compile and Run the Following Program

TASK 2: Compile and run the Following program.

Task 3: Write a C++ program to print 'Hello' on screen and then print your name on a separate
line.

Task 4: Run following code:


Computer Programming Lab 1

Task 5: Take your first and last name as separate inputs and then display them in one single
line.

Task 6: Write a C++ program to display the following pattern.

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

Hint: use std::endl to output a line break.

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 1: What is a compiler?

Question 2: Run the following program segment, and find errors. Then remove the errors and
execute the program.

Include <iostream>
Int main ()
{

std:cout>>”I am trying to find bugs”;


}
Computer Programming Lab 1

Name: Roll No:

Score Card
Viva Marks Report Marks Total Marks

Instructor Signature: ___________________________

Lab Rubrics

Sr. Performance Excellent Good Fair Poor (0 Marks)


# Indicator (5 Marks) (4-3 Marks) (2-1 Marks)
Skill to Quite able to Able to conduct Able to conduct and Unable to
perform test conduct and and demonstrate demonstrate the conduct and
experiment demonstrate the the problem- problem-based task demonstrate the
(PLO-05) problem-based task based task with with insufficient problem-based
1 (P-4) with complete sufficient implementation task.
implementation and implementation and inadequate
results but inadequate results
interpretation. results Interpretation.
Interpretation.
Scale

Sr. Performance Excellent Good Fair Poor (0 Marks)


# Indicator (5 Marks) (4-3 Marks) (2-1 Marks)
Data Correctly analyses Analyses and Analyses and Fails to analyze and
exploration and interprets the interprets data interprets data interpret data.
and analysis data with useful with basic level of at basic level
2
conclusions. conclusions. without useful
(PLO-04) conclusions
(C-4)
Scale

You might also like