0

FlowChart

The flowchart above represents an algorithm that displays the numbers 20, 40, 60, 80, 100, 120, 140, 160, 180 on the screen. Code the algorithm into a program using the while statement. The counter variable should be an int variable named count. Save and run the program. Test the program to see if you get the correct output. Correct any errors, when the program is running correctly copy and paste the IPO chart and the program code into a word document.

I just started C++ and I'm unsure of how to use the while statement and writing the code. Help would be appreciated. So far, here's my code:

#include <iostream>
using namespace std;

int main ()
{
  int count;
  count = 10

;  while (count < 200) {
    cout << count << ", ";
    count*=2;
  }

  system("pause");
  return 0;
}

How do I "add 10 to counter" and also display the numbers listed above? (20, 40, 60, 80, 100, 120, 140, 160, 180) so far, it only displays 10, 20, 40, 80, 160 I am unsure of how to also display the numbers in between while still doubling it? Thanks!

Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
user1756913
  • 71
  • 1
  • 2
  • 4

5 Answers5

2

To add ten to a variable, you can just use:

count = count + 10;

or the shortened form:

count += 10;

To output a number multiplied by two, you can simply use:

cout << (count * 2);

In terms of outputting the number list in the (seemingly) desired format, you want a ", " before every entry bar the first, so you could change your cout statement to something like:

if (count > 10)        // assuming 10 is the first number.
    cout << ", ";
cout << count;

and then ensure at the end that you write out a newline:

cout << '\n';

If you're not actually worried about nice formatting of the numbers on a single line, just user:

cout << count << '\n';

to get one per line.


By way of example, here's a program that prints out the numbers that are one less than multiples of three up to and including one less than thirty:

#include <iostream>

int main (void) {
    int num = 3;
    while (num <= 30) {
        if (num > 3)
            std::cout << ", ";
        std::cout << (num - 1);
        num = num + 3;
    }
    std::cout << '\n';
    return 0;
}

The output is:

2, 5, 8, 11, 14, 17, 20, 23, 26, 29

Analysis of the comments above, along with the example program, should hopefully be enough for you to build a similar program to your own specification.

Of course, there are often better way to do things, were it not for the arbitrary limitations enforced on you. Your whole program could be written as:

#include <iostream>

int main (void) {
    std::cout << 20;
    for (int num = 40; num < 200; num += 20)
        std::cout << ", " << num;
    std::cout << '\n';
    return 0;
}

However, since those limitations are probably to ensure you learn specific parts of C++, you'll have to do it the long way.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
0

As I understand the flowchart, the the product with 2 should only be displayed. So,

count*=2;

Is incorrect because it changes count. You want to add 10, so replace it with count+=10;.

Above, you are outputting count, but you want to output the product with two:

cout << (2*count) << ", ";

Finally, you should move the semicolon from before the while-statement behind count=10.

Also, system("pause"); - Why is it wrong?

Community
  • 1
  • 1
Konstantin Schubert
  • 3,242
  • 1
  • 31
  • 46
0

You're interpreting the flow chart the wrong way around. You're multiplying the counter by 2 with count*=2, you should not be doing that, the flow chart says, Display counter multiplied by 2 that means something different.

while (count < 200) {
  cout << count * 2 << ", ";
  count += 10;
}

Note that the display command is in a parallelogram, while the add command is in a rectangle, these shapes have different meanings in flow charts

wich
  • 16,709
  • 6
  • 47
  • 72
0

You should:

  1. Multiply while couting, so that the multiplication doesn't affect the real value of count.
  2. Add 10 after couting, so that the next iteration will output a new value in the sequence.
  3. Keep iterating while count < 100.

Like this:

while (count < 100) {
    cout << count*2 << ", ";
    count+=10;
}
Tamer Shlash
  • 9,314
  • 5
  • 44
  • 82
  • Okay! I see what I did wrong, but since count is multiplied by 2 and count < 200. It display all the number up to 380. I think I have to stop it at 180. – user1756913 Oct 19 '12 at 06:36
0

use the plus sign(+) to add a specific value and assign them with =; or the combination +=.

to shorten your code it could look like:

int main(int argc, char* argv[])
{
int count = 10;
while( (count+=10) < 200 )
    cout << (count*2) << ", ";

system("pause");
return 0;
}

or if you need such a simple counting loop, i would use the for()-loop like:

int main()
{
for( int count = 10; count < 200; count+=10)
    cout << (count*2) << ", ";

//do stuff    
}
Zaiborg
  • 2,492
  • 19
  • 28