How to search a 2d array? 4 by 4 matrix

I am trying to write code that will search a 4 by 4 matrix called matrix[4][4].
Users will be able to type their own values in. At the end it questions the user what value does the user wish to search for. The code I have written will display the wrong message. For example, even if the matrix has a 22 in it the program will say that the matrix does not contain 22. This is the loop that searches the matrix.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
	int result;
	cout << "Which value would you like to search for in the matrix?: ";
	cin >> result;
	
	int search = 0;
	for (search = 0; search < 4; search++)
	{
		for (int value = 0; value < 4; value++)
		{
			if (matrix[search][value] == result)
			{
				cout << "Your matrix contains " << result << endl;
				return search; 
			}
			
			else
			
				cout << "Your matrix does not contain " << result << endl;
				return 0;		
			
		}
	}
With your current logic, the code will return with a not-found result if the first element tested (the [0][0] component) is not the sought value. Make sure that the not-found option only applies after completing all for loops. You won't need an "else".
if its an array, collapse it to 1-d. //it only works on solid blocks of memory. 2d arrays are solid.
int * pmatrix = &matrix[0][0];
for(int dx = 0; dx < 16; dx++) //16 = rows*cols, in this case, 4x4
{
if(pmatrix[dx] == result)
cout
else
etc
}
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
#include <iostream>

const int arr_size { 4 };

void displayArray(int[arr_size][arr_size]);
bool findArray(int[arr_size][arr_size], int);

int main()
{
   // create a 4 x 4 array, zero filled
   int arr[arr_size][arr_size] { };

   // let's populate the elements
   for (int row { }; row < arr_size; row++)
   {
      for (int col { }; col < arr_size; col++)
      {
         arr[row][col] = ((row + 1) * 10) + col + 1;
      }
   }

   // let's display the 2D array
   displayArray(arr);
   std::cout << '\n';

   bool found { false };

   // let's find 23
   int index { 23 };

   found = findArray(arr, index);

   if (found)
   {
      std::cout << index  << " was found.\n";
   }
   else
   {
      std::cout << index << " was NOT found.\n";
   }

   // let's find 98
   index = 98;

   found = findArray(arr, index);

   if (found)
   {
      std::cout << index << " was found.\n";
   }
   else
   {
      std::cout << index << " was NOT found.\n";
   }
}

void displayArray(int arr[arr_size][arr_size])
{
   for (int row { }; row < arr_size; row++)
   {
      for (int col { }; col < arr_size; col++)
      {
         std::cout << arr[row][col] << ' ';
      }
      std::cout << '\n';
   }
}

bool findArray(int arr[arr_size][arr_size], int num_to_find)
{
   for (int row { }; row < arr_size; row++)
   {
      for (int col { }; col < arr_size; col++)
      {
         if (arr[row][col] == num_to_find)
         {
            return true;
         }
      }
   }
   return false;
}

11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

23 was found.
98 was NOT found.
If you want to know what the element coordinates are:
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
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>

const int arr_size { 4 };

struct arr_elem
{
   bool found { false };
   int  row   { };
   int  col   { };
};

void displayArray(int[arr_size][arr_size]);
arr_elem findArray(int[arr_size][arr_size], int);

int main()
{
   // create a 4 x 4 array, zero filled
   int arr[arr_size][arr_size] { };

   // let's populate the elements
   for (int row { }; row < arr_size; row++)
   {
      for (int col { }; col < arr_size; col++)
      {
         arr[row][col] = ((row + 1) * 10) + col + 1;
      }
   }

   // let's display the 2D array
   displayArray(arr);
   std::cout << '\n';

   arr_elem found { };

   // let's find 23
   int index { 23 };

   found = findArray(arr, index);

   if (found.found == true)
   {
      std::cout << index << " was found at ["
                << found.row << "][" << found.col << "].\n";
   }
   else
   {
      std::cout << index << " was NOT found.\n";
   }

   // let's find 98
   index = 98;

   found = findArray(arr, index);

   if (found.found == true)
   {
      std::cout << index << " was found at ["
                << found.row << "][" << found.col << "]\n";
   }
   else
   {
      std::cout << index << " was NOT found.\n";
   }
}

void displayArray(int arr[arr_size][arr_size])
{
   for (int row { }; row < arr_size; row++)
   {
      for (int col { }; col < arr_size; col++)
      {
         std::cout << arr[row][col] << ' ';
      }
      std::cout << '\n';
   }
}

arr_elem findArray(int arr[arr_size][arr_size], int num_to_find)
{
   arr_elem found { };

   for (int row { }; row < arr_size; row++)
   {
      for (int col { }; col < arr_size; col++)
      {
         if (arr[row][col] == num_to_find)
         {
            found.found = true;
            found.row = row;
            found.col = col;
            return found;
         }
      }
   }
   return found;
}

11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44

23 was found at [1][2].
98 was NOT found.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   const int N = 4;
   int A[N][N] = { {  1,  2,  3,  4 },
                   {  5,  6,  7,  8 },
                   {  9, 10, 11, 12 },
                   { 13, 14, 15, 16 } };

   for ( int result : { 1, 16, 27, -6, 10 } )
   {
      int loc = find( A[0], A[0] + N * N, result ) - A[0];
      cout << result << ( loc != N * N ? " is " : " is not " ) << "found\n";
   }
}

1 is found
16 is found
27 is not found
-6 is not found
10 is found




Or you can identify the location:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   const int N = 4;
   int A[N][N] = { {  1,  2,  3,  4 },
                   {  5,  6,  7,  8 },
                   {  9, 10, 11, 12 },
                   { 13, 14, 15, 16 } };

   for ( int result : { 1, 16, 27, -6, 10 } )
   {
      int loc = find( A[0], A[0] + N * N, result ) - A[0];
      if ( loc == N * N ) cout << result << " is not found\n";
      else                cout << result << " is found in row " << loc / N << ", column " << loc % N << '\n';
   }
}

1 is found in row 0, column 0
16 is found in row 3, column 3
27 is not found
-6 is not found
10 is found in row 2, column 1
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <core/util/ArrayND.hpp>

int main() {
    acpl::Array2D<int> mat( { {1,  2,  3,  4},
                              {5,  6,  7,  8},
                              {9, 10, 11, 12} } ) ;

    assert(mat.nCols() == 4);
    assert(mat.nRows() == 3);

    for(int result : { 1, 7, 11, 12, -6 }) {
        auto indices = mat.find(result);
        if(indices) {
            std::cout << result << " is found in row " << indices.value()[0] << ", column " << indices.value()[1] << '\n';
        } else {
            std::cout << result << " is not found\n";
        }
    }
}


1 is found in row 0, column 0
7 is found in row 1, column 2
11 is found in row 2, column 2
12 is found in row 2, column 3
-6 is not found


https://sourceforge.net/p/acpl/code/ci/master/tree/acpl/Examples/ArrayND/Example_Array2D/main.cpp
Topic archived. No new replies allowed.