Search an array rows and columns

I am having difficulty here im very new to C++, i started writing this code for a project to search an array for two names then display that row from the array that matches input.

for example if i choose Billy and Scott::

billy is in column 2 and scott is in column 5

so the row to display will be Row 1

OR

if i choose john and andrew::
john is in column 1 and andrew is in column 5

so the row to display will be Row 6

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
  #include <iostream>  

using namespace std;     

int main()

{  

char search; 


/*************************************************************************************/

//Change value in [] to MAX value of the array.

char array[48] = {
	
					//   EXAMPLE ARRAY

           "Peter","Billy","Andrew","John","Scott","Alice"
           "Alice","Billy","Andrew","Scott","John","Peter"
           "Alice","Scott","Andrew","Billy","Peter","John"
           "Alice","Billy","Peter","Scott","John","Andrew"
           "Billy","Andrew","John","Alice","Peter","Scott"
           "John","Billy","Peter","Alice","Andrew","Scott"
           "Billy","John","Peter","Andrew","Scott","Alice"
           "Billy","Alice","Andrew","Peter","Scott","John"


};
																			
/**************************************************************************************/	 
     	 
	 cout<<"Enter 2 Names" << endl; // User input.

	 cin >> search; 
	 
	for (int i = 0; i <= 48; i++) // MAX value of array after <=

{

/*

	TODO:

	  search has to be something like  1st Name=Alice. 
	                                   2nd Name=Andrew. 
	
	  Display Row = "Alice","Billy","Andrew","Scott","John","Peter","Scott","Steven"
	
	  SEARCH Criteria:
	 
	  Find the name Alice. 

	  Count 2 columns along the same row from Alice, Find Andrew.

	  Then display the ROW 
*/



	if (search = array[i])

	cout << search << "Result!" << endl; 

	//  ** cout Display Row **


}

	 system("pause");
     return 0;

 
}
you need an multidimensional array of type string.
i now have something like this, there are comments in code at if statement where my problem is.


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
#include<iostream>
#include<conio.h>
#include<string.h>
#include<cstring>
using namespace std;
 
int main()
{
 
char box[6][6]= { 'P','B','A','J','S','T',

			    'T','B','A','S','J','P',

			    'A','S','T','B','P','J',

			    'T','B','P','S','J','A',

			    'B','A','J','T','P','S',

			    'J','B','P','T','A','S',					 

					 
};
 
 
for(int i=0;i<6;i++) 
{
for(int j=0;j<6;j++)

{

// something wrong here.....

if (box[i-1][i]== 'P' && box[j+5][j]== 'T') 


// need box containing P then on same row get box containing T
// program is just showing ++

{
cout<<box[i][j];
}
else
{
cout<<"+";
}
}
cout<<endl;
}
getch();

return 0;
}
Hi,

Please try with the blow code, i think this will satisfy your requirement.

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
int main()
{
	char box[6][6]= { 'P','B','A','J','S','T',

					'T','B','A','S','J','P',

					'A','S','T','B','P','J',

					'T','B','P','S','J','A',

					'B','A','J','T','P','S',

					'J','B','P','T','A','S',						 
					};

	
	for(int i=0;i<6;i++) 
	{
		bool pFound=false,tFound=false;

		for(int j=0;j<6;j++)
		{
			if (box[i][j]== 'P')//Try for 'P'
				pFound=true;
			if(pFound && box[i][j]== 'T') //if 'P' is found then try for 'T'
				tFound=true;
			
			if(pFound==true && tFound==true)//if both found, out put the row number
			{			
				cout<<"Found In Row No: "<<(i+1)<<std::endl;	
				break;
			}
		}	

		pFound=false;//initialization for next round
		tFound=false;//initialization for next round
	}

	getch();

	return 0;
}


output:
Found In Row No: 1
Found In Row No: 6


Let me know if you have any concerns with above code.
Last edited on
thankyou very helpful :)

id like to elaborate further though. this code finds letter P and letter T but they are from different rows.

Search should find 1st value then second value then display only 1 row where the 2 letters are.

display the row as = 'T','B','A','S','J','P' for example.
this code finds letter P and letter T but they are from different rows.

No, this code is finding the both P and T from the same row
From the output

Row 1: 'P','B','A','J','S','T',

Row 6 : 'J','B','P','T','A','S',

both the rows have 'P' and 'T', first i am serching for 'P' then only serching for 'T'

if(pFound && box[i][j]== 'T') //if 'P' is found then try for 'T'

Can you explain what is wrong.
sorry about that NVT :)

Here is a little description of my project.

I have an array of randomized playing cards saved in a text file.

Whats required is for the user to input any 2 cards, then the program will search the array for the 2 card values.

When the 2 cards have been found (from same row)
program will then display that row to the screen.

so if the array was:

2,3,4,5,6,7,8,9,T,J,Q,K,A
4,5,6,7,9,8,A,J,T,Q,K,2,3
etc.......

cards chosen were 5 and T

so starting from first card 5 is in column four, second card T in column nine

display row 1 as = 2,3,4,5,6,7,8,9,T,J,Q,K,A

if 6 was first card and Q was second

then display row 2 as = 4,5,6,7,9,8,A,J,T,Q,K,2,3

the only thing the user must see are their cards and the row which they belong to.


http://www.computerscienceforeveryone.com/Course_1/Unit_8/Lesson_1/

Here is the start of a set of tutorials on pointers and arrays which will explain everything you need to know in depth.

I would also recommend take the full free course available on this site as it will really do you a service in your understanding of programming. It mainly covers C but most everything can be applied to C++.
Last edited on
Topic archived. No new replies allowed.