You are on page 1of 10

Pak-Austria Fachhochschule Institute of Applied

Sciences and Technology, Haripur, Pakistan

DEPARTMENT OF ELECTRICAL ENGINEERING


Data structure and Algorithm Lab Manual
LAB 2

Submitted by:
Name: Muhammad Suleman
Reg# B20F0404EE026

Submitted to:
Name: Rafi Ullah
Designation: LAB Engineer
Date:
October 20, 2021
LAB NO 2
STRUCTURES AND POINTERS
OBJECTIVE:
• To Revise the concepts of structures & pointers using C++.
• To Implement structures and pointers in C++.
APPARATUS:
The apparatus we used in this lab includes
Operating system (Windows)
DEVC++
Many compilers like IDE are present but we used DEVC++ it is efficient and easy to use.

INTRODUCTION:
Structures and pointers are very essential in C++. In structures multiple datatypes fall under the
same section called the structure of that section. Many attributes are given to an object which
is structure and in the main function it can be easily accessed. We use a keyword struct in this
regard for using structure.
Pointers are used to store and manage the addresses of dynamically allocated blocks of
memory. Such blocks are used to store data objects or arrays of objects. Pointers save the
memory. In this lab we will know how to store the variable value in a pointer and how an
location of that value of variable can be accessed by using address &.
Pointers reduce the length and complexity of a program.
Pointers allow passing of arrays and strings to functions more efficiently.
Pointers are used to store the addresses of other variables or memory items.
LAB TASK 1:
Write a program to swap two values by passing pointers as argument to the function. The
output of the program should be like:
CODE:
#include<iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp;
temp=*a;
*a=*b;
*b=temp;
cout<<"After swapping ist number value is"<<*a<<endl;
cout<<"After swapping 2nd number value is"<<*b;
}

int main()
{
int a,b;
cout<<"ENTER IST NUMBER"<<endl;
cin>>a;
cout<<"ENTER 2ND NUMBER"<<endl;
cin>>b;
cout<<"Values after exchange"<<endl;
swap(&a,&b);
return 0;
}
OUTPUT:

LAB TASK 2:
Declare a structure to represent a complex number (a number having a real part and imaginary 
part). Write C++ functions to add, subtract, multiply and divide two complex numbers.
(Comment each line with information about it).
CODE:
#include<iostream>
using namespace std;
struct complex{
int real,img;
};
void add(complex c1,complex c2); //Function declaration of addition
void subtract(complex c1,complex c2);//Function declaration of subtraction
void multiplication(complex c1,complex c2);//Function declaration of multiplication
void division(complex c1,complex c2);//Function declaration of division
int main(){
complex c1; //object c1
cout<<"Enter real part of ist complex number"<<endl;//Asking user to input
cin>>c1.real;//Inputting real no
cout<<"Ente img part of ist complex number"<<endl;//Asking user to input
cin>>c1.img;//Inputting imaginary number
cout<<c1.real<<"+"<<" i"<<c1.img;
complex c2;//object C2
cout<<endl<<"Enter real part of 2nd complex number"<<endl;//Asking user to input
cin>>c2.real;
cout<<endl<<"Ente img part of 2nd complex number"<<endl;//Asking user to input
cin>>c2.img;
cout<<endl<<c2.real<<"+"<<endl<<"i"<<c2.img;
add(c1,c2); //Calling function addition to perform addtion
subtract(c1,c2);//Calling function addition to perform subtraction
multiplication(c1,c2);//Calling function addition to perform multiplication
division(c1,c2);//Calling function addition to perform division
}
void add(complex c1,complex c2){ //function definiton of addition
int a,b;//declaring variables
a=c1.real+c2.real;//Assinging sum into variable of real number
cout<<endl<<"Sum of real numbers of complex number"<<endl<<a<<endl;;//showing
output of addition of real number
b=c1.img+c2.img;//Assinging sum into variable of imaginary number
cout<<endl<<"Sum of imaginary numbers "<<endl<<b<<"\n";;//showing output of
addition of imaginary number
}
void subtract(complex c1,complex c2){ //function definiton of subtraction
int a,b;
a=c1.real-c2.real;//Assinging subtraction into variable of real number
cout<<endl<<"subtract of real numbers of complex
number"<<endl<<a<<endl;//showing output of subtraction of real number
b=c1.img-c2.img;//Assinging subtraction into variable of imaginary number
cout<<endl<<"subtract of imaginary numbers "<<endl<<b<<endl;;//showing output of
subtraction of imaginary number
cout<<endl;
}
void multiplication(complex c1,complex c2){ //function definiton of multiplication
int a,b;
a=(c1.real*c2.real);//Assinging multiplication into variable of real number
cout<<endl<<"multiplication of real numbers of complex
number"<<endl<<a<<endl;;//showing output of multiplication of real number
b=(c1.img*c2.img);//Assinging multiplication into variable of imaginary number
cout<<endl<<"multiplication of imaginary numbers "<<endl<<b<<endl;;//showing
output of multiplication of imaginary number
cout<<endl;
}
void division(complex c1,complex c2){ //function definiton of division
int a,b;
a=(c1.real/c2.real);//Assinging division into variable of real number
cout<<endl<<"division of real numbers of complex number"<<endl<<a<<"";;//showing
output of division of real number
b=(c1.img/c2.img);//Assinging division into variable of imaginary number
cout<<endl<<"division of imaginary numbers "<<endl<<b<<endl;;//showing output of
subtraction of imaginary number
}
OUTPUT:

