DATA STRUCTURE LAB
18B17CI371
LAB RECORD
Submitted By
Rahul Singh
ER NO:-191B317
Submitted to: KB Meena Sir
2020-2021
Department of Computer Science & Engineering
Jaypee University of Engineering and Technology, A-B Road,
Raghogarh, Di stt. - Guna (M.P.), PIN - 473226, INDIA
LAB 1: REVISIT
1. WAP to find out largest element of an array
Sol:- ///////////////////this program is developed by 191B317//////////////////////////
#include<bits/stdc++.h>
using namespace std;
int max_element(int a[],int n)
{
int max=a[0];
for(int i=1;i<n;i++)
{
if(a[i]>max)
{
max=a[i];
}
}
return max;
}
int main()
{
int m;
cout<<"enter the size of array:";
cin>>m;
int arr[m];
cout<<"enter the array elements:";
int i=0;
for(;i<m;i++)
{
cin>>arr[i];
}
cout<<"maximum element in the array is:"<<max_element(arr,m);
///// Program developed by Rahul Singh (191B317)
QUE2: WAP to search an element in array.
///////////////////program developed by Rahul Singh(191B317)
#include<bits/stdc++.h>
using namespace std;
void search_element(int a[],int n ,int l)
{
int i;
for( i=0;i<l;i++)
{
if(a[i]==n)
{
cout<<"element found at:"<<i+1<<endl;
break;
}
}
if (i==l) cout<<"not found"<<endl;
}
int main()
{
int m,x;
cout<<"enter the size of array:";
cin>>m;
int arr[m];
cout<<"enter the array elements:";
int i=0;
for(;i<m;i++)
{
cin>>arr[i];
}
cout<<"enter the elements to be searched:";
cin>>x;
search_element(arr,x,m);
}
Que 3: WAP to check whether the number is prime or not.
///This Program is developed by Rahul Singh (191B317)
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int n)
{
if (n <= 1)
return 0;
for (int i = 2; i < n; i++)
if (n % i == 0)
return 0;
return 1;
}
int main()
{
int no;
cin>>no;
isPrime(no) ? cout<<"it is prime"<<endl : cout <<"it is not prime"<<endl;
}
QUE4: WAP to calculate x^y where x and y are two integer numbers entered
by the user. [do not use pow() function]
///program developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
int power(int m, int n)
{
int ans=1;
while (n != 0)
{
ans *= m;
--n;
}
return ans;
}
int main()
{
int x,y;
cout << "enter x and y";
cin >>x>>y;
cout<<"resut is="<<power(x,y)<<endl;
}
Que5: WAP to replace a character by another character in a string.
Take both the choice from the user.
///program developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
void alter_str(char m,char n,string str1)
{
for(int i=0;i<[Link]();i++)
{
if(str1[i]==m)
{
str1[i]=n;
}
}
cout<<"altered string is:"<<str1<<endl;
}
int main()
{
string str;
char a,b;
cout<<"enter the string:";
cin>>str;
cout<<"enter the character to be replaced:";
cin>>a;
cout<<"enter the character to be replaced with:";
cin>>b;
alter_str(a,b,str);
}
Que 6: WAP to find the reverse of given string.
///program developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
void rev_str(string str1)
{
for(int i=[Link]()-1;i>=0;i--)
{
cout<<str1[i];
}
}
int main()
{
string str;
cout<<"enter the string:";
cin>>str;
rev_str(str);
return 0;
}
Que7: WAP to sort the array and ask the choice from user for
ascending/descending.
///program developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
#define rep(i,n) for (i = 0; i < n; i++)
#define REPR(i,k,n) for (i = k; i >= n; --i)
using namespace std;
void asc_desc(int array[],int l)
{
int j;
rep(j,l)
{
if (array[j] > array[j + 1])
{
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
j = -1;
}
}
int op;
cout<<"enter 1 for ascending and 0 for descending order resptively:";
cin>>op;
if(op)
{
rep(j,l) cout<<array[j]<<" ";
}
else
{
REPR(j,l-1,0)cout<<array[j]<<" ";
}
}
int main()
{
int m,i;
cin>>m;
int arr[m];
cout<<"enter the array elements:";
rep(i,m)cin>>arr[i];
asc_desc(arr,m);
}
QUE 8: WAP to find a word in given statement.
// This program is developed by Rahul Singh(191B317)
#include<iostream>
using namespace std;
void getZarr(string str, int Z[]);
void search(string text, string pattern)
{
string concat = pattern + "$" + text;
int l = [Link]();
int Z[l];
getZarr(concat, Z);
for (int i = 0; i < l; ++i)
{
if (Z[i] == [Link]())
cout << "Pattern found at index "
<< i - [Link]() -1 << endl;
}
}
void getZarr(string str, int Z[])
{
int n = [Link]();
int L, R, k;
L = R = 0;
for (int i = 1; i < n; ++i)
{
if (i > R)
{
L = R = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
else
{
k = i-L;
if (Z[k] < R-i+1)
Z[i] = Z[k];
else
{
L = i;
while (R<n && str[R-L] == str[R])
R++;
Z[i] = R-L;
R--;
}
}
}
}
int main()
{
string text;
string pattern ;
getline(cin,text);
getline(cin,pattern);
search(text, pattern);
return 0;
}
QUE9:WAP to concatenate two strings using pointer.
//// this program is developed by Rahul Singh(191B317)
#include <iostream>
#define MAX_SIZE 100
using namespace std;
void concatenate(char l1[MAX_SIZE], char l2[MAX_SIZE])
{
char * s1 = l1;
char * s2 = l2;
while(*(++s1));
while(*(s1++) = *(s2++));
cout<<"Concatenated string:"<<l1;
}
int main() {
char str1[MAX_SIZE], str2[MAX_SIZE];
cout<<"Enter 1st string: ";
cin>>str1;
cout<<"Enter 2nd string: ";
cin>>str2;
concatenate(str1,str2);
}
Que 10: WAP to create a dynamic array of user desired size and search an
element in that array
//// this program is developed by Rahul Singh(191B317)
#include<bits/stdc++.h>
using namespace std;
void find_no(vector<int>vect)
{
int n;
cout<<"enter the no you want to find";
cin>>n;
int c=0;
for(auto x:vect)
{
c++;
if(x==n)
{
cout<<"found at:"<<c;
break;
}
}
}
int main()
{
vector<int> v;
int n;
cout<<" how many nos you want to enter";
cin>>n;
cout<<"enter elements";
for(int i=0;i<n;i++)
{
int no;
cin>>no;
v.push_back(no);
}
find_no(v);
}
QUE11: WAP to calculate difference between two time periods using the C
structures. Sample output: Enter start time: Enter hours, minutes and
seconds respectively:= 8 :12 :15 Enter stop time: Enter hours, minutes and
seconds respectively: 12 34 55 TIME DIFFERENCE: [Link] - [Link] = [Link]
//// this program is developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
struct TIME
{
int seconds;
int minutes;
int hours;
};
void computeTimeDifference(struct TIME, struct TIME, struct TIME *);
int main()
{
struct TIME t1, t2, difference;
cout << "Enter start time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> [Link] >> [Link] >> [Link];
cout << "Enter stop time." << endl;
cout << "Enter hours, minutes and seconds respectively: ";
cin >> [Link] >> [Link] >> [Link];
computeTimeDifference(t1, t2, &difference);
cout << endl << "TIME DIFFERENCE: " << [Link] << ":" << [Link] << ":
" << [Link];
cout << " - " << [Link] << ":" << [Link] << ":" << [Link];
cout << " = " << [Link] << ":" << [Link] << ":" << d
[Link];
return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *differ
ence){
if([Link] > [Link])
{
--[Link];
[Link] += 60;
}
difference->seconds = [Link] - [Link];
if([Link] > [Link])
{
--[Link];
[Link] += 60;
}
difference->minutes = [Link];
difference->hours = [Link];
}
QUE-12: WAP to add two complex numbers by passing structure to
a function. Sample output: For 1st complex number Enter real and
imaginary part respectively: 2.3 4.5 For 2nd complex number Enter
real and imaginary part respectively: 3.4 5 Sum = 5.7 + 9.5i
//// this program is developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
typedef struct complexNumber {
float real;
float imag;
};
complexNumber addCN(complexNumber num1,complexNumber num2) {
complexNumber temp;
[Link] = [Link] + [Link];
[Link] = [Link] + [Link];
return(temp);
}
int main() {
complexNumber num1, num2, sum;
cout << "Enter real part of Complex Number 1: " << endl;
cin >> [Link];
cout << "Enter imaginary part of Complex Number 1: " << endl;
cin >> [Link];
cout << "Enter real part of Complex Number 2: " << endl;
cin >> [Link];
cout << "Enter imaginary part of Complex Number 2: " << endl;
cin >> [Link];
sum = addCN(num1, num2);
if([Link] >= 0)
cout << "Sum of the two complex numbers is "<< [Link] <<" + "<< [Link]
<<"i";
else
cout << "Sum of the two complex numbers is "<< [Link] <<" + ("<< [Link]
<<")i";
return 0;
}