Click here to Skip to main content
15,881,424 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;
// Returns true if the item is in the list, otherwise false
bool checkUnique(std::vector<int> &list, int value)
{
    for (int i = 0; i < list.size(); ++i)
    {
        if (list.at(i) == value)
        {
            return true;
        }
    }

    return false;
}

// min inclusive, max exclusive
// make sure to set a new seed before using to help make results more random. (std::srand)
int randomInt(int min, int max)
{
    return rand() % (max - min) + min;
}

int magicSquare()
{

}

int main()
{
    vector<int> list;
    srand(time(0)); // gets random number
    //int arr[3][3];

    while (list.size() < 9) // while the list size is less than 9 it continues to repeat
    {
        int num = randomInt(1, 10); // random number 1 min 10 max

        if (!checkUnique(list, num)) // if the unique is not equal to the list or number than list gets pushed b
        {
            list.push_back(num); // increase the container size by one if the if statement is false
        }
    }

    for (int k = 0; k < list.size(); ++k) // gets the row
    {
        for (int j = 0; j < list.size(); ++j) // gets the column
        {

        cout << list.at(k)(j) << endl; // outputs the row and column
        }
    }

    return 0;
}
Posted
Updated 28-Apr-15 18:21pm
v3
Comments
Sergey Alexandrovich Kryukov 28-Apr-15 23:58pm    
What it would supposed to mean?
Anyway, it's hard to understand much from the code which starts with
#include
#include
#include
#include

Did you see it yourself? Please try to format it properly.
—SA

1 solution

hi,
You have used STL conatiners so i suggest instead of using a 2d array either use an std::map container Or store a std::pair in the vector.

An intro to Std::map[^]

putting pairs in std::vector[^]

some suggestions :
1. give meaningful names to variables. Avoid int i, int k etc.
2.Better use of STL : the work of finding the unique element in the vector can be done by using the STL find or find_if functions.
for example :
std::find(vector.begin(), vector.end(), item)!=vector.end()

so you do not have to write a separate function and save yourself a function call.

3).Iterators are the preferred method of traversing through STL containers.how iterators are used[^]
3). you can use new feature of automatic type deduction (keyword 'auto') or range based for loop (you can google for these).

hope this helps !!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900