Need to do a half of a christmas tree with "*" - c

The output desired from the half of Christmas tree is:
*
**
***
****
*****
I could get a output like this:
*
**
***
****
*****
By using only cycles and conditions (arrays, can't be used), how can I get a solution like the first one?
main()
{
int n;
printf("Introduza o nĂºmero de ramos: ");
scanf_s("%d", &n);
for (int i = 1; i <= n; i++)
{
for (int j = 1 ; j <= i; j++)
{
putchar('*');
}
putchar('\n');
}
}

You obviously understand how to put a number of same characters next to each other.
The only thing left to do for you is to notice that the only difference between the first half-tree and the second is some spaces in front of the stars.
Also notice that the number of spaces is quite predictable, given the number of stars: the total width of spaces and stars is constant.

You just need to give a condition in inner loop. you should run the inner loop from 1 to n and print a " "(space) if j is less then i else print '*'
for (int i = 1; i <= n; i++)
{
for (int j = 1 ; j <= n; j++)
{
if(j < i)
putchar(' ');
else
putchar('*');
}
putchar('\n');
}

Related

Error printing a pyramid pattern with asterisks in C [duplicate]

This question already has answers here:
What is a null statement in C?
(6 answers)
Closed 2 years ago.
The program asks the user for the pyramid hight i.e: rows number, and prints out a pyramid.
for input of 5, the result should look like this:
*
***
*****
*******
*********
the code I wrote is:
#include <stdio.h>
void main()
{
int i, j, n = 0;
printf("enter pyramid hight: \t");
scanf("%d", &n);
// FOR EACH ROW:
for (i = 0; i < n; i++)
{
// print spaces till middle:
for (j = 0; j < (n - 1 - i); j++)
{
printf(" ");
}
// print stars:
for (j = 0; j < 2 * n + 1; j++)
;
{
printf("*");
// go to new row
printf("\n");
}
}
}
but the result is:
*
*
*
*
*
what could be going wrong exactly, I think the second loop may be the problem but I can't put my hand on the reason.
You got a logic errors for the second for loop which does not do what you want:
// print stars:
for (j = 0; j < 2 * n + 1; j++) //<-- error #1 - n should be i as it's specific for this row
; //<-- error #2 - this termination is completely wrong, it means this `for` loop doing nothing
{
printf("*");
// go to new row
printf("\n");
}
which should be:
// print stars:
for (j = 0; j < 2 * i + 1; j++)
{
printf("*");
}
// go to new row
printf("\n");

CS50 mario pyramid upside down can't figure why

I thought long and hard before asking this in here but I've spent too much time now trying to figure this one out without cheating.
The CS50 mario ps1 (less comfortable) asks for a *simple left align (at first) pyramid, but my code is giving me it upside down and I can't figure why.
#include <cs50.h>
#include <stdio.h>
int main (void)
{
int n;
do
{
n = get_int("Pyramid Height: ");
}
while (n < 1 || n > 8);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n - i ; j++)
printf("#");
for (int j = 0; j < n - i; j++)
{
printf(" ");
}
printf("\n");
}
}
I'm sorry if this type of questioning shows up regularly here but I really do need your help.
Thanks in advance.
edit:
expected result:
........#
.......##
......###
.....####
....#####
...######
..#######
.########
I can change the dots to spaces afterwards, this is just for visualisation;
the restriction for height is 8, so I guess that each line has always eight characters;
I actually added trailing spaces so that the pyramid could be right aligned, I've metioned wrong before;
I'm going to check the How to debug small programs?;
Sorry, I'm new to this, I didn't know there was a difference between here and stack exchange, gonna look into that.
*Sorry for the "meh" english, it is not my native language.
See what is the difference between my and your code (especially how to count):
void draw(int n, int align, int dir)
{
for (int i = 1; i <= n; i++)
{
if(align)
{
for(int s = 0; s < (dir ? (n - i) : i - 1); s++)
{
printf(" ");
}
}
for (int j = 0; j < (dir ? i : (n - i + 1)) ; j++)
{
printf("#");
}
printf("\n");
}
}
int main (void)
{
draw(8,1,0);
printf("\n");
draw(8,1,1);
printf("\n");
draw(8,0,0);
printf("\n");
draw(8,0,1);
}
https://godbolt.org/z/7YT16j
my code is giving me it upside down and I can't figure why
Let's see what the code looks like
// There's a loop executed n times. The body prints a line, so n lines are printed.
// In case you have doubts, the characters are normally printed top to bottom and
// left to right.
for (int i = 0; i < n; i++)
{
// The following loop prints (n - i) characters '#' at the beginning
// of each line. That's NOT what you are supposed to do, kind the opposite.
for (int j = 0; j < n - i ; j++)
printf("#");
// You should first print the spaces, then the '#'s, starting from 1 '#' at
// the first line and increasing the number by one at each line (so you have
// to change the condition in the loop accordingly).
// This loop prints the right amount of spaces, but only after
// all the '#'s and just before the end of the line, so that you just
// can't see them (change the printed char to '.' to visualize those).
for (int j = 0; j < n - i; j++)
{
printf(" ");
}
// Note that you could use putchar('\n'), here and previously, to print
// only one char, instead of using printf() to print string literals.
printf("\n");
}

