Use a one-dimensional array to solve the following problem. Read in 20 numbers, each of which is between 10 and 100, inclusive. As each number is read, validate it and store it in the array
only if it isn’t a duplicate of a number already read. After reading all the values, display only the unique values that the user entered.
#include <iostream>
#include <vector>
int main()
{
std::vector<int> arr;
for (int i = 0; i < 20; i++)
{
int tmp = 0;
while (1)
{
std::cout << i+1<<".Enter value from 10 to 100 : ";
std::cin >> tmp;
if (tmp >= 10 && tmp <= 100)
{
break;
}
}
int unique = 1;
for (int j = 0; j < arr.size(); j++)
{
if (arr[j] == tmp)
{
unique = 0;
break;
}
}
if (unique)
{
arr.push_back(tmp);
}
}
std::cout << "\nUnique values : ";
for (int i = 0; i < arr.size(); i++)
{
std::cout << arr[i] << " ";
}
std::cout << std::endl;
system("pause");
return 0;
}
Comments
Leave a comment