for loop assistance

Hi, I got an assignment that I need some help with? I need to include this for loop in a program and it should excuted 10 times. I'm noy sure what the 'X' means. and where to start when including it in a program. Any guide lines?

1
2
  for (int i = 1; i <= n; i = i + 3)
cout << 'X';
Last edited on
closed account (jwkNwA7f)
You can just put it in your main function:
1
2
3
4
5
6
7
int main()
{
    int n = 10;
    for (int i = 1; i <= n; i = i + 3)
         cout << 'X';
    //...
}

This just ouputs the character X:
1
2
cout << 'X';
Output:
X


Also, instead of:
i = i + 3
You can just use:
i +=3
Thank you so much.
Last edited on
How can I make this code excute 10 times without changing anything on it?
closed account (jwkNwA7f)
I'm sorry, I'm not sure what you mean.

The code I gave above isn't the full source code, just a portion of the code that has the for loop. I can't do all of your homework for you. I can only help you with your problems that you are having. If this is the case, your code should be something like this:

1
2
3
4
5
6
7
8
9
// Your includes

int main()
{
    int n = 10;
    for (int i = 1; i <= n; i = i + 3)
         cout << 'X';
    //...
}
Last edited on
1
2
3
4
5
6
7
int main()
{
    int n = 10;
    for (int i = 1; i <= n; i = i + 3)
         cout << 'X';
    //...
}


This will only execute 4 times, since i is being incremented by 3. You need to either increment i by only 1 (++i) or have n = 30.
Last edited on
Topic archived. No new replies allowed.