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

In summary: E[i][j]; } } return 0; }In summary, this program prints each letter of a name using an m x n array.
  • #1
Saladsamurai
3,020
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 . . .
 
Technology news on Phys.org
  • #2
Maybe you should first try to move the "cout<<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 ?
 
  • #3
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?
 
  • #4
Yes, you need to put a newline either just before or just after the second for loop.
 
  • #5
I put it after the 2nd loop; I am not sure that I see what putting it before the j loop would do?
 
  • #6
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;
 
  • #7
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 ? :uhh:

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:
  • #8
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

?
 
  • #9
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;
}
 
  • #10
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;
         
    }
 
  • #11
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 cout << B outputs the entire ith row.
 
  • #12
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.
 

What is a for loop?

A for loop is a control flow statement in programming that iterates a set of instructions for a specific number of times, based on a given condition. It is used to repeatedly execute a block of code until a certain condition is met.

Why is my for loop not working?

There could be several reasons why a for loop is not working, such as incorrect syntax, logical errors, or incorrect values for the loop's parameters. It is important to carefully check the code and make sure all the necessary conditions are being met for the loop to run correctly.

Can I use a for loop for different data types?

Yes, for loops can be used for different data types, such as numbers, strings, arrays, and objects. You can also use nested for loops to iterate through nested data structures.

How do I break out of a for loop?

You can use the "break" keyword to exit a for loop before it reaches its intended number of iterations. This can be useful if a certain condition is met and you want to stop the loop from executing further.

Can a for loop be used for infinite iterations?

Yes, a for loop can be used for infinite iterations by omitting the condition or using "true" as the condition. However, it is important to include a break statement to avoid an infinite loop that can cause the program to crash.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
3
Views
727
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
3
Replies
89
Views
4K
Back
Top