VSS C coding to print out a Christmas tree

This is my current code
void printer(int input)
{
int star;
int space;
for (int i = 1; i <= input; i++)
{
star = i;
space = i + input - star - 1;
for (int j = 0; j < space; j++)
{
printf(" ");
}
for (int s = 0; s < star; s++)
{
printf("*");
}
printf("\n");
}
}
I'm trying to print it out like
How tall 3?
*
* *
* * *
_| |_
\___/
and this is what i get. What is wrong with my code? and how will I fix the code?
How Tall? 3
*
**
***
Essentially, this snippet of code here that you use:
star = i;
space = i + input - star - 1; // or, space = input - 1 + i - star;
is the same as writing:
star = i;
space = input - 1;
This is because i and star have the same value (quoth star = i), and therefore, negate each other.
Now, can you see a constant here? Yes, the value of input is never changed anywhere, and therefore your code is always what 1 less than the input is. (In this case, its always preceded by 2 (=3-1) spaces) like this:
*
**
***
^^ mark two spaces
And you also forgot to add a trailing space after the asterix and hence there is no spacing between them.
Therefore, to solve your problem, you could store the value of input temporarily and reduce it by 1 in every iteration so it appears like a pyramid.
Example:
void printer(int input)
{
int star;
int space;
// Store the original length of the space
int space_length = input;
for (int i = 1; i <= input; i++)
{
star = i;
// Get the number of spaces for the current iteration
space = space_length - 1;
for (int j = 0; j < space; j++) {
printf(" ");
}
for (int s = 0; s < star; s++) {
printf("* ");
// ^ note this space after the asterisk
}
// Decrease the length of the space every step
// So that it appears like a slope
// Note how we are using 'space_length' instead of input
// This is because if we decrement 'input', this loop
// will get affected, which is not what we want
space_length--;
printf("\n");
}
///////// bottom part of the tree /////////
// number of spaces needed = input - length of "_| |_" - 1
for (int i = 1; i <= input - (4 - 1); i++)
printf(" ");
printf("_| |_\n");
// number of spaces needed = input - length of "\\___/" - 1
for (int i = 1; i <= input - (4 - 1); i++)
printf(" ");
printf("\\___/\n");
}
which gives the output:
*
* *
* * *
_| |_
\___/
your format is the issue. as you already know the size of the tree, you can deduct the size of the first set of whitespace (the first spaces on each lines), being spaces = (size - 1) - i. it should be, for each i:
i1 = 2
i2 = 1
i3 = 0
which seems to be what the result gives. additionally, between all asterix, just insert one space until the last character, and that's the idea
There are too many things gone wrong with this. I'll not post the correct code, but can guide you through it.
Since you want space after stars, print "* " instead of "*"
You have to use decremental space in beginning, before printing star, there's some logical fault in that, which is, you are adding and subtracting i and star, which are same thing. You need to give (input - star) spaces for the loop.
For the trunk, you have to develop some symmetrical logic.
I hope it helps you.
You can do it like this:
#include <stdio.h>
void printer(int input)
{
// for stars
for (int i = 1; i <= input; i++) {
for (int j = 1; j <= input - i; j++) {
printf(" ");
}
for (int k = 1; k <= i; k++) {
if (k == 1) printf("*");
else printf(" *");
}
printf("\n");
}
// for base
int base_half_len = input - 1;
for (int i = 1; i < base_half_len; i++) {
printf("_");
}
printf("| |");
for (int i = 1; i < base_half_len; i++) {
printf("_");
}
printf("\n\\");
for (int i = 1; i < 2 * input - 2; i++) {
printf("_");
}
printf("/\n");
}
int main(void) {
// your code goes here
printer(10);
return 0;
}
Here's the link where I tested it: https://ideone.com/7zpNeW

