Bool array control

Hi guys,

I need write a function to check element via bool. I have a random number array function. Return function if there is more than one in a number. Array's range 1 to 100.

My random array function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  int randomarray(int arr[10][10])
{
	int i, j;
	srand((int)time(0));
	for (i = 0; i < 10; i++)
	{
		cout << endl;
		for (j = 0; j < 10; j++)
		{
			arr[i][j] = rand() % 100 + 1;
			cout << arr[i][j] << "\t";
		}
	}
return arr[10][10];
}
what you have written, will not work. you have to use pointers to return an array from a function
calling that function will print an array on console(because of cout), but you won't be able to return an array
ok, but how can i calling random number array function with pointers in accordance with the my purpose?
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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int** randomarray(unsigned height, unsigned width)
{
	int** array2D = 0;
	array2D = new int*[height];
	srand(time(0));
	for (int h = 0; h < height; h++)
	{
		array2D[h] = new int[width];

		for (int w = 0; w < width; w++)
		{
			
			array2D[h][w] = rand() % 100 + 1;
		}
	}

	return array2D;
}

int main() {

	int height = 10, width = 10;
	int** array = randomarray(height, width);
	for (int i = 0; i < 10; i++) {
		for (int j = 0; j < 10; j++) {
			cout<<array[i][j]<<" ";
		}
		cout << endl;
	}


	system("pause");
	return 0;
}


Actually, you don't need to return anything. The array is already being passed as a parameter.

Note, I removed the srand() call from the function, and placed it at the start of main(). The reason for that is to avoid the possibility of calling srand more than once, which usually does not give the expected outcome, it will usually re-seed with the same value and hence generate the same set of numbers all over again.

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
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;

const int ROWS = 10;
const int COLS = 10;

void randomarray(int arr[ROWS][COLS])
{
    for (int i = 0; i < ROWS; i++)
        for (int j = 0; j < COLS; j++)
            arr[i][j] = rand() % 100 + 1;
}

void print_array(int arr[ROWS][COLS])
{
    for (int i = 0; i < ROWS; i++)
    {
        for (int j = 0; j < COLS; j++)
        {
            cout << setw(4) << arr[i][j];
        }
        cout << '\n';
    }
}

int main()
{
    srand((int)time(0));

    int array[ROWS][COLS];  
  
    cout << "First:\n";    
    randomarray(array);
    print_array(array);
    
    cout << "\nSecond:\n";    
    randomarray(array);
    print_array(array);
}


Note the use of constants for the number of rows and columns. That's better than having lots of magic numbers scattered throughout the code.
Last edited on
I agree with chervil, somewhy I thought you had to return an array :D, but can you explain, @BJK, what do you mean by checking element via bool?
thx guys but that's not what I want. @Chervil and @nick2361

It's my homework.

I need to 2D random arrray(1-100)

After printing this random array, I need to print the ordered list of this array on a console's bottom line. Like insertion sort or bubble sort.

And No more than one element in the array. need to check if the same item is on the array with bool.

and if he assigns the same element, I want him to return the same element until he throws it. and I need to know how many times he turns it.

(I don't know English well I'm sorry :D )

Example: ([3][3])

4 5 1
3 2 6
7 9 8

1 2 3
4 5 6
7 8 9 True. I need this.

5 5 6
3 4 2
2 3 5

2 2 3
3 4 5
5 5 6 False.
Last edited on
So, as I understand, you need to create a 2d array, randomize it's elements and then sort the array. Then using a bool type variable, check if the array has duplicate elements and if does, change that elements until they are all different and when all they are different, print the sorted array, am I right?
Yes you are right @nick2361 . I need random and sorted array in output. Both array in output.
And info of how many times return until they are all different.
Last edited on
Topic archived. No new replies allowed.