You are on page 1of 23

LABORATORY MANUAL C++

Programming Fundamentals CSCO-1201

Abstract
Programming Fundamental all labs and practice are available in this manual and we will learn
all the content by practically every student having a responsibility to perform all the labs at
their own and ask any question under sign.

shoaib nawaz
Shoaib.03339770257@gmail.com
Table of Content

Lab-01 .................................................................................................................................. 1
Lab-02 ................................................................................................................................ 11
Lab-03 ................................................................................................................................ 22
Lab-04 ................................................................................................................................ 35
Lab-05 ................................................................................................................................ 47
Lab-06 ................................................................................................................................ 57
Lab-07 ................................................................................................................................ 73
Lab-08 ................................................................................................................................ 85
Lab-09 ................................................................................................................................ 96
Lab-10 .............................................................................................................................. 107
Lab-11 .............................................................................................................................. 116
Lab-12 .............................................................................................................................. 122
Lab-13 .............................................................................................................................. 135
Lab-14 .............................................................................................................................. 145
Lab-15 .............................................................................................................................. 152
Programming
Fundamentals
Lab-01

Programming Fundamentals - Lab [COSC-1201] 1


1.1.OBJECTIVES
1. Get basic concepts of Dev C++
2. Using Dev environment for C++ programs.
3. Learn the functionality of Basic Menus of Dev C++.
4. Learn to write and execute a C++ Program in Dev C++.
5. Learn the functionality of program.

1.2. 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.

1.2.1. 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.

Figure 1

Programming Fundamentals - Lab [COSC-1201] 2


1.2.2. 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.

1.3. General Syntax for C++ Programs

The basic C++ construct for writing a program is

2. Preprocessor 1. Comment 5. Program

1. //my first program in C++


3. Main 2. #include <iostream>
3. int main() Body of Function
4. {
5. std::cout<<"Simple C++ Program!";
4, 7 6. return 0;
7. }

Statement terminator

Describing line by line 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.

Programming Fundamentals - Lab [COSC-1201] 3


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 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
into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content
inserted into the standard output.
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.

Programming Fundamentals - Lab [COSC-1201] 4


1.4. 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.
1.5. 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.
1.6. 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.

1.6. Lab Activities

1.6.1. Program No 1
Compile & Run the following code, and write its output:
#include<iostream>
int main()
{
std::cout<<"welcome";
}

Programming Fundamentals - Lab [COSC-1201] 5


Output of Program No 1:

1.6.2. Program No 2

Write a C++ Program in Dev C++ to produce the following output.


Hello World!!!
I am a C++ Program.
Hint: use std::endl to output a line break.

Code of Program No 2:

1.6.3. Program No 3

Write a C ++ Program in Dev C++ to produce the following output without using
the std:: prefix.
Hello World!!!
I am a C++ Program

I am learning to put line spaces in output

Hint: Use “using namespace std;” after the preprocessor directive line.
Code of Program No 3:

Programming Fundamentals - Lab [COSC-1201] 6


1.6.4. Program No 4
Write a C++ program to display the following output using single cout statement:
*
**
***
****

Code of Program No 4:

1.6.5. Program No 5

Write a C++ program to print the following arrow on the screen using cout statements:

Programming Fundamentals - Lab [COSC-1201] 7


***
*****
*
*
*

Code of Program No 5:

1.6.6. Program No 6

Write a program in C++ that displays following output:


*****
***** *****
***** *****
***** *****
***** *****
***** *****
##############################
##############################
##############################

Code of Program No 6:

Programming Fundamentals - Lab [COSC-1201] 8


1.6.7. Program No 7

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”;
}

Code of Program No 7:

Programming Fundamentals - Lab [COSC-1201] 9


Exercise Questions:

1. What statement allows the short names cout and endl to be used
instead of std::cout and std::endl?

2. What does the name std stand for?

3. All C++ programs must have a function named what?

4. The body of main function is enclosed within what symbols?

5. What operator directs information to the std::cout output stream?

6. What are namespaces. Why we use namespace std in our code.

Programming Fundamentals - Lab [COSC-1201] 10


Programming
Fundamentals
Lab-02

Programming Fundamentals - Lab [COSC-1201] 11


2.1. OBJECTIVES

1. Variable Declaration
2. Variable Initialization
3. Datatypes
4. Expression
5. Limits.h Header File
6. Sizeof Operator
7. Compound Assignment Statement
8. Compound Assignment Operator

2.2. VARIABLES

A Variable is a location in the computer memory where a value can be stored for use by a
program.

2.2.1. Variable Declaration


All variables must be declared with a name and a data type before they can be used in a
program.
Example: ‘int’ is the datatype, which means that the variable named ‘number1’ holds integer
value.
int number1;

In the similar fashion, we can declare two or more variables like:


int number1;
int number2;
int number3;

Or, variables with similar datatype can be declared as:


int number1, number2, number3;

2.2.2. Variable Initialization


The process of assigning a value to a variable at the time of declaration is known as variable
initialization. There are three ways to initialize a variable in C++.

1. Type variablename = value;


Example: int number = 10; // initializing number with 10.

2. Type variablename(value);
Example: int number(10);

3. Type variablename{value};
Example: int number{10};
All the examples above are initializing the variable number from 10.

Rules:
1. A variable name is a series of characters consisting of letters, digits and underscores,
that doesn’t begin with a digit.

