Multidimensional Array

Below is my first assignment with multidimensional arrays. I was able to create the 5x5 array with random numbers, I am just unsure to get the program to stop searching after it finds the first position of an integer so it only outputs one location and how to tell the user the number is not in the array. Here is what I have so far. Hope someone can help.

In this task, you will generate a 5×5 array and fill it with random integers between 0 and 24 inclusive. Of course, the numbers may not be distinct. For example, your array could look like this after it has been filled with random integers:
4 0 3 17 9
3 23 2 13 12
7 15 8 4 11
6 10 21 0 20
19 22 1 14 5
Now ask the user for an integer and search the array. If the number exists, tell the user the first location. In the above array, 4 exists in two position. The first is (0,0), so your program should output this. Since 21 exists in only one cell, your program should output (3,2).

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
void multiA (){ 
    int size = 5;
    int num;
    int multiA[5][5];
    srand(time(0));
 
    for (int row = 0; row < size; row++) 
    {    
        for (int column = 0; column < size; column++) 
        { //this will start at row 0 and column 0 
           multiA[row][column] = rand() %25; //column will increment by 1 and fill the board across row 0
           cout << multiA [row][column]<< ' ';
        } //once this goes through 5 columns it will exit, start a new line, and increment row by 1 
        cout << endl; 
    } 
       cout << "Please enter a integer to locate its position in the array.\n";
     cin >> num;  
      for (int row = 0; row < size; row++){   
        for (int column = 0; column < size; column++){
             if (num == multiA[row][column]){
                  int r,c;
                  r = row;
                  c = column;
                  cout << "The position of " << num << " is: (" << r << "," << c << ")\n";
                 //added break; here but did not stop searching if there was more than on number
             } //cant have an else statement here saying number is not in array because it only searched in that specific row and column
            //also tried adding break; here
           }            
        }      
}
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
void multiA(){
	int size = 5;
	int num;
	int multiA[5][5];
	srand( time( 0 ) );

	for( int row = 0; row < size; row++ )
	{
		for( int column = 0; column < size; column++ )
		{ //this will start at row 0 and column 0 
			multiA[row][column] = rand() %25; //column will increment by 1 and fill the board across row 0
			cout << multiA[row][column]<< ' ';
		} //once this goes through 5 columns it will exit, start a new line, and increment row by 1 
		cout << endl;
	}
	cout << "Please enter a integer to locate its position in the array.\n";
	cin >> num;

	r = -1; c = -1;
	for( int row = 0; row < size; row++ ){
		for( int column = 0; column < size; column++ ){
			if( num == multiA[row][column] ){
				int r, c;
				r = row;
				c = column;
				cout << "The position of " << num << " is: (" << r << "," << c << ")\n";
				break;
			}
		}
		if( r != -1 ) // if a row was found
			break;
	}
}


I'm assuming r and c are global. if not, you need to declare them.

break; only exits the current loop. since you're nesting two loops, you need to add the additional trap on the outer loop.
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
void multiA(){
	int size = 5;
	int num;
	int multiA[5][5];
	srand( time( 0 ) );

	for( int row = 0; row < size; row++ )
	{
		for( int column = 0; column < size; column++ )
		{ //this will start at row 0 and column 0 
			multiA[row][column] = rand() %25; //column will increment by 1 and fill the board across row 0
			cout << multiA[row][column]<< ' ';
		} //once this goes through 5 columns it will exit, start a new line, and increment row by 1 
		cout << endl;
	}
	cout << "Please enter a integer to locate its position in the array.\n";
	cin >> num;

	r = -1; c = -1;
	for( int row = 0; row < size; row++ ){
		for( int column = 0; column < size; column++ ){
			if( num == multiA[row][column] ){
				int r, c;
				r = row;
				c = column;
				cout << "The position of " << num << " is: (" << r << "," << c << ")\n";
				break;
			}
		}
		if( r != -1 ){ // if a row was found
			break;
               }
	}
                 if ( r == -1){ //added this so if the number is not in the array 
                 cout << num << " is not in the array.\n";
         } 
}


Thanks. It worked perfectly. I added the if statement if the number is not in the array. I tested it and it worked. does it look good to you?

Can I also use bool instead and of -1?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 cout << "Please enter a integer to locate its position in the array.\n";
     cin >> num;
     bool r = false;
     for( int row = 0; row < size; row++ ){
		for( int column = 0; column < size; column++ ){
			if( num == multiA[row][column] ){
			    r = true;			
				cout << "The position of " << num << " is: (" << row << "," << column << ")\n";
				break;
			}			
		}
		if( true ){ // if a row was found
			break;
        }            
	}
	    if ( false){
                 cout << num << " is not in the array.\n";
         } 
}
Last edited on
yes you can.
Thanks.

Do you know how I can make another array but this time every value can only appear once?
Topic archived. No new replies allowed.