For loops help

P

paolo.arimado

Dear everyone,

Can someone please help me on my HW. I'm trying to write a program
that will display the following:

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

For this particular HW, I can only use for loops and is limited to
three for loops. I'm also limited to one cout="*" and one cout=" ".
However, I can use as many cout << endl; as I can.

I'm having a hard time writing a code for this one. I tried several
times, but I can't get it to work properly. I live in Japan and I take
an online C++ course. The instructor lives in the US so whenever I am
awake, he's asleep. Therefore, I cannot contact him directly when I
need help. I have to get this program up and running ASAP. I tried
searching everywhere for help, but there's none except for this group.
My textbook isn't much of a help either. I WILL GREATLY APPRECIATE THE
HELP I WILL GET!


Code:

#include <iostream>

using namespace std;

int main()
{
const int SIZE = 9;
const int SPACE = 10;
int i, j;
int k = SIZE / 2;

for (i = 1; i <= SPACE; i++) {
for (j = 0; j < (k - i) + 1; j++)
cout << " ";
for (j = 0; j < i; j++)
cout << "*";

cout << endl;
}

cin.ignore();cin.ignore();
return 0;
}

When I compile and run it, I get the following output:
*
**
***
****
*****
******
*******
********
*********
**********

It's kind of hard to see from here. Try to compile the code and test
it. You'll see it better.
 
D

Daz

When I compile and run it, I get the following output:
*
**
***
****
*****
******
*******
********
*********
**********

It's kind of hard to see from here. Try to compile the code and test
it. You'll see it better.

You need to increase the count of the asterisks to be shown in that
line, and decrease the spaces at the beginning of the line as you go,
and then do the opposite after you get the middle. of the design. Yes I
know the explanation is vague, so I have given you just a hint as
opposed to me doing it for you.

I am new to C++ too, so I could be incorrect, but it makes sense to me
and would be something I would try.

Good luck! :)
 
U

utab

You can start that by trying something like

for (){ // It is logical to use two nested for loops because you are
trying to print a 2d
for(){ // pattern
if() // to control if you are at the right place to print an
asterix
// if you are print "*"
else
// print " "
}
// print the newline character

I am now at work but I think I did this a long time ago and can post
that here in the evening if you can wait :). But try over the framework
I have provided.

Regards,
 
P

paolo.arimado

utab said:
You can start that by trying something like

for (){ // It is logical to use two nested for loops because you are
trying to print a 2d
for(){ // pattern
if() // to control if you are at the right place to print an
asterix
// if you are print "*"
else
// print " "
}
// print the newline character

I am now at work but I think I did this a long time ago and can post
that here in the evening if you can wait :). But try over the framework
I have provided.

Regards,


Can you please post the code if you can? I'll try to modify my code
out of your framework and see what happens. Thank you ver much for
helping me.
 
D

Daz

Can you please post the code if you can? I'll try to modify my code
out of your framework and see what happens. Thank you ver much for
helping me.

Yes I can. However, as I don't want to do your homework for you, I have
given a very similar example, only the resulting shape is not quite the
same. However, I am hoping that you may be able to understand what's
happening a little better.

Did whoever set the task mention anything about using the 'inline'
keyword, or whether ot not you could use recursion?

Here's the code for you play with.

#include <iostream>

using namespace std;

int main()
{
int numberOfSpaces = 5;
int numberOfAsterisks = 1;
signed int spacesOffset = -1;
signed int asteriskOffset = 2;
while (numberOfAsterisks > 0)
{
if (numberOfAsterisks == 11)
{
spacesOffset = 1;
asteriskOffset = -2;
}
for (int i = 1; i <= numberOfSpaces; i++)
cout << " ";
numberOfSpaces = numberOfSpaces + spacesOffset;
for (int i = 1; i <= numberOfAsterisks; i++)
cout << "*";
numberOfAsterisks = numberOfAsterisks + asteriskOffset;
cout << endl;
}
return 0;
}

OUTPUT:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
 
D

Daz

Daz said:
Yes I can. However, as I don't want to do your homework for you, I have
given a very similar example, only the resulting shape is not quite the
same. However, I am hoping that you may be able to understand what's
happening a little better.

Did whoever set the task mention anything about using the 'inline'
keyword, or whether ot not you could use recursion?

Here's the code for you play with.

#include <iostream>

using namespace std;

