Data Structure Lab: ER NO:-191B317
Data Structure Lab: ER NO:-191B317
Data Structure Lab: ER NO:-191B317
18B17CI371
LAB RECORD
Submitted By
Rahul Singh
ER NO:-191B317
2020-2021
}
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);
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<str1.length();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);
}
}
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);
int Z[l];
getZarr(concat, Z);
if (Z[i] == pattern.length())
cout << "Pattern found at index "
<< i - pattern.length() -1 << endl;
}
}
L = R = 0;
for (int i = 1; i < n; ++i)
{
if (i > R)
{
L = R = i;
k = i-L;
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() {
}
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: 12:34:55 - 8:12:15 = 4:22:40
//// this program is developed by Rahul Singh(191B317)
#include <bits/stdc++.h>
using namespace std;
struct TIME
{
int seconds;
int minutes;
int hours;
};
int main()
{
struct TIME t1, t2, difference;
cout << endl << "TIME DIFFERENCE: " << t1.hours << ":" << t1.minutes << ":
" << t1.seconds;
cout << " - " << t2.hours << ":" << t2.minutes << ":" << t2.seconds;
cout << " = " << difference.hours << ":" << difference.minutes << ":" << d
ifference.seconds;
return 0;
}
void computeTimeDifference(struct TIME t1, struct TIME t2, struct TIME *differ
ence){