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.
#include <iostream>
usingnamespace 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)
returntrue;
}
returnfalse;
}
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;
}
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.
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.
#include <iostream>
#include <string>
usingnamespace 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;
}