Header Ads

Top 13 General Problem and Solved of Data Structure Lab

 SOLVED SOME OF THE PROBLEM :



Problem 01:  Initialize an array of 10 elements and print the array elements both in normal and reverse order.

For example,

Input: 12 32 43 1 54 53 15 64 3 13

Output: 13 3 64 15 53 54 1 43 32 12


Solved of this problem:

#include<iostream>

using namespace std;

#include<conio.h>

int main()

{

 

              int arr[50], size, i, j, n;

              cout<<"Enter your array size : "<<" ";

              cin>>size;

              cout<<"Enter array elements : "<<" ";

              for(i=0; i<size; i++)

              {

                             cin>>arr[i];

              }

              j=i-1;

              i=0;

              while(i<j)

              {

                             n=arr[i];

                             arr[i]=arr[j];

                             arr[j]=n;

                             i++;

                             j--;

              }

              cout<<"Your reverse  array is :"<<" ";

              for(i=0; i<size; i++)

              {

                             cout<<arr[i]<<" ";

              }

              getch();

}


Console output of this problem:



 



 

Problem 02:   Initialize an integer array of 10 elements and print how many numbers are odd and how many numbers are even.

For example,

Input: 12 32 43 1 54 53 15 64 3 13

Output:

6 odd numbers

4 even numbers

Solved of this problem :

#include <iostream>

#include <conio.h>

using namespace std;

 

int main()

{

    int arr[100];

    int i,size,odd=0,even=0;

    cout<<"Enter your arry size:"<<" ";

    cin>>size;

    cout<<"\nEnter elements of the array:"<<" ";

  for(i=0; i<size; i++)

    {

        cout<<"Enter the element arr["<<i<<"] :";

        cin>>arr[i];

 

    }

    for(i=0; i<size; i++)

    {

        if(arr[i]%2==0)

        {

            even++;

        }

        else{

            odd++;

        }

 

    }

 

    cout<<"\nTotal even numbers of an array :"<<even<<"\n";

    cout<<"Total odd numbers of an array : "<<odd;

     getch();

    return 0;

}








Problem 03: Write a function that takes TWO parameters to print all the odd numbers between a given range. Input the starting value of the range and ending value of the range. Then, send them as the parameters to your function.

For example,

Output:

Starting value: 12

Ending value: 23

13 15 17 19 21 23

Solved of this problem:

#include<stdio.h>

#include <iostream>

using namespace std;

int main(){

 

    int number;

    int min,max;

    cout << "Enter the minimum range: ";

    cin >> min;

 

   cout << "Enter the maximum range: ";

    cin >> max;

 

    cout << "Odd numbers in given range are: ";

    for(number = min;number <= max; number++)

 

         if(number % 2 !=0)

             cout << number<< " ";

 

    printf("\nEven numbers in given range are: ");

    for(number = min;number <= max; number++)

 

         if(number % 2 ==0)

             cout << number << " ";

 

    return 0;

}


Console output of this problem:


 

Problem 04:   Write a program to perform matrix addition between 3 matrices.

For example,

Input:

12 13 14     1 2 3     101 104 107

15 16 17     4 5 6     102 105 108

18 19 20     7 8 9     103 106 109

Output:

114 119 124

121 126 131

128 133 138

Solved of this problem:

 #include <iostream>

using namespace std;

 

int main()

{

  int m, n, c, d, first[10][10], second[10][10],third[10][10],sum[10][10];

 

  cout << "Enter number of rows and columns of matrix:"<<" ";

  cin >> m >> n;

  cout << "Enter elements of first matrix:"<<" ";

 

  for (c = 0; c < m; c++)

    for (d = 0; d < n; d++)

      cin >> first[c][d];

 

  cout << "Enter the elements of second matrix:"<<" ";

 

  for (c = 0; c < m; c++)

    for (d = 0; d < n; d++)

        cin >> second[c][d];

        cout<<"Enter the elements of third matrix:"<<" ";

        for (c = 0; c < m; c++)

        for (d = 0; d < n; d++)

        cin >> third[c][d];

 

  for (c = 0; c < m; c++)

    for (d = 0; d < n; d++)

      sum[c][d] = first[c][d] + second[c][d]+third[c][d];

 

  cout << "Sum of the matrices:"<<" ";

 

  for (c = 0; c < m; c++)

  {

    for (d = 0; d < n; d++)

      cout << sum[c][d] << "\t";

 

    cout << endl;

  }

 

  return 0;

}

 

