-1

Hello and thank you for coming here.

I have to do a program that will draw a number of square choosed by the user with incremental letter. For example, if the user choose 4 square, it will return :

DDDDDDD

DCCCCCD

DCBBBCD

DCBABCD

DCBBBCD

DCCCCCD

DDDDDDD

At the time being, my code look like this ;


#include <iostream>
using namespace std;

int main()
{
 int size;
 int nbsquareletter;
     cout << " How many square ?" << endl;
      cin >> nbsquareletter;
      size = nbsquareletter * 2 - 1;
 char squareletter = 'a';
     for (int row = 1; row <= size; ++row)
      {
          for (int col = 0; col <= size; ++col)
          {

            if (row < col) {
              cout << (char)(squareletter + row - 1) << " ";
            }
            else if (row > col)
            {
              cout << (char)(squareletter + col) << " ";
            }

              /*
              cout << col << " ";
              cout << row << " ";
              */




          }
          cout << endl;
      }
  }


If you have any ideas to help me, don't hesitate, I'm struggling. it's been 3.5 hours. Thanks you for reading and have a good day !

Chris
  • 25
  • 5
  • You'll be glad to hear you don't need anyone's help to figure this out, just a tool you already have: your debugger! This is exactly what a debugger is for. It [runs your program, one line at a time, and shows you what's happening](https://stackoverflow.com/questions/25385173/), this is something that's every C++ developer must know how to do. With your debugger's help you'll able to quickly find all problems in this and all future programs you write, without having to ask anyone for help. Have you tried using your debugger, already? If not, why not? What did your debugger show you? – Sam Varshavchik Oct 19 '22 at 12:38
  • Use a debuggger [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). You can use it [online](https://onlinegdb.com/ZtpvHWvai). – Jason Oct 19 '22 at 12:38
  • I already used it, but i lack the logic behind how to do this. I know this code isn't working and will not do what i want. I just don't know how to do it, i really lack the logic behind the way to do this – Chris Oct 19 '22 at 12:40
  • @Chris Then maybe https://math.stackexchange.com/ – Jason Oct 19 '22 at 12:41
  • 1
    @Chris *but i lack the logic behind how to do this* -- Then you shouldn't have written a single line of code. You only write code once you have a plan down on paper as to how to solve the problem. Once you have that plan, *then* you write the program to follow the plan. You don't write code you know will not work, and then try to figure out from the code how to get it to work. Programming doesn't work that way. – PaulMcKenzie Oct 19 '22 at 12:44
  • [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Oct 19 '22 at 12:45
  • 1
    FWIW, 3.5 hours isn't a lot of time. Using my former university's guidelines, a 15 credit-hour semester is the equivalent of a 45-60 hour work week. My Intro course was a 4 credit course. That's 12-16 hours of expected work per week. We subtract 4 hours to account for lecture and lab and you still have 8-12 hours of study outside of class that's expected. We also had a Calc I pre-req. – sweenish Oct 19 '22 at 12:51
  • The symmetry has me leaning toward a solution involving absolute numbers. What we did for a similar exercise was draw stuff out and play with numbers to identify a pattern. Once we had the pattern, dropping it into code is trivial. – sweenish Oct 19 '22 at 12:56

3 Answers3

2

Try to keep things simple. If you start write code before you have a clear idea of how to solve it you will end up with convoluted code. It will have bugs and fixing them will make the code even less simple.

Some simple considerartions:

  • The letter at position (i,j) is determined by the "distance" from the center. The distance is max(abs(i - i_0), abs(j - j_0).

  • The center is at (i,j) = (size-1,size-1) when we start to count at upper left corner (0,0).

  • The letters can be picked from an array std::string letters = "ABCDEFG...".

  • i and j are in the range [0,2*size-1)

Just writing this (and nothing more) down in code results in this:

#include <iostream>
#include <string>

void print_square(int size){
    std::string letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int i_0 = size-1;
    int j_0 = size-1;
    for (int i=0;i< 2*size-1;++i){
        for (int j=0;j< 2*size-1;++j) {
            int index = std::max(std::abs(i-i_0),std::abs(j-j_0));
            std::cout << letters[index]; 
        }
        std::cout << "\n";
    }
}


int main() {
    print_square(4);
}

Which produces output

DDDDDDD
DCCCCCD
DCBBBCD
DCBABCD
DCBBBCD
DCCCCCD
DDDDDDD

Your code cannot print the right output, because when row == col there is no output, and it misses the diagonal. I didnt look further than that.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1

Instead of fixing bugs in your code I decided to suggest you my own solution. Maybe some other answers will be related to bugs in your code.

On piece of paper I figured out 4 different formulas for 4 parts of a drawn picture, formulas computing what letter to take inside English alphabet. Afterwards I take this letter from array with alphabet.

Try it online!

#include <iostream>

int main() {
    int n = 0;
    std::cin >> n;
    char const letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    for (int i = 0; i < 2 * n - 1; ++i) {
        for (int j = 0; j < 2 * n - 1; ++j)
            std::cout << letters[
                i <= j && i + j <  2 * n - 1 ? n - i - 1 :
                i <= j && i + j >= 2 * n - 1 ? j - n + 1 :
                i >  j && i + j <  2 * n - 1 ? n - j - 1 :
                i >  j && i + j >= 2 * n - 1 ? i - n + 1 : 25
            ];
        std::cout << std::endl;
    }
}

Input:

7

Output:

GGGGGGGGGGGGG
GFFFFFFFFFFFG
GFEEEEEEEEEFG
GFEDDDDDDDEFG
GFEDCCCCCDEFG
GFEDCBBBCDEFG
GFEDCBABCDEFG
GFEDCBBBCDEFG
GFEDCCCCCDEFG
GFEDDDDDDDEFG
GFEEEEEEEEEFG
GFFFFFFFFFFFG
GGGGGGGGGGGGG
Arty
  • 14,883
  • 6
  • 36
  • 69
0

Let's define some functions, to make the recurrence relation obvious.

std::vector<std::string> WrapInLetter(char letter, std::vector<std::string> lines)
{
    for (auto & line : lines)
    {
        line.insert(line.begin(), letter);
        line.insert(line.end(), letter);
    }

    std::size_t size = (lines.size() * 2) + 1;
    std::string edge(size, letter); // A string formed of size copies of letter
    lines.insert(lines.begin(), edge);
    lines.insert(lines.end(), edge);

    return lines;
}

std::vector<std::string> SquareLetter(char letter)
{
    if (letter == 'A') return { "A" };
    return WrapInLetter(letter, SquareLetter(letter - 1));
}

Now main just has to call that function and loop over the result.

int main()
{
    std::cout << "How many square ?" << std::endl;
    int size;
    std::cin >> size;
    for (auto line : SquareLetter('A' + size - 1))
    {
        std::cout << line << std::endl;
    }
}
Caleth
  • 52,200
  • 2
  • 44
  • 75