lundi 6 février 2017

Non repeating numbers with 2D vector (C++)

I am currently working on a project, but I can not seem to figure out how to fill my matrix with distinct random integers. This is the only part I am stuck on. Is there an easy way to go about this?

Is there a way that I can compare each pushed value with the previous values of the matrix?

#include <iostream>
#include <iomanip>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <algorithm>

using namespace std;

void matrix(int );

int main()
{
    int input;
    char again = 'y';

    cout << "Welcome to my perfect matrix program. The function of the program is"
        "\nto:"
        "\n"
        "\n   1. Allow the user to enter the size of the perfect matrix, such as"
        "\n      N. N>=2."
        "\n   2. Create a 2 D vector array of size N x N."
        "\n   3. Populate the 2 D vector array with distinct random numbers."
        "\n   4. Display the sum for each row, column, and diagonals then"
        "\n      determine whether the numbers in N x N vector array are perfect"
        "\n      matrix numbers.";
    cout << endl << endl;

    while(again == 'y' || again == 'Y')
    {
        cout << "Enter the size of the matrix : ";
        cin >> input;

        while(input < 2)
        {
            cout << "\nError *** Incorrect input - You entered a number < 2"
                    "\nEnter a Positive integer Number >= 2: ";
            cin >> input;
        }

        matrix(input);

        cout << "\nWould you like to find another perfect matrix?"
                "\nEnter y || Y for yes or n || N for no: ";
        cin >> again;

        if(again == 'N' || again == 'n')
        {
            cout << "\nThis perfect matrix algorithm is implemented by Brandon"
                    "\nFebruary 13th - 2017\n";
        }
    }

return 0;
}

void matrix(int value)
{
    int N = value;

    vector<vector<int> >arr;

    srand(time(0));

    for(int i = 0; i < N; i++)
    {
        vector<int> temp;
        for(int j = 0; j < N; j++)
        {
            int digit = rand()%16;
            temp.push_back(digit);
        }
        arr.push_back(temp);
    }


    cout << endl;
    cout << "The perfect matrix that is created for size " << value << ":";
    cout << endl << endl;

    for(int i = 0; i < arr.size(); i++)
    {
        for(int j = 0; j < arr[i].size(); j++)
        {
            cout << arr[i][j];
            cout << "   ";
        }
        cout << endl << endl;
    }

    cout << endl;

}




Aucun commentaire:

Enregistrer un commentaire