DSA Lab 1 Tasks

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Faculty of Computing and Information Technology (FCIT)

Department of Computing Indus University, Karachi

Lab no 1
Fundamentals of C++ Language
Objectives:
 What is Data Structure and Algorithm?
 Application of Data Structure and Algorithm.
 Implementation of Datatypes, variable and operator.
 Implementation of typecasting and decision making statement.

Introduction:
C++ is a middle-level programming language developed by Bjarne Stroustrup starting in 1979 at Bell
Labs. C++ runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.
This C++ tutorial adopts a simple and practical approach to describe the concepts of C++ for beginners to advanced
software engineers.
Variable:
A variable definition tells the compiler where and how much storage to create for the variable.
#include <iostream>
using namespace std;

int main () {
// Variable definition:
int a, b;
int c;
float f;

// actual initialization
a = 10;
b = 20;
c = a + b;

cout << c << endl ;

f = 70.0/3.0;
cout << f << endl ;

return 0;
}

Datatypes:
You need to use various variables to store various information. Variables are nothing but
reserved memory locations to store values. This means that when you create a variable you
reserve some space in memory.

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

You may like to store information of various data types like character, wide character, integer,
floating point, double floating point, Boolean etc. Based on the data type of a variable, the
operating system allocates memory and decides what can be stored in the reserved memory.
The following table shows the variable type, how much memory it takes to store the value in
memory, and what is maximum and minimum value which can be stored in such type of
variables.

Type Typical Bit Width Typical Range

char 1byte -127 to 127 or 0 to 255

unsigned char 1byte 0 to 255

signed char 1byte -127 to 127

int 4bytes -2147483648 to 2147483647

unsigned int 4bytes 0 to 4294967295

signed int 4bytes -2147483648 to 2147483647

short int 2bytes -32768 to 32767

unsigned short int 2bytes 0 to 65,535

signed short int 2bytes -32768 to 32767

long int 8bytes -2,147,483,648 to 2,147,483,647

signed long int 8bytes same as long int

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

unsigned long int 8bytes 0 to 4,294,967,295

long long int 8bytes -(2^63) to (2^63)-1

unsigned long long int 8bytes 0 to 18,446,744,073,709,551,615

float 4bytes -2147483648 to 2147483647

double 8bytes

long double 12bytes

Following is the example, which will produce correct size of various data types on your
computer.
#include <iostream>
using namespace std;

int main() {
cout << "Size of char : " << sizeof(char) << endl;
cout << "Size of int : " << sizeof(int) << endl;
cout << "Size of short int : " << sizeof(short int) << endl;
cout << "Size of long int : " << sizeof(long int) << endl;
cout << "Size of float : " << sizeof(float) << endl;
cout << "Size of double : " << sizeof(double) << endl;
cout << "Size of wchar_t : " << sizeof(wchar_t) << endl;

return 0;
}

This example uses endl, which inserts a new-line character after every line and << operator is being used to pass
multiple values out to the screen. We are also using sizeof() operator to get size of various data types.
Output:
Size of char : 1
Size of int : 4
Size of short int : 2
Size of long int : 4
Size of float : 4
Size of double : 8

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

Size of wchar_t : 4
Type Conversion in C++:
Implicit Type Conversion Also known as ‘automatic type conversion’.
 Done by the compiler on its own, without any external trigger from the user.
 Generally takes place when in an expression more than one data type is present. In such condition
type conversion (type promotion) takes place to avoid loss of data.
 All the data types of the variables are upgraded to the data type of the variable with largest data
type. bool -> char -> short int -> int ->
Unsigned int -> long -> unsigned ->
long -> float -> double -> long double

#include <iostream>
using namespace std;

int main()
{
int x = 10; // integer x
char y = 'a'; // character c

// y implicitly converted to int. ASCII


// value of 'a' is 97
x = x + y;

// x is implicitly converted to float


float z = x + 1.0;

cout << "x = " << x << endl


<< "y = " << y << endl
<< "z = " << z << endl;

return 0;
}
Explicit Type Conversion: This process is also called type casting and it is user-
defined. Here the user can typecast the result to make it of a particular data type.
In C++, it can be done by two ways:
 Converting by assignment: This is done by explicitly defining the required type
in front of the expression in parenthesis. This can be also considered as forceful
casting.
Syntax:
(type) expression
#include <iostream>
using namespace std;

int main()

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