Console Output of this problem:





 



 


Problem 05:    Write a function to calculate factorial of a given integer number if that number is a prime number. If it is not, it will give an error.

For example,

Scenario 1

Input: 5

Output: 120

Scenario 2

Input: 4

Output: Error! Not a prime number.

Solved of this problem:

#include <iostream>

using namespace std;

int functionOfPrimeNumber(int a)

{  int check =0;

    for(int i=2; i<3 ; i++)

    {

         if(a<2)

    {

        check =0;


    }

    else if(a%i!=0)

    {

    check= 1;

    }


    else

    {

    check=0;

     break;

    }

}

return check;

}

int functionOfFactorial(int a)

{

int Factorial=1;

 for(int counter=a; counter>=2; counter--)

            {

                Factorial=Factorial*counter;

            }

return Factorial;

      }

int main()

{

    while(1){

   int n;

    cout<<"Enter a integer number:"<<" ";

    cin>>n;

    if(functionOfPrimeNumber(n)==1)

    {   cout<<n<< " is a Prime Number"<<endl;

        cout<< "Factorial of "<<n<<" is "<<functionOfFactorial(n)<<endl;}

    else

    cout<<"Error!"<<""<<n<<""<<" is not a prime number"<<endl;

    }

}


 

Console Output of This Problem :

 


 



Problem 06:    Initialize TWO integer arrays of different sizes. Merge the input arrays and create a new array. Then print the new array in reverse order.

For example,

Array_1 = {10,20,30,40,50}

Array_2 = {1,2,3,4,5,6,7,8}

Output: 8 7 6 5 4 3 2 1 50 40 30 20 10

 

Solved of this problem :

#include <iostream>

using namespace std;

#include<conio.h>

int main()

{

    int firstArray[5],secondArray[8],margedArray[10];

    int i=0,j=0,k=0;

    int InputOne,InputTwo,n;

    cout<<"Enter your first arry size: " <<" ";

    cin>>InputOne;

    cout<<"Enter your first arry elements:"<<" ";

    for(i=0;i<InputOne;i++)

    {

        cin>>firstArray[i];

    }

    cout<<"Enter your second arry size: " <<" ";

    cin>>InputTwo;

    cout<<"Enter your second arry elements:"<<" ";

    for(i=0;i<InputTwo;i++)

    {

        cin>>secondArray[i];

    }

   i=0;

    while(i<InputOne && j<InputTwo)

    {

        if(firstArray[i]<secondArray[j])

        {

            margedArray[k++]=firstArray[i++];

        }

        else

            {

            margedArray[k++]=secondArray[j++];

            }

    }

    while(i<InputOne)

    {

         margedArray[k++]=firstArray[i++];

    }

 

    while(j<InputTwo)

    {

         margedArray[k++]=secondArray[j++];

    }

    cout<<"Your marged array is:"<<" ";

    for(k=0;k<InputOne+InputTwo;k++)

    {

        cout<<margedArray[k]<<" ";

    }

     cout<<endl;

 

            cout<<"Your reversed array is:"<<" ";

            for(int k=12;k>=0;k--)

               {

               cout<<margedArray[k]<<" ";

               }

             getch();

           return 0;

    }

 Console Output of this code:





Problem 07:  Initialize TWO integer arrays A and B of different sizes. Make a new array with the common elements between A and B. Print the new array element(s). If there is no common element, output “No common element!”.

For example,

Scenario 1:

Array_1 = {1,4,6,3,6,9}

Array_2 = {5,3,7,1,2,6}

Output: 1 6 3

Scenario 2:

Array_1 = {1,4,6,3,6,9}

Array_2 = {5,8,7,12,21,63}

Output: No common element!

 

Solved of this problem :

#include<iostream>

#include<conio.h>

using namespace std;

 int main()

