• 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.
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.
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)
returnfalse;
if (flag = 1)
returntrue;
// 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()".
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
// change this to 100 and the program may take a fair amount of time
constint N = 10 ;
// array_type is a convenient alias for 'array of NxN int'
// http://en.cppreference.com/w/cpp/language/type_aliasusing array_type = int[N][N] ;
// Elements must be random selected between 0-100 [0-N*N]
constint 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( constauto& 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( constauto& row : array ) for( int v : row )
if( v == value ) returntrue ; // if value is found, return true
returnfalse ; // 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
unsignedlonglong fill_unique( array_type& array ) // passed by reference
{
unsignedlonglong 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.
constauto num_tries = fill_unique(array) ;
// print out the array
print(array) ;
std::cout << "\nnote: " << num_tries << " random numbers were generated.\n" ;
}
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))
{
returntrue;
}
}
}
returnfalse;
}
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.
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.
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?
#include <iostream>
#include <iomanip>
#include <ctime>
constexprint 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))
{
returntrue;
}
}
}
returnfalse;
}
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