Why does the code give me wrong triangle?

Make a triangle
*
**
***
****
*****
******
*******
********
*********
**********
***********
************
int rows = 12, i = 1, j = 1;
while(i <= rows)
{
while(j <= i)
{
printf("*");
j++;
}
printf("\n");
i++;
j=1;
I try to make a triangle like
********
*******
******
*****
****
***
**
*
but i am getting wrong.
You have problem with
while(j <= i)
loop. Here is my solution:
int rows = 12, i = 1, j = 1;
while (i <= rows)
{
while (j <= (rows - i +1))
{
printf("*");
j++;
}
printf("\n");
i++;
j = 1;
}
Basically you have to reverse your loops. So for each loop in your code you're increasing the number of instances '*' is printed for each iteration of i. This is because i and j start at 1 and are increased until the number of iterations equals rows.
int rows = 12, i = 1, j = 1;
while (i <= rows)
{
while (j <= (rows - i +1))
{}
}
What you want is to start from 12, and decrement each time until you hit 0. So to start with you can loose the variable rows, and set i and j equal to 12. In the outer while() loop you want to decrement i and reset j = i each time the inner loop closes. The inner loop should print * and decrement j. Below is my solutions, but Loc Tran's answer works just as well.
int main(void){
int i = 12, j = 12;
while(i >=0)
{
while(j >= 0)
{
printf("*");
j--;
}
printf("\n");
i--;
j=i;
}
}
If this is for a school assignment (I had one quite similar in a 1st year course) I suggest you try understand why your code is different to the provided solutions.

How do I print out this 2D array?

Novice programmer here trying to print out a 2D array of asterisks. The output I'm trying to achieve is:
*******
*******
*******
*******
That's 7 asterisks with a leading space increment by 1 every row with row 1 starting with 0 leading spaces and row 4 starting with 3 leading spaces.
Only the <stdio.h> library can be used and I'd like to keep using nested for loops. Here's my attempt:
#include <stdio.h>
int main(void)
{
int i, j;
char star[1] = {'*'};
for(i = 0; i < 4; i++)
{
for(j = 0; j < 8; j++)
{
printf("%*c", i, star[0]);
}
printf("\n");
}
return 0;
}
Here's what I'm getting:
********
********
* * * * * * * *
* * * * * * * *
I'd also like for this question to be an opportunity for me to learn good general programming practices and habits from this community so if you could critique my implementation, I'd greatly appreciate it.
Firstly, this does not really have anything to do with multidemensional arrays. Secondly you are using the width format specifier in your printf call
printf("%*c", i, star[0]);
and setting that width to be i. Therefore each time you call
printf("%*c", i, star[0]);
you are specifying the minimum width of the printed output to be i characters. As your output is a single * character this results in whitespace padding.
Thus in your final iteration of your i based loop (i = 3) you are causing the string
' *'
to be printed each time, penultimate iteration (i = 2)
' *'
and so on.
As you don't really need a char array of length 1, you could get your desired output with
for(i = 0; i < 4; i++)
{
if (i > 0)
{
printf("%*c",i,' ');
}
for(j = 0; j < 8; j++)
{
printf("%c", '*');
}
printf("\n");
}
The program you wrote prints space before every star, and that's space length is tied to the 'i' variable'.
You should make it print (spaces * i) before the loop that prints line of 8 stars even starts.
Here is how i would write it.
#include
int main() {
for(int i = 0; i < 4; i++ ) {
for( int p = 0; p < i; p++ ) {
printf(" ");
}
for( int j = 0; j < 8; j++ ) {
printf("*");
}
printf("\n");
}
return 0;
}
Here it is in Python, if you're interested:
for x in range(4):
print ((x * " ") + ("*" * 7))
Apply this same idea of padding the beginning with spaces based on the iteration, and then adding the asterisks

Resources