• 0

problem with while loop?


Question

Hey guys,

 

Im trying to: determine how fast your computer is, and how much console output slows it down. Write a while loop that counts from 1 to 1,000,000,000 by 1?s.  Use the modulo operator to determine when the count is a multiple of 100,000,000 and print that value on the screen. 

 

 

This is what I have however it is only printing 00:

#include "stdio.h"

void main()
{
	
	int count = 0;
	
	do
	{
		printf("%d\n" , count);
		if (count % 100,000,000 == 0)
			{
				printf("%d", count);
				printf("\n");
				count++;
				continue;
				
				
			}
		
		count++;

	}while (count <= 1,000,000,000);
}	

And here is the printout: 

 

Any ideas why its not printing out the multiples of 100,000,000?

post-505691-0-34743000-1379971455.png

Link to comment
Share on other sites

13 answers to this question

Recommended Posts

  • 0

Why do you have commas in your numbers? "100,000,000" should be "100000000" and "1,000,000,000" should be "1000000000". In fact, I'm a little surprised your compiler didn't flag that for you. I tried compiling your code with Clang 3.0 and it gave me the following diagnostic information:

rar.c:3:1: error: 'main' must return 'int'
void main()
^
rar.c:11:13: warning: expression result unused [-Wunused-value]
                if (count % 100,000,000 == 0)
                    ~~~~~ ^ ~~~
rar.c:23:16: warning: expression result unused [-Wunused-value]
        }while (count <= 1,000,000,000);
                ~~~~~ ^  ~
2 warnings and 1 error generated.
  • Like 1
Link to comment
Share on other sites

  • 0

Also, you could just remove the count++ and continue instructions inside your if statement, and your program would logically do the same thing.

 

I don't think the assignment wants you to print every single value to the console, only the ones that are multiples of 100000000.

  • Like 2
Link to comment
Share on other sites

  • 0

Also, you could just remove the count++ and continue instructions inside your if statement, and your program would logically do the same thing.

 

I don't think the assignment wants you to print every single value to the console, only the ones that are multiples of 100000000.

 

Actually, he's going to skip a number every time he enters the conditional...

Link to comment
Share on other sites

  • 0

Actually, he's going to skip a number every time he enters the conditional...

 

Nuh uh! The count++ at the bottom of the loop is skipped because of the "continue" statement.

Link to comment
Share on other sites

  • 0

I just noticed that you are going to overrun the precision of int. The int type is guaranteed by the C standard to be at least 16 bits, which means the largest number it can hold is 32767 (two's compliment). You should be using long, which is guaranteed to be at least 32 bits (2,147,483,647 two's compliment), or unsigned long (4,294,967,295).

Link to comment
Share on other sites

  • 0

int will be 32-bit on Windows, unless (perhaps?) you're compiling in 64-bit mode. If there's a concern for portability, int32_t from stdint.h should be used instead.

 

Per the C standard int is required to be at least 16 bits, so it may actually be more on some platforms or with some compilers. However it is never too early to enforce good practice, such as not to relying on implementation-specific or platform-specific behavior. You don't want to end up with problems like the one discussed in this thread by relying on a specific implementation rather than following the standard.

Link to comment
Share on other sites

  • 0

Per the C standard int is required to be at least 16 bits, so it may actually be more on some platforms or with some compilers. However it is never too early to enforce good practice, such as not to relying on implementation-specific or platform-specific behavior. You don't want to end up with problems like the one discussed in this thread by relying on a specific implementation rather than following the standard.

Well, perhaps when while loops aren't fully understood is a bit early to talk about potential portability issues. ;) 

Link to comment
Share on other sites

  • 0

Nuh uh! The count++ at the bottom of the loop is skipped because of the "continue" statement.

 

Now y'see... This is why people shouldn't try to help others when falling asleep... How embarrassing...  :blush:

  • Like 1
Link to comment
Share on other sites

  • 0

Thank you for explaining to me that the commas aren't recognized.  I'm using Microsoft Visual Studio to do all of this in and it wasn't flagging me.  I thought that I needed to put it in there for readability, not understanding that it was messing up what I was trying to do. After removing the commas it worked flawlessly.  As for over running int by not making it a long, I didn't need to change it to long int before it would work, but thank you for addressing that as it could have been another error in the future.  So I went back and changed it just in case.  You all were extremely helpful!  Thanks again!

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.