I am stuck at this pattern. Please help.
I am stuck at this pattern. Please help.
You need to show us your attempt to solve the problem, by posting the code in code blocks, then we can help you. We cannot do the work for you. You can only learn to program in C by actually writing code yourself.
The same is true with anything you want to learn.
I have written this code for 1 no. pyramid which is working fine.. I just can't seem to figure out how to repeat it horizontally.
Code:#include<stdio.h> int main() { int i, j, n; printf("Enter No. of Rows : "); scanf("%d", &n); for (i = 1; i <= n; i++) { for (j = 1; j <= n; j++) { if (i + j <= n) { printf(" "); } else { printf("* "); } } printf("\n"); } return 0; }
Last edited by Salem; 09-08-2022 at 11:40 AM. Reason: removed crayola
The first step is to make it output the same number of trailing spaces as leading spaces.
So that the pattern is essentially rectangular in shape.
....*....
...* *...
..* * *..
.* * * *.
* * * * *
Once you can do that, all you need to do is repeat the printing of each row.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Ok. I modified the code to print in rectangular shape. But i don't know how to repeat it row wise. Please help on that. Here is the modified code:-
Code:#include<stdio.h> int main() { int n; printf("Enter No. of Rows : "); scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { (i + j <= n) ? printf(" ") : printf(" *"); } printf("\n"); } return 0; }
No it doesn't.
All you did was push everything along one character.Code:#include<stdio.h> int main() { int n; printf("Enter No. of Rows : "); scanf("%d", &n); for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { (i + j <= n) ? printf(".") : printf(" *"); } printf("\n"); } return 0; } $ gcc bar.c $ ./a.out Enter No. of Rows : 5 .... * ... * * .. * * * . * * * * * * * * *
You need to print the same number of trailing spaces as leading spaces on each row, so that the total number of characters on each row is the same.
Then you can repeat printing each row to end up with the same shape printed several times.
FWIW, your super compressed logic isn't doing you any favours.
With that, you can write three simple loops to outputCode:for (int i = 1; i <= n; i++) { int nSpaces = n - i; int nStars = i;
- the leading spaces
- the stars
- the trailing spaces.
One more thing, when you paste code, use "paste as text" and/or "copy as text".
Your IDE brings all the html markup with it and makes a mess when pasted here.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Your lucky day.
Code:/* main.cpp, Draw Pyramid, Created by Great on 9/10/22. */ #include <stdio.h> #pragma mark - Interface int add_array(char *, int, int, char); int draw_pyramid(char *, int, char); #pragma mark - Implementation int add_array(char *buffer, int index, int number, char character) { int n = number; int i = index; while (0 != n) { n = n - 1; buffer[i] = character; i = 1 + i; } return i; } int draw_pyramid(char *buffer, int height, char character) { int i = 0; int star = 0; int space = height; while (1 != space) { star = 1 + star; space = space - 1; i = add_array(buffer, i, space, ' '); i = add_array(buffer, i, star, character); i = add_array(buffer, i, star - 1, character); i = add_array(buffer, i, space, ' '); i = add_array(buffer, i, space, ' '); i = add_array(buffer, i, star, character); i = add_array(buffer, i, star - 1, character); i = add_array(buffer, i, space, ' '); i = add_array(buffer, i, space, ' '); i = add_array(buffer, i, star, character); i = add_array(buffer, i, star - 1, character); i = add_array(buffer, i, space, ' '); i = add_array(buffer, i, space, ' '); i = add_array(buffer, i, star, character); i = add_array(buffer, i, star - 1, character); buffer[i] = '\r'; i = 1 + i; /* 1 character for newline */ } buffer[i - 1] = '\0'; /* Remove extra '\r' character */ return i; } int main(int argc, const char *argv[]) { char buffer[1024]; int characters = draw_pyramid(buffer, 10, '*'); printf("%d characters \n", characters); printf("%s\n", buffer); return 0; }
Last edited by kodax; 09-10-2022 at 12:45 AM.
Ty salem Got it now..
Code:#include<stdio.h> int main() { int n,r; printf("Enter No. of Rows : "); scanf("%d",&n); printf("Enter No. of Reptitions : "); scanf("%d",&r); for(int i=1;i<=n;i++) { for(int repeat=1;repeat<=r;repeat++) { for(int space=1;space<=n-i;space++) { printf(" "); } for(int star=1;star<=i;star++) { printf("* "); } for(int space=1;space<=n-i;space++) { printf(" "); } } printf("\n"); } return 0; }
Ty to u too kodax.. but i was trying to get it through loops only.. I'm just a newbie and haven't covered arrays yet..