Sie sind auf Seite 1von 13

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 2: REVISIT
1. WAP to generate Fibonacci sequence upto N terms.
Sol:-
/////This program is developed by Rahul Singh(191B317)

#include <bits/stdc++.h> 
using namespace std;   
void Fibonacci(int n)  
{  
    int f1 = 0, f2 = 1, i;  
  
    if (n < 1)  
        return;  
  
    for (i = 1; i <= n; i++)  
    {  
        cout<<f2<<" ";  
        int m = f1 + f2;  
        f1 = f2;  
        f2 = m;  
    }  
}   
int main()  
{  
    int no;
    cin>>no;
    Fibonacci(no);  

2. WAP to find out series sum of 1^2 + 2^2 + …. + n^2
/////This program is developed by Rahul Singh(191B317)
#include <bits/stdc++.h> 
using namespace std;   
void sumseries(int n)  
{  
  cout<<(n*(n+1)*((2*n)+1))/6;
}   
int main()  
{  
    int no;
    cin>>no;
    sumseries(no);  

Que 3: WAP to find GCD of two numbers.


/////This program is developed by Rahul Singh(191B317)
#include <iostream> 
using namespace std; 
int gcd(int a, int b) 

    if (b == 0) 
        return a;              //using modulo operator in eucledian algorithm
    return gcd(b, a % b);  
      

int main() 

    int a ,b;
    cin>>a>>b;
    cout<<"GCD of "<<a<<" and "<<b<<" is "<<gcd(a, b);  
}

//taken reference from internet in search of better algorithm
QUE4: WAP to multiply two numbers by using addition.
//////This program is developed by Rahul Singh (191B317)
#include<bits/stdc++.h> 
using namespace std; 
int multiply(int x, int y) 

    if(y == 0) 
    return 0; 
    if(y > 0 ) 
    return (x + multiply(x, y-1)); 
    if(y < 0 ) 
    return -multiply(x, -y); 

int main() 

    int m,n;
    cin>>m>>n;
    cout <<multiply(m,n); 
}

Que5: WAP to convert a decimal into binary number.


//////This program is developed by Rahul Singh (191B317)
#include<bits/stdc++.h> 
using namespace std; 
void decimalToBinary(int x)
{
    vector<int>v;
    while(x)
    {
        int no;
        no=x%2;
        x=x/2;
        v.push_back(no);
    }
    for(int x:v)
    {
        cout<<x;
    }
}
int main()
{
    int n;
    cin>>n;
    decimalToBinary(n);}

Que 6: WAP to convert a binary number into decimal.


//////This program is developed by Rahul Singh (191B317)
#include<bits/stdc++.h> 
using namespace std; 
void BinaryTodecimal(int x)
{
    int ans=0,temp,power =1;
    temp=x;
    while(temp)
    {
        int m=temp%10;
        temp/=10;
        ans+=m*power;
        power*=2;
    }
    cout<<ans<<endl;
}
int main()
{
    int n;
    cin>>n;
    BinaryTodecimal(n);
}

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 = concat.length(); 

    int Z[l]; 
    getZarr(concat, Z); 

    
    for (int i = 0; i < l; ++i) 
    { 
        
        if (Z[i] == pattern.length()) 
            cout << "Pattern found at index "
                << i - pattern.length() -1 << endl; 
    } 

void getZarr(string str, int Z[]) 

    int n = str.length(); 
    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: 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;
};

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 >> t1.hours >> t1.minutes >> t1.seconds;

    cout << "Enter stop time." << endl;
    cout << "Enter hours, minutes and seconds respectively: ";
    cin >> t2.hours >> t2.minutes >> t2.seconds;

    computeTimeDifference(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){
    
    if(t2.seconds > t1.seconds)
    {
        --t1.minutes;
        t1.seconds += 60;
    }
    difference->seconds = t1.seconds - t2.seconds;
    if(t2.minutes > t1.minutes)
    {
        --t1.hours;
        t1.minutes += 60;
    }
    difference->minutes = t1.minutes-t2.minutes;
    difference->hours = t1.hours-t2.hours;
}

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;
   temp.real = num1.real + num2.real;
   temp.imag = num1.imag + num2.imag;
   return(temp);
}
int main() {
   complexNumber num1, num2, sum;
   cout << "Enter real part of Complex Number 1: " << endl;

   cin >> num1.real;
   cout << "Enter imaginary part of Complex Number 1: " << endl;

   cin >> num1.imag;
   cout << "Enter real part of Complex Number 2: " << endl;

   cin >> num2.real;
   cout << "Enter imaginary part of Complex Number 2: " << endl;

   cin >> num2.imag;
   sum = addCN(num1, num2);
   if(sum.imag >= 0)
   cout << "Sum of the two complex numbers is "<< sum.real <<" + "<< sum.imag 
<<"i";
   else
   cout << "Sum of the two complex numbers is "<< sum.real <<" + ("<< sum.imag 
<<")i";
   return 0;
}

Das könnte Ihnen auch gefallen