Sign in to follow this  
Followers 0
Terabojin

problem with while loop?

14 posts in this topic

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

Share this post


Link to post
Share on other sites

When writing code, you don't put commas in your numbers.  Try that...

Share this post


Link to post
Share on other sites

.. Does C even recognize commas in numbers?  It's been like, 20 years since I did anything in it...

Share this post


Link to post
Share on other sites
#include <stdio.h>

Edit: That's not the problem. The problem has been stated already. This is just another thing you should be doing.

Share this post


Link to post
Share on other sites

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.
1 person likes this

Share this post


Link to post
Share on other sites

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.

2 people like this

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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).

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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. ;) 

1 person likes this

Share this post


Link to post
Share on other sites

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:

1 person likes this

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.