LAB TASK 3:
Construct a structure to compute time difference of two time periods. The time periods should be
entered by the user. (Comment each line with information about it)
Hint* use functions and pointers to struct the structure.

CODE:
#include<iostream>
using namespace std;
struct time{

int hour,minute,sec;
};\
void subtract(time ist, time second);
int main(){time ist;
cout<<"Enter hour of ist time period"<<endl;
cin>>ist.hour;
cout<<"Enter minutes of ist time period"<<endl;
cin>>ist.minute;
cout<<"Enter sec of ist time period"<<endl;
cin>>ist.sec;
time second;
cout<<"Enter hour of 2nd time period"<<endl;
cin>>second.hour;
cout<<"Enter minutes of 2nd time period"<<endl;
cin>>second.minute;
cout<<"Enter sec of 2nd time period"<<endl;
cin>>second.sec;
cout<<"ist Time period "<<"\t"<<second.hour<<":"<<second.minute<<":"<<second.sec;
cout<<endl;
subtract( ist, second);
}
void subtract(time ist, time second){
int a,b,c;
cout<<endl<<"Difference between time period"<<endl<<endl;
a=ist.hour-second.hour;
b=ist.minute-second.minute;
c=ist.sec-second.sec;
cout<<"Time periods will be "<<endl<<a<<":"<<b<<":"<<c;
}

OUTPUT:

ANALYSIS AND RESULTS / CONCUSION:


All the tasks have been performed and results are prepared.

FIRST TASK:
In the first task swap function has been made and its arguments has been assigned pointer
variables. Then a temporary variable has been taken swap condition has been applied. Then in
main function has input the numbers and swap function is called by address to point the
location of variables.

SECOND TASK:
In a second task we use structures and create two objects c1 and c2. User has input the real and
imaginary numbers of two complex numbers. Then functions of addition, subtraction,
multiplication and division has been declared and defined. Then in main function they have
been called and output showed on the screen. Problem I persisted is showing output in form of
complex number on the screen then came to know about solution of that problem. Then it
become easy to perform the task and successfully output displayed on the screen.

THIRD TASK:
In third task time duration has been calculated between two-time intervals. User has input the
first-time duration and then second time duration. Then by function time duration between
intervals has been calculated. Problem I persisted is about understanding time duration means
how to enter intervals. By countering this it become easy to perform the task.

You might also like