Help with my home work please, program that prints stars

Hey guys

I've been trying to solve this but I couldn't ..

Writing a program that prints stars , so I already wrote one using while loop but I did one of the 3 and I've been trying to do the rest but I couldn't.

http://oi42.tinypic.com/2uethep.jpg

this is a picture of the output of the code this is the pseudocode of the program:

int x
int y=0
int z=0
print to the user that he should enter a number enter x
while(z<x) { while(y<x) { print "*" y++ } z++ y=z print end line }

.. you can see this program will give us b , but how could I do the same for a,c,d ? I tried and I could only put space at the start and it wouldn't look the same.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
using namespace std;
int main(){

int x;
int row=0;
int c=0;

cout<<"please enter a number"<<endl;
cin>>x;

while (c<x){
while (row<=c){

	cout<<"*";
	row++;


}
c++;
cout<<endl;


}
getchar;
getchar;
return(0);

}


it only prints a star in each line , but I wanted to do a

please give me a hint to solve a , c , d

thank you
Last edited on
It may help to think of each line as needing to contain a fixed number of characters, and all that changes with each iteration is the point at which asterisks change to spaces (or vice versa).

-Albatross
Yeah man I'm sorry I didn't get your point , let's focus on the code for a moment , I made this code to solve for problem a " in the picture " , but it just won't work !

for what I know is that if I entered 5 as the x value " for example "
he's going to the outer while so the statement is going to be like this since c=0 (0<5) it's true so he's going to the inner while and row should be 0 so the statement is going to be ( 0<=0) which is true and he's going to print one star and then is 1<=0 it's false so it's going to exit and add 1 to c cuz of " c++" and print a new line and then going back to the first while (1<5) which is true and it's going to the inner while (0<=1) which is true and he's going to print a star and add one so row is going to be 1 , (1<=1) which is true and he's going to print another star and so on

but the problem is the program only prints star then a new line then a star and so on

why ? am I getting the while loop wrong or what ?
oh man I solve a now , I just forgot to set row to be 0 after the inner loop ^^

now I need some help with c and d

could you guys help me ?

I tried to solve but I couldn't
Your variable row never resets to zero. Therefore, it stays equal to c each time the inner while loop starts, and consequently you just get a column of stars.

Have you considered using for loops? They're syntactic sugar, but they're extremely useful syntactic sugar, and would help you avoid issues such as these. :)
for(initialization;condition;iteration)

Example:
1
2
3
4
5
6
for(int i = 1; i <= 5; ++i) {
    std::cout << "Bunny #" << i << ".\n";
    if(i == 5)
        std::cout << "Why are we even counting bunnies?";
}
Bunny #1.
Bunny #2.
Bunny #3.
Bunny #4.
Bunny #5.
Why are we even counting bunnies?


EDIT: Oops, you solved A when this was posted. All well.

-Albatross
Last edited on
There are -in the first figure (a- 10 rows with an incremental number of columns
so if you know the number of iterations to solve the problem, you should use for loops instead of while or do while loops, i'll give to you the first figure (a).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//figure.cpp
//##

#include <iostream>
using std::cout;
using std::endl;


int main(){

for(int row=1;row<=10;row++){
        for(int col=1;col<=row;col++)
                cout<<'*';

cout<<endl; //start a new line of output
}//end for

return 0; //indicates success
}//end main 



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


Thanks for trying to help me man , Yeah this is my homework and I need to do the same in the class or I'll get a 0 :( , that's why I'm trying to solve this using while loop , our prof said that we're going to learn for loop after doing this.
eyenrique, thanks so much I did state in my last post that I solved the first one using while loop , the problem was that I didn't set row to be 0 after the while loop , that's why it's been giving me one star each time ^^

but now I'm trying to solve c , d using while loop " cuz our prof said use while loop only, until we learn do while and for loop ^^ "

but now I'm stuck at this output x=5
*****
****
***
**
*

it's not the same I know , but I'll try harder , any tips would be useful , please post it ^^
Maybe you could use some spaces to push each line forward. The exact amount would vary per line, but I'm sure you could figure out the pattern.

Part of the art that is programming is problem solving, after all. :)

-Albatross
I think that this is not the kind of problems to practice while loops... anyway
figure (c) its like (a) just play with the outer and inner loops limits and reset counters when need it.
Thanks guys so much for helping , the problem is that I got no one to ask , every time I ask our prof something he just thinks that I'm lazy and wants me to figure everything without helping , even if he tried to help he just gives you an answer that you already know , like asking what's wrong , and then says it's a logical error , and I already know that -_-

anyways sorry for getting out of the subject ^^

now i'm working on c

the problem is it gives me the first two lines right for example x=5
it prints

*****
space****
space***
space**
space*

* space means " " just to clear it out "

you can see that I got the first two lines but the other lines it just won't work

even if I entered 10 , the first two lines are the correct ones.

I think it's a problem with the print , but how could I solve this ?

it's just going to print one space each time.
Last edited on
version c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include<iostream>
#include<iomanip>// <<<<<for setw()
using namespace std;
int main()
{
    int x;
    int row=0;
    int c=0;
    cout<<"please enter a number"<<endl;
    cin>>x;
    while (c<x)
    {
        cout<<setw(c+1);
        while (row<x-c)
        {
            cout<<"*";
            row++;
        }
        row=0;
        c++;
        cout<<endl;
    }
    getchar;
    getchar;
    return(0);
}
it's just going to print one space each time.
Well, then make it print more than one space as needed.

Here's a pointed question for ya. How many spaces would each line need for the rightmost stars in each line to line up vertically?

@People posting full solutions in this thread:
Mandatory reading for you both.
http://www.cplusplus.com/articles/DjGEy60M/

-Albatross
Last edited on
Thanks buddy , but I'll try to solve it myself , hopefully I can.
Thanks albatross , the problem is this may be my 4th c++ lab this week , so I didn't learn much

But I thought of a solution if I could do it then it will solve c problem , but I don't think that I can , the solution is that we make the program prints spaces depending on row's value.

The question is , how can I do this ?
How have you been printing asterisks depending on the row's value thus far?

-Albatross
@Cutefriendzoned
You have to know that you can nest and stack blocks of code like for loops or if..else statements;
Hint: inside of your outer loop you can stack two loops 1 for spaces and the other one to print the character; play with your imagination.
@ Albatross I am sorry my bad I'm not the guy of big words sorry.
@ Cutefriendzoned you have a great attitude to solve it by yourself not many on here are like you. I'm sorry for my post.
Albatross , I got your point , thank you ^^

eyenrique, thank you man I had the idea but I didn't do it because I thought it wouldn't work :(

Chriscpp , it's ok man ^^

..

I need to study calculus for the mid term exam.

I'll try the solution and work to solve this tomorrow , hopefully this thread won't get close in case I wanted to ask a question.

Thank you guys so much for helping me out.
Last edited on
It works
believe me ;D


Eyenrique-MacBook-Pro:Help Eyenrique$ ./figure 
 **********
  *********
   ********
    *******
     ******
      *****
       ****
        ***
         **
          *


But i can't give to you the code :P
Topic archived. No new replies allowed.