Programming Fundamentals - Lab [COSC-1201] 12


2. C++ is a case sensitive language, so the variable name A1 and a1 are two different
variable names.
3. Reserved words like int, main, cout cannot be used as variable names.

2.3. Datatypes

Following Datatypes are available in C++

1. Char
2. Int
3. Float
4. Double

The following table gives details of their size and ranges:


Datatype Description Size Range
Character Datatype
Char Any Single character. It may include 1 byte -128 to 127 or 0 to
a letter, a digit, a punctuation mark 255
or a space.
Signed char Signed Character 1 byte -128 to 127
Unsigned char Unsigned char 1 byte O to 255
Integer datatype
Int Integer 4 bytes -2147483648 to
2147483647
Signed int Signed integer. Values may be 4 bytes -2147483648 to
negative, positive or zero 2147483647
Unsigned int Values are always positive or zero. 4 bytes 0 to 4294967295
Never negative
Short 2 bytes -32768 to 32767
Signed short Values may be negative, positive or 2 bytes -32768 to 32767
zero
Unsigned Values are always positive or zero. 2 bytes 0 to 65535
short Never negative
Long Long integer 4 bytes -2147483648 to
2147483647
Signed long Signed long integer 4 bytes -2147483648 to
2147483647
Unsigned long Unsigned long integer. Values are 4 bytes 0 to 4294967295
always positive or zero. Never
negative
Floating Point Datatype
Float Floating point number. There is no 4 bytes 3.4x10-38 to
fixed number of digits before or after 3.4x10+38
the decimal point. It provides
precision of 6 decimal places

Double Double precision floating point 8 bytes 1.7x10-308 to

Programming Fundamentals - Lab [COSC-1201] 13


number. More accurate compared to 1.7x10+308
float. Used to store large real values.
Provides precision of up to 15
decimal places.
Long double Long double precision floating point 16 bytes 1.7x10-4932 to
+4932
number. Used to store very large real 1.7x10
values. Provides precision of up to
19 decimal places.
Boolean Data Type
Bool Boolean value. It can only take one 1 byte True or false
of two values: true or false

2.4. Limits.h Header File

The limits of all datatypes can be found in header file ‘limits.h’ header file. To check limits
we can use the following commands. For example “cout<<CHAR_MIN<<endl;” will give -
128 output.
Datatype Minimum Value MaximumValue
Char CHAR_MIN CHAR_MAX
Int INT_MIN INT_MAX
Short SHRT_MIN SHRT_MAX
Long LONG_MIN LONG_MAX
Unsigned long - ULONG_MAX
Float FLT_MIN FLT_MAX
Double DBL_MIN DBL_MAX
Long double LDBL_MIN LDBL_MAX

2.5. Expressions
A statement that evaluates to a value is known as expression. It consists of operands and
operators.
Example:
A+B;
In the above statement, + is the operator, A and B are operands

2.6. Sizeof Operator


The sizeof operator is used to find the size of any data value. It gives the number of bytes
occupied by that value.
Syntax: sizeof(operand)
The operand can be a variable or a constant.
Example: sizeof(n); //where n is a variable name

2.7. Compound Assignment Statement


An assignment statement that assigns a value to many variables is known as compound
assignment statement. Compound assignment statement does not work when we are declaring
the same variable. i.e., (int a=b=0; // this statement will not work) A=B=0;
2.8. Compound Assignment Operator
Compound Assignment Operator combines assignment operator (=) with arithmetic operators

Programming Fundamentals - Lab [COSC-1201] 14


(+, -, /, *).
A +=5;
A-=2;
A*=3;
A/=4;

Example Program:

Write a Program that add two floating point numbers and print their result.
#include <iostream>
using namespace std;

int main(){
float number1=2.5;
float number2=2.5;
float sum ;
sum=number1+number2;
cout<<"The sum of "<<number1<<" and "<<number2<<"is "<<sum;

2.9. LAB Work

2.9.1. Expression Evaluation

Evaluate the following expression in its order of precedence:


10*(24/(5–2))+13

Step Operator Reduced expression

2.9.2. Program No 2
Write a program to determine the value of following expression, where a =1.5, b=1.1, c=2.89
2+4ab*c+5

Code of Program No 2:

Programming Fundamentals - Lab [COSC-1201] 15


2.9.3. Program No 3
Write a program to calculate and print the area of a square.

Code of Program No 3:

Programming Fundamentals - Lab [COSC-1201] 16


2.9.4. Program No 4
Write a program in C++, which takes radius from the user and calculate the area of sphere
i.e. Area= 4pr2
Hint p=3.1416
Area= 4*3.1416*r*r

Programming Fundamentals - Lab [COSC-1201] 17


2.9.5. Program No 5
Write a program that include limits header file, and show the max and min limits of
all datatypes.

Programming Fundamentals - Lab [COSC-1201] 18


2.9.6. Program No 6

Write a program to find number of bytes occupied by various data types using the sizeof
operator.
int a;
char b;
float c;
long int d;
bool e;
unsigned int j;
unsigned long k;

Programming Fundamentals - Lab [COSC-1201] 19


2.9.7. Program No 7

Programming Fundamentals - Lab [COSC-1201] 20


Write equivalent expressions of following compound assignment operators
Operator Equivalent expression
A+=10
A-=10
A*=10
A/=10
A%=10

Programming Fundamentals - Lab [COSC-1201] 21

You might also like