Answer to Question #295648 in C++ for Kdrkem

Question #295648

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.



1
Expert's answer
2022-02-09T08:04:47-0500
#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;
}

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