Jump to content



Photo

problem with while loop?


  • Please log in to reply
13 replies to this topic

#1 Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 23 September 2013 - 21:24

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?

Attached Images

  • failed2-5.png



#2 Fahim S.

Fahim S.

    Neowinian Senior

  • Tech Issues Solved: 2
  • Joined: 15-April 02
  • OS: Windows 8 - OG
  • Phone: Google Nexus 4 16GB by LG

Posted 23 September 2013 - 21:27

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



#3 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 23 September 2013 - 21:27

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



#4 Garnet H.

Garnet H.

    astropheed

  • Tech Issues Solved: 2
  • Joined: 08-December 11
  • Location: Sydney, AU

Posted 23 September 2013 - 21:29

#include <stdio.h>

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



#5 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 23 September 2013 - 21:29

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.


#6 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 23 September 2013 - 21:31

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.



#7 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 23 September 2013 - 21:34

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



#8 +Majesticmerc

Majesticmerc

    Resident Idealist

  • Tech Issues Solved: 7
  • Joined: 24-August 05
  • Location: United Kingdom
  • OS: Arch Linux / Win 7
  • Phone: HTC One X

Posted 23 September 2013 - 21:35

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.



#9 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 23 September 2013 - 21:44

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



#10 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 23 September 2013 - 22:43

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.



#11 +Karl L.

Karl L.

    xorangekiller

  • Tech Issues Solved: 15
  • Joined: 24-January 09
  • Location: Virginia, USA
  • OS: Debian Testing

Posted 23 September 2013 - 22:58

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.



#12 Andre S.

Andre S.

    Asik

  • Tech Issues Solved: 10
  • Joined: 26-October 05

Posted 23 September 2013 - 23:29

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



#13 FloatingFatMan

FloatingFatMan

    Resident Fat Dude

  • Tech Issues Solved: 1
  • Joined: 23-August 04
  • Location: UK

Posted 24 September 2013 - 05:25

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:



#14 OP Terabojin

Terabojin

    Neowinian

  • Joined: 16-September 13
  • Location: California

Posted 24 September 2013 - 16:16

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!





Click here to login or here to register to remove this ad, it's free!