Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

Answer to question no.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,flag=0;
    cout << "Enter a number:";
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    cout << "The array: ";
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;

    cout << "Duplicate numbers:";
    for (int i = 0; i < n; i++)
    {
        for (int j = i + 1; j < n; j++)
        {
            if (a[i] == a[j])
            {
                flag=1;
                cout << a[i] << " ";
            }
        }
    }
    if(flag==0){
        cout<<"No duplicate numbers";
    }
}

Answer to question no.2

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, mm = 1, ctr = 0;
    int auxArr[100];
    cout << "Enter a number:";
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        auxArr[i] = 0;
    }
    cout << "The array:";
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (a[i] == a[j])
            {
                
                auxArr[i] = mm;
                mm++;
            }
        }
        mm = 1;
    }
    
        cout << "After removing the duplicates the final array:";
        for (int i = 0; i < n; i++)
        {
            if (auxArr[i] == 1)
            {
                cout << a[i] << " ";
            }
        }
    
}

Answer to question no.3


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main(){
    int n,flag=0;
    cout << "Enter a number:";
    cin>>n;
    int a1[n];
    int a2[n];
     cout << "Enter first array:"<<endl;
    for(int i=0;i<n;i++){
        cin>>a1[i];
    }
    cout << "Enter second array:"<<endl;
    for(int i=0;i<n;i++){
        cin>>a2[i];
    }
 cout << "The first array:";
    for(int i=0;i<n;i++){
         cout << a1[i] << " ";
    }
     cout<<endl;
    cout << "The second array:";
    for(int i=0;i<n;i++){
         cout << a2[i] << " ";
    }

     cout<<endl;
      cout<<"The intersection:";
      for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(a1[i]==a2[j]){
                flag=1;
                cout << a1[i] << " "; 
            }
        }
    }
    if(flag==0){
        cout<<"No common elements found";
    }

}
Answer to question no.4

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void swap(int *p, int *q)
{
    int temp = *p;
    *p = *q;
    *q = temp;
}
void sort(int a[], int size, int pos)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = i + 1; j < size; j++)
        {
            if (a[i] > a[j])
            {
                swap(&a[i], &a[j]);
            }
        }
    }
   cout<<"The kth smallest element: "<<a[pos - 1];
}
int main()
{
    int n, k;
    cout << "Enter a number:";
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    cout << "Enter the position "<<" ";
    cout<<endl;
   cin >> k;
    sort(a, n, k);
}

Answer to question no.5


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int a[100];
    int n;
    cout << "Enter a number:";
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    cout << "The array: ";
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    cout << "Reverse of the array:";
    for (int i = n - 1; i >= 0; i--)
    {
        cout << a[i] << " ";
    }
}

Answer to question no.6

#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
void SWAP(int *p, int *q)
{
    int temp = *p;
    *p = *q;
    *q = temp;
}
void BUBBLE_SORT(int a[], int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = size - i - 1; j < size; j++)
        {
            if (a[j] > a[j + 1])
            {
                SWAP(&a[j], &a[j + 1]);
            }
        }
    }
}
int main()
{
    int n;
    cout<<"Enter a number:";
    cin >> n;
    ofstream in1("in.txt");
    int max = 250;
    int min = -249;
    for (int i = 0; i < n; i++)
    {
        in1 << rand() % (max - min + 1) + min << " ";
    }
    in1.close();
    ifstream in2("in.txt");
    int a[n];
    for (int i = 0; i < n; i++)
    {
        in2 >> a[i];
    }
    BUBBLE_SORT(a, n);
    ofstream out("out.txt");
    for (int i = 0; i < n; i++)
    {
        out << a[i] << " ";
    }
    in1.close();
    in2.close();
    out.close();
    return 0;
}
Answer to question no.7

#include <iostream>
#include <bits/stdc++.h>
#include <fstream>
using namespace std;
void REPLACEMENT_SORT(char a[], int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = i + 1; j < size; j++)
        {
            if ((a[i] > a[j]))
            {
                swap(a[i], a[j]);
            }
        }
    }
}
int main()
{
    char str[1000];
    int i;
    ofstream in1("in.txt");

    for (i = 0; i < 1000; i++)
    {

        char c = (90 - (rand() % 26));

        str[i] = c;
    }
    for (i = 0; i < 1000; i++)
    {

        in1 << (str[i]) << " ";
    }
    in1.close();
    ifstream in2("in.txt");
    for (int i = 0; i < 1000; i++)
    {
        in2 >> str[i];
    }
    REPLACEMENT_SORT(str, 1000);
    for (int i = 0; i < 1000; i++)
    {
        cout<< str[i] << " ";
    }
    in1.close();
    in2.close();
   
}
Answer to question no.8

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cout << "Enter a number:";
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
        cin>>a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout<<endl;
    for (int i = 0; i < n; i++)
    {
        if (a[i] % 3 == 0)
        {
            a[i]=-1;
            cout<<a[i]<<" ";

        }
        else{
             cout <<a[i]<<" ";
        }
    }
}
Answer to question no.9

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void swap(int *p, int *q)
{
    int temp = *p;
    *p = *q;
    *q = temp;
}
void sort(int a[], int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = i + 1; j < size; j++)
        {
            if (a[i] < a[j])
            {
                swap(&a[i], &a[j]);
            }
        }
    }
}
int main()
{
    int n, takenSum = 0, notTakenSum = 0, ctr = 0, sum = 0;
    cout << "Enter a number:";
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }

    sort(a, n);

    int k = 0;
    for (int i = 0; i < n; i++)
    {
        sum = sum + a[i];
    }

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

        int k = a[i];
        ctr++;
        takenSum = takenSum + k;
        sum = sum - k;
        notTakenSum = sum;
        if (notTakenSum < takenSum)
        {
            break;
        }
    }
    cout << ctr;
}

Answer to question no.10

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void swap(int *p, int *q)
{
    int temp = *p;
    *p = *q;
    *q = temp;
}
void sort(int a[], int size)
{
    for (int i = 0; i < size - 1; i++)
    {
        for (int j = i + 1; j < size; j++)
        {
            if (a[i] > a[j])
            {
                swap(&a[i], &a[j]);
            }
        }
    }
}
void diff(int a[], int n)
{
    int diff = INT_MAX;
    int m, k;
    for (int i = 0; i < n - 1; i++)
    {
        if (a[i + 1] - a[i] < diff)
            diff = a[i + 1] - a[i];
        m = i + 1;
        k = i;
    }
    cout << a[m] << " " << a[k];
}

int main()
{
    int n, k;
    cout << "Enter a number:";
    cin >> n;
    int a[n];
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int i = 0; i < n; i++)
    {
        cout << a[i] << " ";
    }
    cout << endl;
    sort(a, n);
    diff(a, n);
}

You might also like