My question:
What is the output of the code corresponding to the following pseudocode?

``````Set y = 0
For (i = 0; i<=6; i=i+3)
For (j = 0; j<=15; j=j+5)
Set y = y + 1;
End For (j)
End For (i)
Output y``````

This is what I have so far but I'm not sure if it's right/complete:

``````#include <iostream>

int main()
{
int y = 0;
for(int i = 0; i <= 6; i+= 3)
for(int j = 0; j<= 15; j+=5)
++y;

std::cout << y << "\n";
}``````

Edited by __avd: Add [code] tags. Encase your code in: [code] and [/code] tags.

4
Contributors
4
Replies
5
Views
8 Years
Discussion Span
Last Post by rafta

code looks fine at a glance, why dont you type it in and run it... the output should be 12 i think

You are right on brother.

One suggestion: whenever you see a self re-assignment, you should have "compound operator" runnin' through your noggin'

``````y = y + 1;

//is the same thing as

y += 1;  //The += operator signifies "accumulation"``````

It's such a common task to re-assign a value to the original variable, that c++ made these compound operators available to you:

``````y = y / x;     y /= x;
y = y * x;     y *= x;
y = y - x;     y -= x;
y = y + x;     y += x;
y = y % x;     y %= x;

for (i = 0; i<=6;  i+=3)
for (j = 0; j<=15; j+=5)``````

Edited by Clinton Portis: queso con queso with a side of queso.

Edited by confusedndazed: n/a

``````#include <iostream>
using namespace std;

int main()
{
int y = 0;
for(int i = 0; i <= 6; i+= 3)
for(int j = 0; j<= 15; j+=5)
++y;

cout << y << "\n";

system("PAUSE");
return 0;

}``````