Random Elements in Array

Hey. I have to write a program for lesson.

• Create an array 10x10
• Elements must be random selected between 0-100
• A function must control the elements. If its same, must create another random element.

Sorry for english mistakes. I'm just sending my codes. Please help.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
  bool checkElement (int mtrx[10][10], int a, int b)
{
	int flag;
	for (int s3 = 0; s3 <= a ; s3++)
	{
		for (int s4 = 0; s4 < 10 ; s4++)
		{
			if ((mtrx[a][b] == mtrx[s3][s4]) && (s3 != a && s4 != b))
			{
				flag = 0;
			}
			else
				flag = 1;
		}
	}
	if (flag = 0)
		return false;
	if (flag = 1)
		return true;

}




void create(int mtrx[10][10])
{
	srand(time(NULL));
	for (int s1 = 0; s1 < 10; s1++)
	{
		for (int s2 = 0; s2 < 10; s2++)
		{
			mtrx[s1][s2] = rand() % 100 + 1;
			checkElement(mtrx, s1, s2);
			if (checkElement == false)
			{
				s2--;
			}
			else
			{
				/*NOTHING*/
			}
		}
	}
	
}
Last edited on
Helo arf,

I have revised your code a little. The use of "row" and "col" helps you visualize the array easier. Not necessary to make these changes, but it helps. I have some comments in the code worth reading.

The way "srand" works you should only do this once at the beginning of main.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
bool checkElement(int mtrx[10][10], int _row, int _col)
{
	int flag;
	for (int row = 0; row <= a; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			if ((mtrx[_row][_col] == mtrx[row][col]) && (row != _row && col != _col))
			{
				flag = 0;
			}
			else
				flag = 1;
		}
	}
	if (flag = 0)
		return false;
	if (flag = 1)
		return true;
	// Could  also be written as.
	//if (flag)
	//	return true;

	//return false;
}




void create(int mtrx[10][10])
{
	srand(time(NULL));  // <--- Put in main. Only needs done once. Not everytime the function is called.
	//srand(static_cast<std::size_t>(time(NULL))); // <--- "std::size_t" is another name for unsigned int.
	                                             // better to use this example becuse "time()" returns an
	                                             // unsigned int.
	for (int row = 0; row < 10; row++)
	{
		for (int col = 0; col < 10; scol++)
		{
			mtrx[row][col] = rand() % 100 + 1;
			checkElement(mtrx, s1, s2);
			if (checkElement == false)
			{
				col--;
			}
			else  //  <--- Else statement not needed if nothing.
			{
				/*NOTHING*/
			}
		}
	}
}


I will have to work something up to test these functions not having the rest of your code.

At the moment everything looks OK except the placement of "srand()".

Hope that helps,

Andy
I used "srand()" in main and there is no warning. But I have logical errors.

Actually we're not coding in English. I've translated to help for topic.

Thanks.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

// change this to 100 and the program may take a fair amount of time
const int N = 10 ;

// array_type is a convenient alias for 'array of NxN int'
// http://en.cppreference.com/w/cpp/language/type_alias
using array_type = int[N][N] ;

// Elements must be random selected between 0-100 [0-N*N]
const int MAX_RANDOM_VALUE = N*N ;
// Note: make this smaller than N*N-1 and we would get an infinite loop

void print( const array_type& array ) // passed by reference to const
{
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    // for each row in the array
    for( const auto& row : array )
    {
        // print each value in the row
        for( int v : row ) std::cout << std::setw(6) << v ;
        std::cout << '\n' ;
    }
}

// return true if the NxN array contains the value
bool contains_value( const array_type& array, int value ) // passed by reference to const
{
    // check each element in the array
    for( const auto& row : array ) for( int v : row )
        if( v == value ) return true ; // if value is found, return true

    return false ; // we have gone through the entire array and did not find value
}