{

  int n1,n2,i,j;

  cout<<"Enter your first array size: "<<" ";

  cin>>n1;

 int array1[n1];

 /* Enter distinct elements */

  cout<<"Enter the elements of the first array: "<<" ";

  for(i=0;i<n1;i++)

  {

    cin>>array1[i];

  }

 cout<<"\nEnter your second array size: "<<" ";

  cin>>n2;

  int array2[n2];

  cout<<"Enter the elements of the second array: "<<" ";

  for(i=0;i<n2;i++)

  {

    cin>>array2[i];

  }

  /* printing elements that are common in both the arrays */

  cout<<"\nYour common elements of the two arrays: "<<" ";

  for(i=0;i<n1;i++)

  {

    for(j=0;j<n2;j++)

    {

      if(array1[i]==array2[j])

      {

        cout<<array1[i]<<" ";

        }

    }   

  }

  getch();

  return 0;

}


 Console output of this problem:



 



Problem 08:  Initialize an array. Size should be more than FIVE. Write you program to change the array in such a way so that there cannot be any duplicate element in the array anymore. Print the changed array. If the initialized array already had no duplicate elements from the beginning, output a message saying “Array already unique!”;

For example,

Scenario 1:

Array_1 = {1,4,6,3,6,9,1}

Output: 1 4 6 3 9

Scenario 2:

Array_1 = {1,4,5,3,6,9}

Output: Array already unique!

 

Solved of this problem:

#include<iostream>

#include<conio.h>

 using namespace std;

 int main()

{

    int i,j,k,n,a[30];

    cout<<"Enter your array size:"<<" ";

    cin>>n;

    cout<<"\nEnter elements of array:"<<" ";

    for(i=0;i<n;++i)

        {

            cin>>a[i];

        }

 for(i=0;i<n;++i)

    {

        for(j=i+1;j<n;)

        {

            if(a[i]==a[j])

            {

                for(k=j;k<n-1;++k)

                    a[k]=a[k+1];

                     --n;

            }

            else

                ++j;

        }

    }

 

 

    cout<<"Your array elements without any duplicates elements:"<<" ";

    for(i=0;i<n;++i)

    {

        cout<<a[i]<<" ";

    }

   cout<<endl;

    for(int i=0; i<n; i++)

    {

        if(a[i]==n)

        {

            cout<<"Array already unique."<<endl;

            break;

        }

    }

   getch();

 

    return 0;

}






Problem 09:    Initialize an integer array A of size 10. Take an integer as input and print how many times that integer occurs in A.

For example,

Array_1 = {8,4,6,1,6,9,6,1,9,8}

Output:

Input a number to search: 6

The number occurs 3 times in the array

 

Solved of this problem:

#include <iostream>

using namespace std;

int main()

{

    int array[10],dup[10];

    int size,i,j,InputNumber;

    cout<<"Enter your array size:"<<" ";

    cin>>size;

    cout<<"Enter your array elements:"<<" ";

   for(i=0; i<size; i++)

    {

        cin>>array[i];

        dup[i]=-1;

    }

    for(i=0; i<size; i++)

    {

        int cnt=1;

        for(j=i+1; j<size; j++)

        {

            if(array[i]==array[j])

            {

                cnt++;

                dup[j]=0;

            }

        }

                if(dup[i]!=0)

        {

            dup[i]=cnt;

        }

    }

    cout<<"Enter a integer number from array :"<<" "<<endl;

    cin>>InputNumber;

    for(i=0; i<=1; i++)

    {

        if(InputNumber==array[i])

        {

         cout<<"your number"<<" "<<array[i]<<" "<<" occured"<<" "<<dup[i]<<" "<<"times"<<endl;

        }

    }

return 0;

    }


Your whole Screenshot here: (Console Output):

 

 



Problem 10: Initialize an integer array of size 10. Print the number of times each element occurs in the array.

For example,

Array_1 = {8,4,6,1,6,9,6,1,9,8}

Output:

8 occurs = 2 times

4 occurs = 1 time

6 occurs = 3 times

1 occurs = 2 times

9 occurs = 2 times

 

Solved of this problem is:

#include <iostream>

 using namespace std;

 int main()

