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
Click For Summary

Discussion Overview

The discussion revolves around creating a C++ program to print letters using nested loops and arrays, specifically focusing on the letters "B" and "E". Participants explore how to format the output so that the letters appear next to each other rather than on separate lines. The conversation includes technical challenges, code snippets, and suggestions for structuring the program.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares a code snippet for printing the letter "B" using a 2D array and expresses confusion about the output format.
  • Another participant suggests moving the "cout << endl;" statement to improve the output formatting.
  • There is a discussion about the significance of the 13th column being a null character in C-strings.
  • Participants propose using additional loops to print multiple letters horizontally and discuss the need for a new index for each letter.
  • One participant suggests creating a larger alphabet table to manage multiple letters and their representations.
  • Another participant expresses confusion about how to print letters next to each other and seeks clarification on using indices for multiple arrays.
  • One participant realizes that using a single index allows for printing entire rows of characters, simplifying the code.
  • There is a sentiment of frustration regarding the requirement to use for loops, with one participant questioning the educational value of the exercise.

Areas of Agreement / Disagreement

Participants express varying levels of understanding regarding the use of indices and loops. While some agree on the need for structured loops to print letters, others are uncertain about the best approach to achieve the desired output format. The discussion remains unresolved regarding the optimal method for printing letters horizontally.

Contextual Notes

Participants mention the use of C-strings and the implications of null characters in array declarations. There is also a suggestion to create a comprehensive alphabet table, but the details of its implementation are not fully explored.

Who May Find This Useful

This discussion may be useful for C++ learners interested in array manipulation, string handling, and formatting output in console applications.

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 . . .
 
Technology 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;
}
 
  • #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 count << 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.
 

Similar threads

Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
73
Views
6K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
2K