// A function must control the elements. If its same, must create another random element.
// fill the array with unique random values. Elements must be random selected between 0-100
// Just for interest, return the number of candidate random values that were generated
unsigned long long fill_unique( array_type& array ) // passed by reference
{
    unsigned long long num_candidates = 0 ;

    // for each element in the array
    for( auto& row : array ) for( int& element : row )
    {
        // generate a random number in the range 0-N*N
        int value ;
        do
        {
             value = std::rand() % (MAX_RANDOM_VALUE+1) ;
             ++num_candidates ;

        } while( contains_value( array, value ) ) ;
          // If its same, must create another random element.

        element = value ; // set the element to the unique random value
    }

    return num_candidates ;
}

int main()
{
    // seed the legacy random number generator
    std::srand( std::time(nullptr) ) ;

    // Create an array (of integers) 10x10
    array_type array ;

    // for each element in the array, set the value to -1 (note: -1 is not in [0,N*N])
    for( auto& row : array ) for( int& v : row ) v = -1 ;

    // fill the array with unique random values.
    const auto num_tries = fill_unique(array) ;

    // print out the array
    print(array) ;

    std::cout << "\nnote: " << num_tries << " random numbers were generated.\n" ;
}

http://coliru.stacked-crooked.com/a/8ee7073ba7af2c2c
Helo arf,

Sorry it took so long. I knew that it had something to do with the if statement in the following code. One simple change made all the difference. As you can see I changed the second "&&" to "||" and I have yet to find a duplicate i the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
bool checkElement(std::array <std::array< int, 10 >, 10 >& mtrx, int _row, int _col)
{
	//int flag{};

	for (int row = 0; row <= _row; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			if ((mtrx[_row][_col] == mtrx[row][col]) && (row != _row || col != _col))
			{
				return true;
			}
		}
	}

	return false;
}


I also shortened the code a little.

Hope that helps,

Andy

P.S. JLBorges has a very good solution.

Edit: I was working in a different direction and miss changing the function definition back to what it was. Using your original definition should not change the way the function works.
Last edited on
So thanks for all replies. But Handy Andy's codes are so complex. Cannot we do all thins like my program? And there is a lot of terms that i don't know. Its my first education year.
Hello arf,

If there is something you do not understand let me know.

Andy
Its my first year in computer engineering. And we didn't learn something like you write codes yet. I mean auto&, that for loops etc.

So if i understand everything you write, it won't be useful for this homework. But i swear, gonna learn everything you've used. Cause it looks amazing.

We know for loops like i write, arrays basicly, random basicly... Why my codes isn't working, cannot you fix a little and make useful?

Thanks, and pls check PM.
To all following,

What I should have posted.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <iomanip>
#include <ctime>

constexpr int MAXSIZE{ 10 };  // <--- Can also use "const"

bool checkElement(int mtrx[MAXSIZE][MAXSIZE], int _row, int _col)
{
	//int flag{};

	for (int row = 0; row <= _row; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			if ((mtrx[_row][_col] == mtrx[row][col]) && (row != _row || col != _col))
			{
				return true;
			}
		}
	}

	return false;
}

void create(int mtrx[MAXSIZE][MAXSIZE])
{
	//srand(time(NULL));  // <--- Put in main. Only needs done once. Not everytime the function is called.
						//srand(static_cast<std::size_t>(time(NULL))); // <--- "std::size_t" is another name for unsigned int.
						// better to use this example becuse "time()" returns an
						// unsigned int.
	for (int row = 0; row < 10; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			mtrx[row][col] = rand() % 100 + 1;
			if (checkElement(mtrx, row, col))
			{
				col--;
			}
		}
	}
}

void Print(int mtrx[MAXSIZE][MAXSIZE])
{
	for (int row = 0; row < 10; row++)
	{
		for (int col = 0; col < 10; col++)
		{
			std::cout << std::setw(4) << mtrx[row][col] << "  ";
		}
		std::cout << std::endl;
	}
}

int main()
{
	//int mtrx[10][10]{};
	int  mtrx[MAXSIZE][MAXSIZE]{};

	srand(static_cast<std::size_t>(time(NULL)));

	create(mtrx);
	
	Print(mtrx);

	return 0;
}  // End main 


Andy
Hand Andy, magnificent! Thanks everyone.
Topic archived. No new replies allowed.