Answer to Question #155946 in C++ for Abdul Rehman

Question #155946

Write a C++ program in which, read data from user into an integer array. Now remove all duplicate values. 



1
Expert's answer
2021-01-17T20:55:53-0500
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
    srand((int)time(0));
    int n;
    cout <<"n="; cin >>n;

    double*a = new double[n];

    for (int i = 0; i < n; i++)
    {
        a[i]=rand()%5 + 1;
        cout <<a[i]<<" ";
    }
    cout <<endl;

    for (int i = 0; i < n - 1; i++)
        for (int j = i + 1; j < n; j++)
            if (a[i]==a[j])
            {
                for (int k = j; k < n; k++)
                    a[k]=a[k + 1];
                n--;
                j--;
            }

    for (int i = 0; i < n; i++)
        cout <<a[i]<<" ";
    cout <<endl;
    delete[]a;
    system("pause");
    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