110010101010

110101010110101001010101010100101010
Last edited on
You used a cin inside a loop to fill up the array. Printing of the array will happen in a similar fashion. Simply cout << array[i] in a for loop going from 0 to < 20. The while loop is unnecessary.

Besides the cout statement, you will also have a check inside the loop to see if the number was already printed. The way to do this is, after printing out array[0], all subsequent indeces (1 to 19) should not be printed if their value was found in previous indeces.

So for any index 'i' from 1 to 19, you check all indeces from 0 to i-1. If the number at index i is found in any of those previous indeces, do not print that number, as it is a duplicate. Else print it.

Going by this logic, the duplicate_array becomes unnecessary.


Last edited on
Line 16: Not sure what you're trying to do here. What you are in fact doing is comparing the address of array to itself, which will always be true.

Line 13,19: You want to move these to before your loop. You only want to display this once, not for every number.

Line 18: You can't display an entire array that way. That statement is going to print the address of the array, not the contents.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

bool is_duplicate (int array[], int i)
{   for (int j=0; j<20; j++)
    {   // Check if match and indexes are different
        if (array[i] == array[j] && i != j)
            return true; 
    }
    return false;    
}
          
int main()
{   char response;
    int array[20];

    cout << "Please enter 20 numbers from 1 - 100:  " << endl;
    for(int i = 0; i < 20; i++)
        cin >> array[i];    
 
    cout << "Here is the list of numbers:  " << endl;
    for (int i = 0; i < 20; i++)
    {   if (! is_duplicate (array, i))
            cout << array[i] << endl;
    }    
    cin >> response;
    return 0;
}






Last edited on
1010101010
Last edited on
Please DON'T delete your question after you've received an answer. It ruins this thread as a learning resource for others. It's a selfish abuse of this forum, and of the people who've taken the time and effort to answer your question.
Original question for reference:
I need to write a program that lets the user input 20 numbers from 1 to 100, the values must be stored in a array, then display the values of the array, but if the user inputed the same number or a duplicate, it shouldn't be shown in the "Here is what is in your list ".
- I need help making a solution, not really sure how to do this one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;
int main()
{
    char response;
    int array[20];
    int duplicate_array[20];
    int check = 0;
    for(int i = 0; i < 20; i++)
    {
    cout << "Please enter 20 numbers from 1 - 100:  " << endl;
    cin >> array[i];
    }
 
        while(array==array)
        {
            cout << "Here is the list of numbers:  " << array << endl;
            if // i would assume an if statement here?
        }
    
    cin >> response;
    return 0;

}


Okay sorry, i will do as you say.
Topic archived. No new replies allowed.