### 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?

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` `````` using namespace std; int main(){ string a; string b; int i,j; cout<<"Input Class 5A Student Name and Gender(M/F)"<

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

Thank you for using code tags.

Consider this as a start to your program:
 ``123456789101112131415161718192021222324252627282930`` ``````#include #include // <--- 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:

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546`` ``````#include #include 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]); std::cout << "Enter sex (F/M): "; std::cin >> a[row]; 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]); std::cout << "Enter sex (F/M): "; std::cin >> b[row]; std::cin.ignore(); } size_t cindx {}; for (size_t r = 0; r < MAXROWS; ++r) if (a[r] == "F" || a[r] == "f") c[cindx++] = a[r]; for (size_t r = 0; r < MAXROWS; ++r) if (b[r] == "F" || b[r] == "f") c[cindx++] = b[r]; for (size_t i = 0; i < cindx; ++i) std::cout << c[i] << '\n'; }``````

Topic archived. No new replies allowed.