{

    int array[25],dup[25];

    int size,i,j,cnt;

    cout<<"Enter your array size:"<<" ";

    cin>>size;

    cout<<"Enter your array elements:"<<" ";

    for(i=0; i<size; i++)

    {

        cin>>array[i];

        dup[i]=-1;

    }

    for(i=0; i<size; i++)

    {

        cnt=1;

        for(j=i+1; j<size; j++)

        {

            if(array[i]==array[j])

            {

                cnt++;

                dup[j]=0;

            }

        }

        if(dup[i]!=0)

        {

            dup[i]=cnt;

        }

    }

    cout<<"Duplicates elements in array:"<<" "<<endl;

    for(i=0; i<size; i++)

    {

        if(dup[i]!=0)

        {

            cout<<"Your Number is"<<" "<<array[i]<<endl;

            cout<<"your number"<<" "<<array[i]<<" "<<" occured"<<" "<<dup[i]<<" "<<"times"<<endl;

        }

    }

    return 0;

}

 

 

 

 














Problem 11: Initialize a matrix of minimum 3x4 (row x column) size. Output its transpose matrix.

For example,

Matrix_1:

1 6 7 9

2 4 8 5

3 1 9 4


Output:

1 2 3

6 4 1

7 8 9

9 5 4



Solved:

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
int m, n;
int A[10][10];
int transpose[10][10];
cout << "Enter the your rows and culumns size: "<<" ";
cin >> m>>n;

cout << "Enter your  matrix elements:"<<endl;
for(int i = 0; i < m; i++)
{
 for(int j = 0; j < n; j++)
 cin >> A[i][j];
}
cout<<"...................."<<endl;
 for (int i = 0; i < m; ++i)
      for (int j = 0; j < n; ++j) {
         transpose[j][i] = A[i][j];
      }
   cout << "Your Transpose Matrix is:"<<endl;
   for (int i = 0; i < n; ++i)
      for (int j = 0; j < m; ++j) {
         cout << " " << transpose[i][j];
         if (j == m - 1)
            cout << endl << endl;
      }

cout << endl;
}
return 0;
}


Problem 12: Write a code that will create custom ciphers (encoded words) on strings. Follow this procedure:
    1. Write a function named encode that takes TWO parameters, a string s and an integer j.

    2. Skip j number of characters in the string and increase the ASCII value of the next character by 2.

    3. Perform step (ii) throughout the string.

Return the converted string from encoding function.

For example,

Sample String (s): I am a student

Sample Integer (j): 2

Converted String: I cm c student



Solved:
#include<iostream>
using namespace std;

string encode(string s, int k)
{
    int n = s.length();
    for(int i=k; i<n; i=i+k+1)
        s[i]= s[i]+2;
    return s;
}
string ConvertedIntoDecode(string s, int k )
{
    int n = s.length();
    for(int i=k; i<n; i=i+k+1)
        s[i]= s[i];
    return s;
}
int main()
{
    while(1)
    {
        string str;
        cout<<"Sample String (s): ";
        getline (cin, str);
        int j;
        cout<<"Sample Integer (j):";
        cin >> j;
        string convertedString = encode(str,j);
        cout<<"YOur encode is:"<<" "<<convertedString<<"\n\n";
        string ConvertedString=ConvertedIntoDecode(str, j);
        cout<<" YOur decode of encode is:"<<" "<<ConvertedString<<"\n\n";
        cin.get();
    }
    return 0;
}


Problem 13: Write a program with an appropriate data structure to keep records of 10 students. Each student will have the following information:
Unique ID (you can use an integer for this)
Number of Credits Completed
CGPA
1.Print all the student’s IDs whose CGPA is more than 3.75.
2.Print all the student’s ID who has completed more than 50 credits

Solved:

#include<iostream>
using namespace std;
struct student{
    int id;
    int credit;
    float cgpa;
};
int main(){
    student s[10];
    for(int i=0; i < 10;i++){
        cout<< "Student["<<i+1<<"]: id, credit, cgpa\n";
        cin>>s[i].id;
        cin>>s[i].credit;
        cin>>s[i].cgpa;
    }
    for(int i=0; i < 10;i++){
        cout<< "Student["<<i+1<<"]"<<endl;
        cout<<"ID: "<<s[i].id<<endl;
        cout<<"Credit Completed:"<<s[i].credit<<endl;
        cout<<"CGPA:"<<s[i].cgpa<<endl;
    }
    int i=0;
    while (i<10)
    {
        if(s[i].cgpa>3.75 && s[i].credit>50)
        cout<<" credit is:"<<" "<<s[i].credit<<endl;
        cout<<"and"<<" "<<"cgpa is:"<<" "<<s[i].cgpa<<endl;
        i++;
    }
}

No comments

Powered by Blogger.