0

How can I improve code that would output this fixed-size shape to the console?

Shape:

#....
.#...
#.#..
.#.#.
#.#.#
.#.#.
#.#..
.#...
#....

I wrote the code, but it seems to me incorrect. Although he copes with this task. I want to combine all the elifs into an if, which is spelled out at the beginning. I think there is some kind of dependency between 10, 8, 6, 4 that will allow me to do this. I can't find this dependency to improve the code.

#include <iostream>
using namespace std;

int main() {
    for (int x = 1; x < 10; x++) {
        for (int y = 1; y < 6; y++) {
            if ((x == y) or (x == (10 - y))) cout << "#";
            else if ((x == (8 - y)) and (x > y)) cout << "#";
            else if ((x == (6 - y)) and (x > y)) cout << "#";
            else if ((x == (4 - y)) and (x > y)) cout << "#";
            else cout << ".";
        }
        cout << endl;
    }
    return 0;
}
ann_txg
  • 11
  • 2
  • 3
    Is there a purpose of doing it like that? If it's fixed size, just put it in a string. – Ted Lyngmo Nov 01 '22 at 22:49
  • 1
    Hint: What do 10, 8, 6 and 4 have in common? – tadman Nov 01 '22 at 22:50
  • @Ted Lyngmo I want to make the code more beautiful. I think we can do without the numbers 10, 8, 6, 4. There is some kind of connection between these numbers. I'm trying to figure out which one, it will make the code better. This is my purpose. – ann_txg Nov 01 '22 at 22:51
  • @tadman These are even numbers. I do not know how to apply it. – ann_txg Nov 01 '22 at 22:52
  • Hint: `!(y % 2)` – tadman Nov 01 '22 at 22:52
  • Re: _"more beautiful"_ - that's in the eye of the beholder. I think `std::cout << "#....\n.#...\n#.#..\n.#.#.\n#.#.#\n.#.#.\n#.#..\n.#...\n#....\n";` is pretty neat. – Ted Lyngmo Nov 01 '22 at 22:55
  • @tadman Should I replace 10, 8, 6, 2 to (y % 2) or what? I've tried but it didn't help me yet. – ann_txg Nov 01 '22 at 22:55
  • @TedLyngmo I agree. But it is a task where I can't write like this. – ann_txg Nov 01 '22 at 22:55
  • @tadman Could you tell more about this idea, please? – ann_txg Nov 01 '22 at 23:02
  • From what I see, `#` is set where two conditions hold: indices are of same parity and `y` (which is horizontal according to your notation) coordinate is less or equal then both diagonal (starting at top left going downwards) and antidiagonal (starting at bottom left going upwards) on current (`x`) row. – YurkoFlisk Nov 01 '22 at 23:12
  • @YurkoFlisk Yes, you are right. But how can I integrate this to my code? – ann_txg Nov 01 '22 at 23:18
  • @YurkoFlisk I understand how to write a condition for index evenness, but I don't understand how to write a condition for diagonals. – ann_txg Nov 01 '22 at 23:21
  • The top diagonal is `y == x` and bottom one is (assuming 1-based indexing as in your code) `y == 10 - x`. The condition that point `(x, y)` is to the left of both is trivial then. – YurkoFlisk Nov 01 '22 at 23:25
  • @YurkoFlisk Yes, it is the solution – ann_txg Nov 01 '22 at 23:31
  • Consider something more generic. You can parameterize the entire thing with a single value (width), and so it makes sense to write a function that can output any size, including configurable characters: https://godbolt.org/z/5xbbzKqj6 – paddy Nov 01 '22 at 23:40

1 Answers1

2

Employing a string "#.#.#....", all you have to do for each line is decide which part of this string to display.

#include <cmath>
#include <iostream>
#include <string>

int main() {
    const std::string S = "#.#.#....";
    for (int row = 0; row < 9; ++row) {
        std::cout << S.substr(std::abs(row - 4), 5) << '\n';
    }
}
Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
fana
  • 1,370
  • 2
  • 7
  • Now, I found my code was edited. The Editor say "made into a MRE", but I don't know what is the word "MRE" (Abbreviation for something?) I can understand adding include statements is good. Thanks. But I don't know about other modification such as spacing format. Is some recommended coding style defined in SO, and this modification is based on it? – fana Nov 04 '22 at 04:19
  • That's abbreviation for Minimal Reproducible Example. See on [help center](https://stackoverflow.com/help/minimal-reproducible-example). – YurkoFlisk Nov 05 '22 at 17:54