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.
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.
#include<iostream>
usingnamespace 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
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).
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
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.
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 ^^
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 ?
#include<iostream>
#include<iomanip>// <<<<<for setw()
usingnamespace 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);
}
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.
@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.