{
double x = 1.2;

// Explicit conversion from double to int


int sum = (int)x + 1;

cout << "Sum = " << sum;

return 0;
}

Scope of variable:
A scope is a region of the program and broadly speaking there are three places, where variables
can be declared −
 Inside a function or a block which is called local variables,
 In the definition of function parameters which is called formal parameters.
 Outside of all functions which is called global variables
Local Variables:
Variables that are declared inside a function or block are local variables. They can be used only
by statements that are inside that function or block of code. Local variables are not known to
functions outside their own. Following is the example using local variables.
#include <iostream>
using namespace std;

int main () {
// Local variable declaration:
int a, b;
int c;

// actual initialization
a = 10;
b = 20;
c = a + b;

cout << c;

return 0;
}

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi
Global Variables
Global variables are defined outside of all the functions, usually on top of the program. The global
variables will hold their value throughout the life-time of your program.
#include <iostream>
using namespace std;

// Global variable declaration:


int g;

int main () {
// Local variable declaration:
int a, b;

// actual initialization
a = 10;
b = 20;
g = a + b;

cout << g;

return 0;
}
Operators:
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations. C++ is rich in built-in operators and provide the following types of operators −

 Arithmetic Operators
 Relational Operators
 Logical Operators
 Bitwise Operators
 Assignment Operators
Decision Making System:
Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by
the program, along with a statement or statements to be executed if the condition is determined to be true, and
optionally, other statements to be executed if the condition is determined to be false.

Sr.No Statement & Description

1 if statement

An ‘if’ statement consists of a Boolean expression followed by one or more


statements.

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

#include <iostream>
using namespace std;

int main() {
if (20 > 18) {
cout << "20 is greater than 18";
}
return 0;
}
2 if...else statement
An ‘if’ statement can be followed by an optional ‘else’ statement, which
executes when the boolean expression is false.
#include <iostream>
using namespace std;

int main()
{ int time =
20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
}
3 If-Else-if
#include <iostream>
using namespace std;

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

int main()
{ int time =
22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20)
{ cout << "Good
day.";
} else {
cout << "Good evening.";
}
return 0;
}
5 switch statements
int day= 4;
switch (day){
case 1:
cout<< "Monday";
break;
case 2:
cout<< "Tuesday";
break;
case 3:
cout<< "Wednesday";
break;
case 4:
cout<< "Thursday";
break;
case 5:
cout<< "Friday";
break;
case 6:
cout<< "Saturday";
break;
case 7:
cout<< "Sunday";
break;
}

Data Structure and Algorithms


Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

Lab Tasks:
Q1) Evaluate the following C++ expression assuming i=5, j=10, k=15 and tell the value
(true/false).i == k / j

Code:
#include<iostream>
using namespace std;
int main(){
int i = 5, j = 10, k = 15;

if (i == k / j)
cout << "True";
else
cout << "False";

return 0;
}

Output:

Q2) Evaluate the following C++ expression assuming i=5 and k= 15


And tell the value (true/false). k % i < k / i

Code:
#include<iostream>
using namespace std;
int main(){
int i=5, k=15;
if (k % i < k / i)
cout<< "true";
else
cout<< "true";
return 0;
}

Output:

Q3) write a nested if statement to print the appropriate activity depending on the value of a
variable temperature and humidity as in the table below: Assume that the temperature can
only be warm and cold and the humidity can only be dry and humid.
Data Structure and Algorithms
Faculty of Computing and Information Technology (FCIT)
Department of Computing Indus University, Karachi

if temperature is if humidity is print this activity


warm dry "play tennis"
warm humid "swim"
cold dry "play basketball"
cold humid "watch TV"
Code:
#include <iostream>
using namespace std;
int main(){
string temp, humidity;
cout <<"Enter Temperature(warm or cold): "<<endl;
cin >>temp;
cout<<"Enter Humidity (dry or humid): "<<endl;
cin>>humidity;
while (true){

if (temp=="warm" && humidity=="dry"){


cout<<"Play Tennis"<<endl;
break;
}
if(temp=="warm" && humidity == "humid"){
cout<<"Go for Swim"<<endl;
break;
}
if(temp=="cold" && humidity == "dry"){
cout<<"Play Basketball"<<endl;
break;
}
if(temp=="cold" && humidity == "humid"){
cout<<"Watch TV"<<endl;
break;
}
else
cout<<"Invalid Option"<<endl;
break;
}
return 0;
}
Output:

Data Structure and Algorithms

You might also like