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
Related
I need to print centered string inside a frame of square stars pattern.
EXAMPLE:
const char tekst[]="This is example of string programming in C";
int width = 20;
OUTPUT:
********************
* This is example *
* of string progr- *
* amming in C *
********************
If number of spaces that need to be added is odd, excess space should be added to the right.
If the whole word cannot fit in the row, dash is added and word continues in the next row.
Auxiliary strings are not allowed.
Code:
#include <stdio.h>
#include <string.h>
void framed(const char *tekst, int width) {
int i, j = 0, count = 0;
for (i = 0; i < width; i++)
printf("*");
printf("\n");
while (tekst[j] != '\0') {
count++;
if (count == 1)
printf("* ");
printf("%c", tekst[j]);
j++;
if (count + 5 > width) {
printf(" *");
printf("\n");
count = 0;
}
if (j == strlen(tekst))
for (i = 0; i < width - count; i++)
printf(" ");
}
printf("*\n");
for (i = 0; i < width; i++)
printf("*");
}
int main() {
const char tekst[] = "This is example of string programming in C";
int width = 20;
framed(tekst, width);
return 0;
}
This is my output:
********************
* This is example *
* of string progra *
* mming in C *
********************
Could you help me to fix my code for correct output?
Here you take the additional character "* "" *" into account (with a little off-by-one because of >):
if (count + 5 > width)
Here you don't:
for (i = 0; i < width - count; i++)
If you do take them into account, it (the frame) works.
for (i = 0; i <= width - (count +5); i++)
assuming that you also output consistently a little later
printf(" *\n");
I recommend to replace the very magic number 5 with the less magic number of additional characters; 4 for "* *" and to adapt the conditions <= and >.
The other differences to required output (centering instead of left aligning and inserting the "-" correctly have not even been attempted by the shown code, so I assume that they are not part of what is asked about.
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");
}
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');
}
Hey I'm working on the CS50 more comfortable problem and I can't figure out how to print the second mario pyramid on the same line. In my code it already prints out, but it's not on the same line.
It doesn't matter if you guide me or show me how to do it. I'm using CS50 as a practice, I'm not turning anything in. So this wouldn't be cheating.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int height = 0;
// left pyramid variables
int i = 0;
int j = 0;
int k = 0;
// variable for gap
int g = 0;
// right pyramid variables
int l = 0;
int m = 0;
int n = 0;
//do - while loop -- works
do
{
printf("Height: ");
scanf("%d", &height);
}
while (height < 0 || height > 23);
// Print pyramids
// print spaces for left pyramid (less spaces needed with time) ✓
// print hashes for left pyramid ✓
// print gap (2)
// print hashes for right pyramid
// print new line - for next row
// Left Pyramid
// Rows -- determines the height
for (i = 0; i < height; i++)
{
// Cols -- in this one we are doing the spaces
// the -i makes it left aligned -- to make it right aligned remove the "-1"
for (j = 0; j < height-i; j++)
{
// Printing Spaces
printf(" ");
}
// "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one
// whenever the height increases, so that's why we add + 1 to it
// if I don't add 1 to it what it does is that prints a new line, and then it prints
// 4 things instead of 5 for example.
for (k = 0; k < i + 1; k++)
{
printf("#");
}
// Print new line
printf("\n");
}
// Gap -- fix gap, the rest works how it should -- I think I need to make everything
// inside one loop
// for (g = 0; g < height; g++)
// {
// printf(" ");
// }
// Right Pyramid
// Rows -- determines the height
for (l = 0; l < height; l++)
{
// Cols -- in this one we are doing the spaces
// right aligned
for (m = 0; m < height; m++)
{
// Printing Spaces
printf(" ");
}
// "i+1" - we want i to be 1 whenever height is 0, and we want i to increase by one
// whenever the height increases, so that's why we add + 1 to it
// if I don't add 1 to it what it does is that prints a new line, and then it prints
// 4 things instead of 5 for example.
for (n = 0; n < l + 1; n++)
{
printf("#");
}
// Print new line
printf("\n");
}
return 0;
}
Why not just do something like into the first loop:
for (k = 0; k < i + 1; k++)
{
printf("#");
}
/* this is new */
/* Draw the gap */
for (k = 0; k < gap; k++) {
printf(" ");
}
/* Draw the left part*/
for (k = 0; k < i + 1; k++)
{
printf("#");
}
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