DSA Lab 1 Tasks
DSA Lab 1 Tasks
DSA Lab 1 Tasks
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;
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.
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.
double 8bytes
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
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
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()
{
double x = 1.2;
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;
}
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.
1 if statement
#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;
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;
}
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:
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