I’m not even going to try to solve the problem in this code because it’s too confusing and maybe the problem was born out of confusion. The simpler you can do it, the better. Even if you want to learn a specific concept from this type of code, it is best to learn in a case where it would be necessary. If someone asked to do so, the person did a disservice to you. It would be a useless and unproductive requirement. and I would argue that this form is disadvantageous.
Do not declare variables outside the function without need. Always state as close as possible to where it will be used. Some people don’t see the value of this, but it creates a greater cognitive capacity to do simpler things. If you only need the variable inside the loop, create it inside the loop.
If you have two vectors to consider, create two loops, it’s the natural way to do this. This is the structured form to make this code. The original knots in the head. Including a loop for
is much more natural to the case.
There was a column variable of type char
. Or make the line char
and limits the number of asterisks to 127, int
in both. I left int
because there is no real gain in using char
there, even if only want low values, which in fact is not even validated, because the scanf()
is not very suitable for this. Be consistent.
I gave an organized one. It is very difficult to understand what the code does in written form.
Once this is done, the code becomes too simple.
#include<stdio.h>
int main(void) {
printf("TELL ME THE NUMBER YOU WANT: \n");
int n;
scanf("%d", &n);
for (int row = 0; row < n; row++) {
for (int column = 0; column < n; column++) printf("*");
printf("\n");
}
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Man, thank you so much! I am a beginner and am learning on my own, your advice will help me a lot... I myself realized that " aesthetically" was strange what I had done, but I could not do otherwise, unfortunately...
– Vitor Matos