int main()
{
int numberOfSpaces = 5;
int numberOfAsterisks = 1;
signed int spacesOffset = -1;
signed int asteriskOffset = 2;
while (numberOfAsterisks > 0)
{
if (numberOfAsterisks == 11)
{
spacesOffset = 1;
asteriskOffset = -2;
}
for (int i = 1; i <= numberOfSpaces; i++)
cout << " ";
numberOfSpaces = numberOfSpaces + spacesOffset;
for (int i = 1; i <= numberOfAsterisks; i++)
cout << "*";
numberOfAsterisks = numberOfAsterisks + asteriskOffset;
cout << endl;
}
return 0;
}

OUTPUT:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Paolo, I have yet to see your edited version of this code or hear any
comments from you. I suspect that you have used my code for your
homework, and if this is the case, as I stated before, it does NOT
print the pattern that was asked, and your tutor will most likely
notice this. Please can you let me know how you got on? This group is
not here to do your homework for you, and if you can't take the time to
learn from someone's example, and get back to them telling them what
you have learnt, you may find that some people will be more inclined to
ignore your requests for assistance.

I for one, love helping people learn, as it gives me an immense feeling
of satisfaction. However, if you don't wan't to learn from the examples
given, and instead you use other people's code, then I would be wasting
their time, and maybe even yours.

Please could you let me know how you got on, as I am interested to know?
 
P

paolo.arimado

Daz said:
Paolo, I have yet to see your edited version of this code or hear any
comments from you. I suspect that you have used my code for your
homework, and if this is the case, as I stated before, it does NOT
print the pattern that was asked, and your tutor will most likely
notice this. Please can you let me know how you got on? This group is
not here to do your homework for you, and if you can't take the time to
learn from someone's example, and get back to them telling them what
you have learnt, you may find that some people will be more inclined to
ignore your requests for assistance.

I for one, love helping people learn, as it gives me an immense feeling
of satisfaction. However, if you don't wan't to learn from the examples
given, and instead you use other people's code, then I would be wasting
their time, and maybe even yours.

Please could you let me know how you got on, as I am interested to know?



Daz,

Sorry if I wasn't able to come and post my edited code. I was kind of
busy at school. Anyways, here's the edited code:

#include <iostream>

using namespace std;

int main()
{
int numberOfSpaces = 5;
int numberOfStars = 1;
int spacesOffset = -1;
int starsOffset = 2;

for (;numberOfStars > 0;)
{
if (numberOfStars == 9)
{
cout << " ";
for (int i = 1; i <= numberOfStars; i++) cout << "*";
cout << endl;
spacesOffset = 1;
starsOffset = -2;
}
for (int i = 1; i <= numberOfSpaces; i++) cout << " ";
numberOfSpaces = numberOfSpaces + spacesOffset;
for (int i = 1; i <= numberOfStars; i++) cout << "*";
numberOfStars = numberOfStars + starsOffset;
cout << endl;
}
cin.ignore();cin.ignore();
return 0;
}

As you can see, the one you've written works just fine, however, I had
to modify the middle part in order to get the desired pattern that my
instructor asked me to do. Lastly I want to thank you for helping me.
 
D

Daz

Daz,

Sorry if I wasn't able to come and post my edited code. I was kind of
busy at school. Anyways, here's the edited code:

#include <iostream>

using namespace std;

int main()
{
int numberOfSpaces = 5;
int numberOfStars = 1;
int spacesOffset = -1;
int starsOffset = 2;

for (;numberOfStars > 0;)
{
if (numberOfStars == 9)
{
cout << " ";
for (int i = 1; i <= numberOfStars; i++) cout << "*";
cout << endl;
spacesOffset = 1;
starsOffset = -2;
}
for (int i = 1; i <= numberOfSpaces; i++) cout << " ";
numberOfSpaces = numberOfSpaces + spacesOffset;
for (int i = 1; i <= numberOfStars; i++) cout << "*";
numberOfStars = numberOfStars + starsOffset;
cout << endl;
}
cin.ignore();cin.ignore();
return 0;
}

As you can see, the one you've written works just fine, however, I had
to modify the middle part in order to get the desired pattern that my
instructor asked me to do. Lastly I want to thank you for helping me.

Fantastic! Very well done! You have proved that you understood the code
enough to modify it to do what you actually wanted it to do. It just
goes to show that the answer is always simple, once you know the
question. Thanks for getting back to me, and may I wish you all the
best in the future.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top