Create B-shaped Array Using Nested Loops | C++ Program

  • Context: C/C++ 
  • Thread starter Thread starter Saladsamurai
  • Start date Start date
  • Tags Tags
    Loop Stupid
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
11 replies · 3K views
Saladsamurai
Messages
3,009
Reaction score
7
Okay, so the object of this program is to write my name using an m x n array so that it prints each letter made up of that letter. That is, the first letter is B and thus is made up of a bunch of B's. Here is my code...I do not know what the problem is?

Code:
#include <iostream>
#include <cstring>
using namespace std;

int main ()

{
    int i,j;
    char B[7][13]={
         {"BBBBBBBBBBBB"},
         {"BB    BBBBBB"},
         {"BB   BBB    "},
         {"BB BB       "},
         {"BB  BBBB    "},
         {"BB     BBBBB"},
         {"BBBBBBBBB   "}};
         
         for (i=0;i<7;i++)
         {
             for (j=0;j<12;j++)
             {
                 cout << B[i][j];
             }
         }
         
         cout << endl;
         system ("PAUSE");
         return 0;
         
    }

this is the output it is giving me
Code:
BBBBBBBBBBBBBB    BBBBBBBB   BBB    BB BB       BB  BBBB    BB     BBBBBBBBBBBBB
B
Press any key to continue . . .
 
Physics news on Phys.org
Maybe you should first try to move the "count<<endl;" inside the "for(i)" loop (I guess eventually you will need a "h" loop on the letters of your name). Also, is it correct to declare 13 elements and then put in only 12 ? Will this not mess up ?
 
The 13th column is the "null character" so yes.

And yes..the moving of endl; worked...I think I see why now! Thanks!

What will the "h" loop be for? The next letter? Wouldn't I need to use two indices for each letter since I want them to print horizontally?
 
Yes, you need to put a newline either just before or just after the second for loop.
 
I put it after the 2nd loop; I am not sure that I see what putting it before the j loop would do?
 
How can I get the E to print NEXT to the B?? Supposedly it can be done with for loops...
Code:
#include <iostream>
#include <cstring>
using namespace std;

int main ()

{
    int i,j;
    char B[7][13]={
         {"BBBBBBBBBBBB"},
         {"BB    BBBBBB"},
         {"BB   BBB    "},
         {"BB BB       "},
         {"BB  BBBB    "},
         {"BB     BBBBB"},
         {"BBBBBBBBB   "}};

     char E[7][13]={
         {"EEEEEEEEEEEE"},
         {"EE          "},
         {"EE          "},
         {"EEEEEEEEE   "},
         {"EEEEEEEEE   "},
         {"EE          "},
         {"EEEEEEEEEEEE"}};
         
         
         
         for (i=0;i<7;i++)
         {
             for (j=0;j<12;j++)
             {
                 cout << B[i][j];
             }
              cout << endl;
         }
         
         
         
         
         
         system ("PAUSE");
         return 0;
 
Saladsamurai said:
The 13th column is the "null character" so yes.
Ah sure, I have not used C-strings a lot. Thanks.
Saladsamurai said:
What will the "h" loop be for? The next letter? Wouldn't I need to use two indices for each letter since I want them to print horizontally?
I was thinking to make other tables for other letters, use the same indices for rows and columns, and a new index for the tables. That would in the end amount to one 3-indices table. It's only relevant if you want to re-use the letters. Eventually, you have one big alphabet table with 3 indices, and another table containing the index for each letter in your sentence. Do I make sense ? :rolleyes:

Code:
char alphabet[26][7][13] = {
{
{"     A    "} 
{ ...}
...
}{
{"     B    "}
...
}{
...
}{
{"   Z   "}
...
}
};
and for the printing of the sentence "BOB", one table
Code:
int bob[3] = {1,12,1}
It would also help for the handling of blank, null, coma... Well, that's ASCII :approve:
 
Last edited:
I kind of follow you. But I am still lost on the h index. Using my last post, I have two arrays. One named B and one named E. How would I call them such that I can count

BE

to the screen? as opposed to

B
E

?
 
Saladsamurai said:
I kind of follow you. But I am still lost on the h index. Using my last post, I have two arrays. One named B and one named E. How would I call them such that I can cout

BE

to the screen? as opposed to

B
E

?
You need to do first the loop on the first row of B (the first letter), then the loop on the second row of E (the second letter),..., then the second row of the first letter, the second row of the second letter,...,...,..., then the last row of the first letter, then the last row of the second letter,...then the last row of the last letter. So either you have an explicit list of all letters with each letter has its own for loop for all chars in the row, or you need a big table. For the sentence "BOB"
Code:
char alphabet[letter][row][column] = {{{AA..}{AA..}...}{{BB..}{BB..}...}...};
char fixed_space[5] = "     ";
int sentence[3] = {1,11,1};
for(all rows){
  for(all letters){
    for(all chars){
      cout << alphabet[sentence[letter]][row][char];
    }
    cout << fixed_space;
  }
  cout << endl;
}
 
Somehow this works... why don't I need two indices now? Why does this even work?
This is stupid.

Code:
#include <iostream>
using namespace std;

int main ()

{
    int i,j;
    char B[7][13]={
         {"BBBBBBBBBBBB"},
         {"BB    BBBBBB"},
         {"BB   BBB    "},
         {"BB BB       "},
         {"BB  BBBB    "},
         {"BB     BBBBB"},
         {"BBBBBBBBB   "}};
    
    char E[7][13]={
         {"EEEEEEEEEEEE"},
         {"EE          "},
         {"EE          "},
         {"EEEEEEEEE   "},
         {"EEEEEEEEE   "},
         {"EE          "},
         {"EEEEEEEEEEEE"}};
         
              
         for (i=0;i<7;i++)
         {
    
            cout << B[i] << '\t'<<E[i];
             
            cout << endl;
         }
         
         
         
         
         
         
         
         
         
         system ("PAUSE");
         return 0;
         
    }
 
Saladsamurai said:
Somehow this works... why don't I need two indices now?
That's great ! :biggrin:
That's why I love coding.
I think B is interpreted as a string of chars, so count << B outputs the entire ith row.
 
Actually.. it occurs to me that I do not even need ... All I am doing is outputting each entire array with a tab in between.

But my prof wants it done with for loops. This is so stupid. This teaches me nothing useful.