How to traverse through two 2d arrays to create a list

Write a program that registers all the students in both streams in different arrays i.e., 5A and 5B. Traverse through both registers and come up with a full list i.e., 5C that has all the names of the students in 5A and 5B . Where the 2nd row value would be F meaning female and the list for 5C would contain only the female students from both lists with F as the value on the second column. So could anyone help me with the code to generate the list?

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
 using namespace std;
int main(){
	string a[5][2];
	string b[6][2];
	int i,j;
	cout<<"Input Class 5A Student Name and Gender(M/F)"<<endl;
	for(i=0;i<5;i++){
		for(j=0;j<2;j++){
		
		cout<<"\nStudent&Gender ["<<i<<"]["<<j<<"]= ";
		cin>>a[i][j];
}
	}
	cout<<"Input Class 5B Student Name and Gender(M/F)"<<endl;
	for(i=0;i<6;i++){
		for(j=0;j<2;j++){
		
		cout<<"\nStudent&Gender ["<<i<<"]["<<j<<"]= ";
		cin>>b[i][j];
}
	}
	cout<<"\nThe List of Class 5A is"<<endl;
	for(i=0;i<5;i++){
		for(j=0;j<2;j++){
			cout<<"\t"<<a[i][j];
	}
		cout<<endl;
	}
	cout<<"\nThe List of Class 5B is"<<endl;
	for(i=0;i<6;i++){
		for(j=0;j<2;j++){
			cout<<"\t"<<b[i][j];
		}
		cout<<endl;
	}
	cout<<"\nThe List of Class 5C is"<<endl;
	cout<<"\t"<<"Name "<<"\t"<<"Gender "<<endl;
	for(i=0;i<5;i++){
		if(a[i][1]=="F")
		cout<<"\t"<<a[i][0]<<"\t"<<"  "<<a[i][1]<<endl;
	}
	for(i=0;i<6;i++){
		if(b[i][1]=="F")
		cout<<"\t"<<b[i][0]<<"\t"<<"  "<<b[i][1]<<endl;
	}

	
	
	
	return 0;
}


Solved.
(See above)
Last edited on
Hello lrdbash,

Thank you for using code tags.

Consider this as a start to your program:
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
#include <iostream>
#include <string>  // <--- Added.

using namespace std;

int main()
{
    constexpr unsigned MAXROWS{ 5 }, MAXCOLS{ 2 };

    string a[MAXROWS][MAXCOLS];
    string b[MAXROWS][MAXCOLS];  // <--- Why is rows 6?
    //int row, col;

    cout << "Class 5A" << '\n';

    for (unsigned row{}; row < MAXROWS; row++)
    {
        for (unsigned col{}; col < MAXCOLS; col++)
        {
            //cout << "\na[" << row << "][" << col << "]= ";  // <--- May mean something to you, but looks bad to others. Useful for testing.

            std::cout << "\n Enter name: ";
            std::getline(std::cin, a[row][col++]);
            
            std::cout << " Enter sex (F/M): ";
            std::cin >> a[row][col];

            std::cin.ignore();
        }
    }

First you need to get the input for "a" and "b", Choose better names than a single letter, working befoe you move on. The for loops that print the arrays are motly fine.

Notice how I used "MAXROWS" and "MAXCOLS" in the for loops. This not only makes the program easier to work with it also makes it easier to change the number of rows if you need to.

I would also recommend making sure the "sex" is a capital letter before you leave the inner for loop.

Unless you need the loop iterator outside the for loop, which you do not, it is better to define the iterator's type in the for statement and keep it local to the for loop. When the for loop is finished the variable is destroyed.

When you get to part "C" you need the for loops to check both arrays for "F" not just 1 or create a new array to hold only the female names.

If you are just printing the names a new array is not needed.

Later, when I have coded it, I will show you a way to speed up testing.

Andy
Thanks for the help @Andy
Simply without functions/structs etc, consider:

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

int main()
{
	constexpr size_t MAXROWS {5}, MAXCOLS {2};

	std::string a[MAXROWS][MAXCOLS];
	std::string b[MAXROWS][MAXCOLS];
	std::string c[MAXROWS * 2];

	std::cout << "Class 5A" << '\n';

	for (size_t row {}; row < MAXROWS; ++row) {
		std::cout << "\nEnter name: ";
		std::getline(std::cin, a[row][0]);

		std::cout << "Enter sex (F/M): ";
		std::cin >> a[row][1];
		std::cin.ignore();
	}

	std::cout << "Class 5B" << '\n';

	for (size_t row {}; row < MAXROWS; ++row) {
		std::cout << "\nEnter name: ";
		std::getline(std::cin, b[row][0]);

		std::cout << "Enter sex (F/M): ";
		std::cin >> b[row][1];
		std::cin.ignore();
	}

	size_t cindx {};

	for (size_t r = 0; r < MAXROWS; ++r)
		if (a[r][1] == "F" || a[r][1] == "f")
			c[cindx++] = a[r][0];

	for (size_t r = 0; r < MAXROWS; ++r)
		if (b[r][1] == "F" || b[r][1] == "f")
			c[cindx++] = b[r][0];

	for (size_t i = 0; i < cindx; ++i)
		std::cout << c[i] << '\n';
}

Topic archived. No new replies allowed.