Home > Software engineering >  How can i prohibit duplicate input values on a 2D array?
How can i prohibit duplicate input values on a 2D array?

Time:12-09

Here is my code and I want to restrict reoccurring values when the user is trying to input the same value. It would be best if everything is only at the main function because i'm still learning about declaring more functions.

`

#include <iostream>
using namespace std;

    int main() {
        
        int num[10][10];
        int times;
        
        cout << "Please input the number of times you wish to enter a value but does not exceed to 100: ";
        cin >> times; 
        
        cout << "Enter a value " << times * times << " times." << endl;
        
        
        for(int i = 0; i < times; i  ) {
            for(int k = 0; k < times; k  ) {
                cout << "Please input a number on index [" << i << "][" << k << "]: ";
                cin >> num[i][k];
            }
        }
        
        //Displaying the inputs
        cout << "Printing all values inside the array: " << endl; 
        for(int i = 0; i < times; i  ) {
            cout << endl;
             for(int k = 0; k < times; k  ) {
                cout << "\t" << num[i][k] << " ";     
            }
        }

 return 0;        
}

`

This is my expected output to be when a user tries to input a duplicate value:

Please input a number on index [0][0]: 7 Please input a number on index [0][1]: 7 Value already entered. Please try again. Please input a number on index [0][1]:

CodePudding user response:

In this context, you could have a function like this:

bool doesExist(
    const int array[][10], const size_t size, const int value, int x, int y)
{
    for (size_t i = 0; i < size; i  )
        for (size_t j = 0; j < size; j  ) {
            if (x == i && y == j)
                continue; // Skip the new element.

            // If duplicate found
            if (value == array[i][j])
                return true;
        }

    return false;
}

It takes the array, its size, the value to be compared, and the position of the unique element inserted for the first time as arguments.

You could implement it this way:

cin >> num[i][k];

if (doesExist(num, times, num[i][k], i, k)) {
    cout << "Already exists.\n";
    ...
}

This is not the best approach to this problem. In C , it is recommended to apply STL containers as they provide more safety and iterators.

CodePudding user response:

You just want the use not to enter duplicate values :-

  1. First very basic you can just check all the previous values, if it matches with the current one then you can tell the user to change the value.

  2. You can use unordered_map, as map is (key,value) pair whenever you insert any value in map just set its corresponding value to 1, and then you can check in your map if thats value is already present in map or not if its present then you can tell the user to change, in map it will be easy to search.(code is simple)

  • Related