Answer to Question #254618 in C++ for Kamo

Question #254618
Write a C++ programm to create a one-dimensional (integer type) array of size N. Create a Manu-based approach and solve the following. I) Number of occurrences of a given Number in the array. Ii) Remove the duplicate elements in the array. III) Second largest and Second smallest element in the array. IV) Search an element in an array. V) Sorting the array elements in ascending or descending based on user choice.
1
Expert's answer
2021-10-21T16:05:34-0400
#include <iostream>


using namespace std;
void NumberOfOccur(int arr[],int n,int num){
    int count=0;
    for(int i=0;i<n;i++){
        if (arr[i]==num)
            count++;
    }
    cout<<"\nNumber of occurance of "<<num<<" is "<<count;
}
int RemoveDublicates(int arr[],int n){
    if (n==0 || n==1)
        return n;
    int tp[n];
    int j = 0;
    for (int i=0; i<n-1; i++)
        if (arr[i] != arr[i+1])
            tp[j++] = arr[i];
    tp[j++] = arr[n-1];
    for (int i=0; i<j; i++)
        arr[i] = tp[i];
 
    return j;
}
void SecondLargestSmallest(int arr[],int n){
    
}
void searchElement(int arr[],int n){
    
}
void sort(int arr[],int n){
    
}
int main()
{
    int n=10;
    int arr[n]={1, 1, 2, 2, 3, 4, 4, 4, 5, 5};
    cout<<"Choose an operation to perform:\n";
    cout<<"1. Number of occurrences of a given Number in the array:\n";
    cout<<"2. Remove the duplicate elements in the array:\n";
    cout<<"3. Second largest and Second smallest element in the array:\n";
    cout<<"4. Search an element in an array:\n";
    cout<<"5. Sorting the array elements in ascending or descending based on user choice:\n";
    int c;
    cin>>c;
    if (c==1){
        int num;
        cout<<"\nEnter a number: ";
        cin>>num;
        NumberOfOccur(arr,n,num);
    }
    else if(c==2){
       int f=RemoveDublicates(arr,n);
       for (int i=0; i<n; i++)
            cout << arr[i] << " ";
    }
    else if(c==3){
        SecondLargestSmallest(arr,n);
    }
    else if(c==4){
        searchElement(arr,n);
    }
    else if(c==5){
        sort(arr,n);
    }
    else{
        cout<<"\nInvalid choice";
    }
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS