-1

I'm aware that the increment operator = ++i, is shorthand for i = i +1
whereas the shorthand for decrement operator is --i (decrement operator being i = i -1).

In this case, I'm trying to print out a certain amount of asterisks for numStars and so I went with the increment operator (i = i + 1). I went with numPrinted = numPrinted + 1 to increment. Here is a brief glance of the code:

  numStars = 12;
  numPrinted = 1;

  while (numPrinted < numStars) {
     numStars = numStars * numPrinted;
     numPrinted = numPrinted + 1; //Went for increment since I'm assuming 
     additional asterisks will be shown

     System.out.print("*"); /* My output produces a total of 14 asterisks 
     whereas the expected output wants 12 asterisks */

  }

Seeing that the expected output is 12 and numStars = 12 already, was it necessary of me to put numStars = numStars * numPrinted? Because I feel that might be the reasons there's those two extra asterisks in my output rather than just 12 asterisks. Unless the extra two asterisks are there because of my decision to increment numPrinted?

Thank you in advance for the help and suggestions that will be made here.

acruz30
  • 21
  • 1
  • 4
  • No, you should treat `numStars` as a constant, and you should certainly not multiply it with `numPrinted`. You should also change your `while` loop to a `for` loop. – Andreas Sep 27 '17 at 22:51
  • [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Sep 27 '17 at 22:52

3 Answers3

0

Your loop will continue until numPrinted is more or equal to numStars.

If you know that you want 12 loops, you should not be changing the value of numStars, only of numPrinted.

Furthermore, numPrinted should not start at 1. This will result in 11 stars.

The correct way to structure the loop, (should you wish to use a while loop instead of a for loop) is as follows:

  numStars = 12;
  numPrinted = 0;

  while (numPrinted < numStars) {
     numPrinted = numPrinted + 1; 
     System.out.print("*");
  }
user3362196
  • 102
  • 7
  • So I will need to keep numStars at the same value in order to produce the expected output of 12 asterisks. I went back to try to change the value of numPrinted from 1 to 0 as you suggested here. However, the exercise isn't letting me change any of the default numbers (if that makes sense). Rather, it's only letting me change my while loop. Would it still be possible to produce 12 asterisks even if numPrinted = 0? – acruz30 Sep 27 '17 at 23:02
  • Can you change the condition of the while loop? You could use "while(numPrinted <= numStars)" instead of "while(numPrinted < numStars)" – user3362196 Sep 27 '17 at 23:07
  • thank you I was able to output 12 asterisks when I changed from < to <= . If you don't mind, changing it to <= helped in getting the exact amount of 12 asterisks? And should I practice using <= rather than < for future coding? – acruz30 Sep 27 '17 at 23:10
  • It depends on what you are trying to accomplish. Neither option is better than the other by default. It is up to personal preference and what makes sense given the context of the loop. <= means "less than or equal to" so your loop would run even if numPrinted and numStars were the same value. It stops when numPrinted is greater than numStars. – user3362196 Sep 27 '17 at 23:15
-1

In what language are you coding ?

You can try:

  numStars = 12;
  numPrinted = 0;

  while (numPrinted < numStars) {
     numPrinted = numPrinted + 1; //increment by one

     System.out.print("*"); /* Print one asterisk */
  }

Start counting from 0, otherwise 11 asterisks will be printed.

Enis
  • 15
  • 2
  • I am coding in Java. Another user went ahead and also suggested the numPrinted to be assigned 0 as its value. However, the exercise is only allowing me to correct my while loop and I am not able to change any of the default values such as numPrinted = 1; in the exercise. Is there a possible way of printing 12 asterisks even if numPrinted starts at 1? – acruz30 Sep 27 '17 at 23:04
  • Yes, either use 'while (numPrinted <= numStars)' or 'while (numPrinted < numStars + 1)'. – Enis Sep 27 '17 at 23:08
-1

numStars = 12; numPrinted = 1;

  while (numPrinted <= numStars) {      
     numPrinted = numPrinted + 1;      
     cout << "*";
  }  

I am coding c++, the value numPrinted = 1, and the value of numStars is the # of loops. this loop 12 times and print 12 asterisks in the row.

Piano
  • 1