10 Replies - 569 Views - Last Post: 28 October 2013 - 10:40 AM Rate Topic: -----

#1 Friendzoned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-October 13

Simple question

Posted 27 October 2013 - 08:02 AM

Hey I'm currently taking c++ , and I got a question and here it is " I posted it on other forums "

Quote

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.


So I tried to let y=x and let the statement be y<=x but it won't work it gives a logical error.

Is This A Good Question/Topic? 0
  • +

Replies To: Simple question

#2 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Simple question

Posted 27 October 2013 - 08:20 AM

Take a piece of paper and pen. SLOWLY draw the pattern you want to code up, an few times. Put your mind right on the tip of the pen, in your imagination.

What patterns are you using, at a low level, to draw the pattern (top to bottom, and left to right). No backtracking.

For simple shapes, a nested for loop is good to use. The outer one handles the top to bottom (rows) logic (there isn't much), and the inner for loop handles the left to right logic.

Work on it. These exercises are too good to just Google an answer to. You have to build up your problem solving skills for things like this, in order to program. If you get stuck, post your code in between :code: tags, so the output will keep it's pattern's shape.
Was This Post Helpful? 0
  • +
  • -

#3 Friendzoned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-October 13

Re: Simple question

Posted 27 October 2013 - 09:21 AM

Thank you so much , I'll work on it and try harder to solve it as soon as I finish my mid term exam for calculus 1

Can you please check this thread a day or two from now ? cuz I need to solve this cuz it's a home work , if I didn't solve it myself then I'll need some help .

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 Friendzoned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-October 13

Re: Simple question

Posted 28 October 2013 - 05:09 AM

So today I've been working on the program to make it print stars like " a "

but I got stuck , could you guys give me a hint ?

this is the code:

#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;
	row++;


}
c++;
cout<<endl;


}
getchar;
getchar;
return(0);

}


what I know is that for example if I entered 5 as the value of x and should be like this 0<x if yes then it goes inside the other while row<=c and prints one star since 0<=0 and after c will be one because we incremented it "c++" and it should print a new line then goes back to while c<x so it's gonna be like this 1<5 and if yes it goes inside the other while and it'll be like this row<=c or 0<=1 then it should print 2 stars and so on , but it only prints one star each like with this code.

thanks guys

each line***

sorry about my English, it's my second language.
Was This Post Helpful? 0
  • +
  • -

#5 Friendzoned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-October 13

Re: Simple question

Posted 28 October 2013 - 07:30 AM

I'm really going to need some help on this one guys , I've been trying to solve this but I only solved b , and I need a ,c,d to complete my homework.

can you guys give me a hint or something , please!
Was This Post Helpful? 0
  • +
  • -

#6 Friendzoned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-October 13

Re: Simple question

Posted 28 October 2013 - 08:09 AM

there's a probleem with the code , I meant to put cout<<"*"; instead of cout<<row;
Was This Post Helpful? 0
  • +
  • -

#7 Friendzoned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-October 13

Re: Simple question

Posted 28 October 2013 - 08:52 AM

I did solve a now , I need c and d guys , could you help me ?
Was This Post Helpful? 0
  • +
  • -

#8 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 8897
  • View blog
  • Posts: 33,364
  • Joined: 12-June 08

Re: Simple question

Posted 28 October 2013 - 09:02 AM

Help you how? If you got half of them done it is just a re-application of what you learned.. how to control the columns of * printed and the vertical row position.
Was This Post Helpful? 0
  • +
  • -

#9 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Simple question

Posted 28 October 2013 - 09:23 AM

Start with something along these lines:
int width = 10, spaces, i;

for(spaces = 0; spaces<10; spaces++) {
   for(i=0; i<spaces; i++)
      cout a space

   for(i=0; i < width-spaces;i++)
      cout a star

   cout a newline
}



So finish C and do D, along the lines of what you've learned from printing these patterns.

This post has been edited by Adak: 28 October 2013 - 09:24 AM

Was This Post Helpful? 0
  • +
  • -

#10 Friendzoned  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-October 13

Re: Simple question

Posted 28 October 2013 - 09:36 AM

I'm trying to solve c now , the problem is that I'm only allowed to use while loop for now

I'm stuck at the first two lines , it prints it right but after the first two lines it just get the same as b because there's only one space " that it prints "

I'll try harder , and Adak , thanks for helping , I did similar to this program , that at the end it prints a space and a new line but it only works with the first two lines.
Was This Post Helpful? 0
  • +
  • -

#11 Adak  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 331
  • View blog
  • Posts: 1,168
  • Joined: 01-April 11

Re: Simple question

Posted 28 October 2013 - 10:40 AM

Forget the one's you did before that didn't work, and if you need to use while loops then replace the for loops with the while loops.

It's not that tough, just remember to set up the while loop, because it doesn't have an initial assignment to the iterator i.

The first for loop becomes simply

spaces = 0; //the "set up"
while(spaces < 10) {
   i=0; //"set up" for the next while loop
   while(i < spaces) 
      cout a space here;
   etc. 

}



It's not that difficult once you get into it. Keep working at it and the light will appear!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1