Hello everyone,
I am writing this post again because there were ambiguities in the previous one. I need help with this task tomorrow. I will present what I have, what I got from you and ask which code is best to use to perform this task.
I will start with the program I wrote to fill the 6x6 array with random numbers and display it on the console. The program displays random numbers stored in an array in the range of -5 to 64 ... I believe deeply that it contains no error.
int x,y;
int rand_tab[6][6];
int main()
{
srand(time(0));
for(y=0;y<6y++)
{
for(x=0;x<6;x++)
{
rand_tab[y][x]=(( rand() % 68 ) - 25 );
cout << rand_tab[y][x] << " ";
}
cout << endl;
}
I know more or less what's going on, but I'm wondering how to modify the program to select numbers divisible by 3. How to add this code. I came up with such an idea:
srand(time(0));
for(y=0;y<6;y++)
{
for(x=0;x<6;x++)
{
if( (rand_tab[ y ]&& rand_tab[x]) % 3 == 0 )
{
rand_tab[y][x]=(( rand() % 68 ) - 25 );
cout << rand_tab[y][x] << " ";
}
Unfortunately, the program does not show me any values in this case. Zong :-(
The next task that I need is to get started adding to the program a function that would display from the drawn numbers only those values that are not repeated from the rand () function.
I found a program on the net that does the rand () function does just that, in the range of 0-100:
#pragma hdrstop
#pragma argsused
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <ctime>
void swap( int array[], int pos1, int pos2 ) {
int tmp = array[ pos1 ];
array[ pos1 ] = array[ pos2 ];
array[ pos2 ] = tmp;
}
int _tmain( int argc, _TCHAR * argv[] ) {
srand( time( NULL ) );
int numbers[ 100 ];
int matrix[ 6 ][ 6 ];
// fill in the numbers
for( int i = 0; i < 100; ++i ) {
numbers[ i ] = i;
}
// a random number of swaps
int repeat = rand() % 100 + 100;
// perform the swaps...
for( int i = 0; i < repeat; ++i ) {
// ... with random positions
swap( numbers, rand() % 100, rand() % 100 );
}
// fill in the matrix
for( int i = 0; i < 6; ++i ) {
for( int j = 0; j < 6; ++j ) {
matrix[ i ][ j ] = numbers[ i * 10 + j ];
}
}
std::cout << "This is the matrix:" << std::endl << std::endl;
// print the matrix
for( int i = 0; i < 6; ++i )
for( i = 0; i < 10; i++ )
{
for( int j = 0; j < 6; ++j ) {
std::cout << matrix[ i ][ j ] << " ";
}
std::cout << std::endl;
}
getchar();
system( "PAUSE" );
return 0;
}
Now the question is, is it a better wau to modify the program I have already written, or modyfing the code mentioned above?
Thank you all for any, even the slightest help.
Thanks guys!