I have two code, 1st is working and 2nd is not, where I'm wrong

1st code:

#include
using namespace std;

int main() {
string letters[2][4] = {
{ “A”, “B”, “C”, “D” },
{ “E”, “F”, “G”, “H” }
};

cout << letters[0][2];
return 0;
}

2nd code:

#include
#include
using namespace std;
int main(){
string letters[2][4][2]={
{ {
{“A”,“B”,},
{“C”,“D”,}
},

  {
    {"E","F",},
    {"G","H",}
  }
 },
 
 {
 {
    {"I","J",},
    {"K","L",},
 },  
 
  { {"M","N",},
    {"O","P",},
 }
 },   
};





 cout<<letters[0][0];
 return 0;

}

Hi, for the second one, it looks like how you have formed your arrays and sizes are not right… the sub array should have a size of four. Also the way you are indexing into letters in your cout is not right there, you are missing an index because this is a 3d array. Try the below code:

#include <string>
#include <iostream>
using namespace std;
int main(){
string letters[2][4][2]={
  { 
    {"A", "B",}, 
    {"C", "D",},
    {"E","F",},
    {"G","H",}
  },
  
  {
    {"I","J",},
    {"K","L",},
    {"M","N",},
    {"O","P",}
  },   
};

 cout<<letters[0][0][0];
 return 0;
}
1 Like

thank you friend for solving my problem