Quick Question for C++ Homework Please Help :)

Hello! I apologize if I'm breaking any unspoken or spoken forum rules. I'm completely new to C++ and the forum scene in general. I would appreciate any assistance or direction that anyone can offer :)

The Problem:

Program Language: C++
Using: Visual studio 2010

Alright, So I am doing an exercise with loops given to me by my professor.

Basically, I am having trouble getting this little pattern to appear in my output display.

Ideally it looks like this.

*
.. *
.... *
......*

NOTE: Without the periods, I only did that because the forum messenger takes away any empty spaces.

Easy enough right? Well, They need to be individually placed. Now, every time they go through the loop, I need to Add two spaces to the next one. The trouble is I have no idea how to add those two spaces automatically using a loop. I know that this is insanely simple and the solution is probably staring me right in the face but I just can't see it. Any help is enormously appreciated!

Last edited on
Well you can simply have a string and add the spaces on every iteration of the loop, AFTER printing the string + '*'.


If you cant get it to work, please send the code so we can help further.
Hey, Shoyoninja, thanks a lot for replying to my problem!

To make things a little bit I don't know, maybe tricky is the word...

My professor doesn't allow us to utilize anything that we haven't studied in class.

He hasn't taught us about strings yet so I'm supposed to make due without.

I'll Type out the exact way he phrases the question and see if that helps.


(3) Make a diagnoal line of 'N' asterisks down the screen. For example, if 'N' is 4, then.

*
..*
....*
......*

-------------------------------------------------------------------------

So basically, I needs to create a "for" loop that will initially place one asterisk, go through the loop again, enter two spaces, enter the asterisk, go through the loop again, etc.

I can use any many if statements, loops, etc as a like though. I'm sorry I know I'm not the most well spoken guy in the world, far from it.

As of yet there really is no code.
Please, write us a loop that repeats N times and on every time does two things:
1. Print the value of the loop counter.
2. Print one '*' and newline.

Then we can guide you further.
#include <iostream>
using namespace std;
int main()

{
int loopcounter,i,N;

loopcounter = 0;

cout << "Enter N" << endl;
cin >> N;
cout << endl << endl;

for(i=0;i<N;i++)
{
loopcounter ++;

cout << loopcounter << endl;

cout << "* \n";
}

return 0;
}


//Something like this?
Yes. Try to use code tags on posts. It makes reading and commenting easier.

Do notice that you don't need separate 'loopcounter', because you have the 'i'. Actually, we really like the i's 0..N-1 values rather than the loopcounters 1..N range. Lets rename the 'i' to 'row' to be more expressive.

With those changes (and one about namespaces):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
  using std::cout;
  using std::cin;
  using std::endl;

  int row, N;
  cout << "Enter N" << endl;
  cin >> N;
  cout << endl << endl;

  for ( row=0; row < N; ++row )
  {
    cout << row << endl;
    cout << "*\n";
  }
  return 0;
}

The line 16 prints a number. How many spaces should there be before the *? Do you notice any correlation between the 'row' value and the number of required spaces?
For the first * there should be no preceding spaces.
There should be 2 preceding spaces for the second *, on the second row.
There should be 4 preceding spaces for the third *, on the third row.

And I need that trend to continue throughout the program.

" Do you notice any correlation between the 'row' value and the number of required spaces?"

Yes, It would seem that for every 1 row I output after the first, I need to add two spaces.

What I mean to say is they go hand in hand and advance at the same pace.

Maybe I'm not understanding what you're saying.

I'll try the code tags the next time I post code, I'm brand spanking new, thanks for the advice!!
So, 2*row and number of spaces go hand in hand?

Thus, if your code has within the loop the right number, how would you print that many space characters?
Topic archived. No new replies allowed.