How to Modify C++ Code to Create a Center-Aligned Pyramid?

  • Context: Comp Sci 
  • Thread starter Thread starter Naldo6
  • Start date Start date
  • Tags Tags
    C++ Pyramid
Click For Summary

Discussion Overview

The discussion revolves around modifying C++ code to create a center-aligned pyramid of asterisks. Participants explore how to adjust the existing code to achieve the desired output format, focusing on the use of spaces to align the pyramid correctly.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant shares their initial code that produces a left-aligned triangle of asterisks and requests help to modify it into a pyramid shape.
  • Another participant points out the need to print spaces before the asterisks to achieve the center alignment.
  • Several participants discuss how to calculate the number of spaces needed for each line based on the pyramid's height.
  • There is a suggestion to use an additional loop to print the required spaces before printing the asterisks.
  • A participant expresses confusion about how to implement the space-printing loop within their existing code structure.
  • Eventually, one participant successfully modifies their code to create the desired pyramid shape and shares their solution.

Areas of Agreement / Disagreement

Participants generally agree on the need to print spaces for alignment, but there is some confusion regarding the implementation details. The discussion includes multiple attempts and clarifications, indicating that not all participants are on the same page regarding the coding approach.

Contextual Notes

Some participants express uncertainty about specific coding instructions and the use of loops, indicating varying levels of familiarity with C++ programming concepts.

Who May Find This Useful

Individuals learning C++ programming, particularly those interested in formatting output in console applications, may find this discussion helpful.

Naldo6
Messages
102
Reaction score
0
I need to do a pyramid in C++ like this one:
*
* *
* * *
* * * *
* * * * *

buti just know how to do the figure at one side:

*
* *
* * *
* * * *
* * * * *


# include <stdio.h>
# include <math.h>

void main()
{

int count,count2;

for(count=1;count<=5;count++)
{
for(count2=1;count2<=count;count2++)
printf(" *");
printf("\n");
}
}

Can anyone help me hoy to change from the side to a pyramid form?
 
Physics news on Phys.org
Please elaborate, as both pyramids look identical.
 
ok sorry, i did it as a pyramid but the page puts it at the corner yet. Bu i mean i just know how to do the figure that is above drawn with the *, but i ned to do it but in the pyramid form, like a triangle of the cue ball, but with base 5.
 
You have to print spaces before stars.
 
ok, and howi do that?...
 
how i do that in my code above ?
 
If you draw your triangle with visible spaces, then you can count how many you need on each line, and you can programme accordingly, right?

----*
---*-*
--*-*-*
-*-*-*-*
*-*-*-*-*
So you see that there are fours spaces on the first line, three on the second, and so on.
Now you just have to translate that into your code.
 
ok, i know that ther i need always four spaces in each line but i don't know how to put that in codes for run my programs perfectly...i just know how to put all the * to begin at the left corner...that the problem

can u help me more?...
 
...
for(count=1;count<=5;count++)
{
/* here you need to insert 4 spaces when count==1,
3 spaces when count==2, (3=5-2)
2 spaces when count==3, (2=5-3)
1 space when count==4, (1=5-4)
0 space when count==5 (0=5-5)
get the idea?
You can do this with an additional for/next loop that runs 5-count times, each time it should print ONE space.
Try it out, if it does not work, post this part of your code.
If it works, post the result!
*/
for(count2=1;count2<=count;count2++)
printf(" *");
printf("\n");
}
...
 
  • #10
Do you mean that right?...

but i don't know how to state the instruction of the spaces in that for between the other two for...
help me a little more please

# include <stdio.h>
# include <math.h>

void main()
{

int count,count2;

for(count=1;count<=5;count++)
{
for( ; ; )
for(count2=1;count2<=count;count2++)
printf(" *");
printf("\n");

}
}
 
  • #11
i don't know how to do this:

You can do this with an additional for/next loop that runs 5-count times, each time it should print ONE space.

i have just discuss in my class the instruction for no more than that...
 
  • #12
for(count=1;count<=5;count++)
{
// complete the start value and end condition for the loop, the index should increment by one
for( ; ; ){ /* here you add a printf statement that prints simply " " (a space)*/}
//
for(count2=1;count2<=count;count2++)
printf(" *");
printf("\n");

}
 
  • #13
ty... i sucees with my pyramid i get the correct code

# include <stdio.h>
# include <math.h>

void main()
{

int count,count2,count1;


for(count=1;count<=5;count++)
{
for(count1=count;count1<=5;count1++)
printf(" ");
for(count2=1;count2<=count;count2++)
printf(" *");
printf("\n");

}
}
 
  • #14
Congratulations!
 
  • #15
ty... i will need your help durng the rest of the semester... i have another homeworks.. i will begin on the week so i will request your help... a lot of thanks...
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K