The Helper Forums
The Helper offers free technical support for computer games,
computer hardware, and other computer software. Staffed
by volunteer tech support workers, The Helper is highly
regarded as a premium destination for those who are
experiencing technical problems with their computers.
Affiliate Sites
Warcraft 3 World Editor Tutorials - The Greatest Resource for the new Warcraft 3 Mapper
Silkroad Online Forums
Houston Microsoft Access, VBA and Visual Basic Programming
The source for NUON news, information, and games.
Griffonrawl Muay Thai and MMA
Warcraft 3 Modding Site.
Element Tower Defense by Karawasa.
Topaz Games.
Chernobyl Lost Riddles

Able Auto Glass specializes in Houston Auto Glass Replacement as well as windshield repair and replacement services.
Apex Steel Pipe - Buys and sells Used Steel Pipe.

Go Back   The Helper Forums > Technical Support Zone > Programming Forum

Reply
 
Thread Tools Display Modes
  #1  
Old November 6th, 2009, 05:18 AM
tom_mai78101's Avatar
tom_mai78101 tom_mai78101 is offline
The Helper Connoisseur, SC2 Galaxy Editor Newbie
 
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)
Telling if it's a prime number or not: Need a helping hand in C++

Here's the stubborn code:

Code:
#include <stdafx.h>
#include <windows.h>


int main(void)
{
	int number = NULL, PlaceHolder;
	bool is_prime = true;
	PlaceHolder = 2;
	printf("Enter any non-negative number: ");
	scanf("%d", &number); //I don't know if scanf_s is okay or not. They're all the same.
	while (PlaceHolder < number)
	{
		if (number % PlaceHolder == 0)
		{
			is_prime = true;
		}
		else
		{
			is_prime = false;
		}
		PlaceHolder += 1;
	}
	if (is_prime = true)
	{
		printf("%d is a prime number.", number);
	}
	else
	{
		printf("%d is not a prime number.", number);
	}
	Sleep(500);
	return 0;
	system("EXIT");
}
No matter what number I put in, it always comes up with a message saying that number is a prime number.

I need your help in this situation. I want to know why it's calling the "is a prime number" message. Any sort of comments are appreciated. Thanks in advance.
Reply With Quote
  #2  
Old November 6th, 2009, 07:12 AM
Smith_S9's Avatar
Smith_S9 Smith_S9 is offline
Son, I am disappoint.
 
Join Date: May 2006
Location: Romania
Posts: 4,522
Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)
First of all, it is supposed to be

Code:
if (is_prime == true)
Which you can anyway put directly as

Code:
if (is_prime)
because it's useless to compare it to a boolean value, since the variable is already a boolean.

Then, there are two flaws. First, isn't it supposed to become false when you find a number that can divide the number you want to determine as prime? You're setting it to true when you find such a number. Well, the number is not prime if he has any divisor other than 1 and the number itself.

And second, you should start with the is_prime as true and only turn it to false if you find a divisor, not turn it to true everytime the number isn't divided. Let's have an example:
number = 8
Iteration 1: 8%2 = 0 -> is_prime = false (assuming that you have already fixed the first flaw)
Iteration 2: 8%3 = 1 -> is_prime = true (which is wrong, because the number has already been determined as not prime. you should never set is_prime to true back; only to false)
Iteration 3: 8%4 = 0 -> is_prime = false
Iteration 4: 8%5 = 3 -> is_prime = true
Iteration 5: 8%6 = 2 -> is_prime = true
Iteration 6: 8%7 = 1 -> is_prime = true

Result: 8 is a prime number =].

The correct code is like this:

Code:
int main(void)
{
	int number = NULL, PlaceHolder;
	bool is_prime = true;
	PlaceHolder = 2;
	printf("Enter any non-negative number: ");
	scanf("%d", &number);
	while (PlaceHolder < number)
	{
		if (number % PlaceHolder == 0)
		{
			is_prime = false;
		}
		PlaceHolder += 1;
	}
	if (is_prime)
	{
		printf("%d is a prime number.", number);
	}
	else
	{
		printf("%d is not a prime number.", number);
	}
	Sleep(500);
	return 0;
	system("EXIT");
}
Everytime you have a problem in programming and you can't find the bug, run the program step by step, using the compiler's debug options, if any, or on paper. Most of the times, this will give you a clue about what's wrong.
__________________
Reply With Quote
  #3  
Old November 6th, 2009, 07:29 AM
tom_mai78101's Avatar
tom_mai78101 tom_mai78101 is offline
The Helper Connoisseur, SC2 Galaxy Editor Newbie
 
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)
Thanks.

But, when I was running it step by step, instead of leading me to a place where I can just type in a number, I was taken to a place where many lines of assembly and other unusual codes were located. I pressed F11 until I stepped out a lot of times...

Here's where I've been stepping into:

PracticeC++.cpp
printf.c
_file.c
printf.c
_file.c
mlock.c
_file.c
printf.c
_file.c
printf.c
_stfbuf.c
fileno.c
_stfbuf.c
isatty.c
_stdbuf.c
_file.c
_stdbuf.c
dbgheap.c
mlock.c
dbghook.c

...

The list goes on and on into the assembly language.


I don't how how to step into my program and stay in there in VS 2008 Pro. But, I think I can try it on a piece of paper.
Reply With Quote
  #4  
Old November 6th, 2009, 08:12 AM
Smith_S9's Avatar
Smith_S9 Smith_S9 is offline
Son, I am disappoint.
 
Join Date: May 2006
Location: Romania
Posts: 4,522
Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)
I am not familiar with C compilers, sorry . I have no idea how to debug your code with step by step with the compiler you use. I recommend you to read the documentation of that respective program.

However, with such simple pieces of code as this one, doing it manually, on paper, shouldn't be a problem.
__________________
Reply With Quote
  #5  
Old November 6th, 2009, 10:12 AM
tom_mai78101's Avatar
tom_mai78101 tom_mai78101 is offline
The Helper Connoisseur, SC2 Galaxy Editor Newbie
 
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)
Thank you. (Too many Visual Studio 2008 documentations...by the way.)
Reply With Quote
  #6  
Old November 6th, 2009, 12:12 PM
Xienoph's Avatar
Xienoph Xienoph is offline
Junior Member (Am I really helping anyone?)
 
Join Date: Apr 2009
Posts: 248
Xienoph is starting to make their name known (+75)
To exit the while loop, PlaceHolder >= number. To get this to happen for the first time, PlaceHolder == number, which means that number % PlaceHolder == 0, which means that is_prime is true.
Reply With Quote
  #7  
Old November 6th, 2009, 06:30 PM
tom_mai78101's Avatar
tom_mai78101 tom_mai78101 is offline
The Helper Connoisseur, SC2 Galaxy Editor Newbie
 
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)
Thanks for the alternative.
Reply With Quote
  #8  
Old November 7th, 2009, 02:19 AM
Smith_S9's Avatar
Smith_S9 Smith_S9 is offline
Son, I am disappoint.
 
Join Date: May 2006
Location: Romania
Posts: 4,522
Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)
Quote:
Originally Posted by Xienoph View Post
To exit the while loop, PlaceHolder >= number. To get this to happen for the first time, PlaceHolder == number, which means that number % PlaceHolder == 0, which means that is_prime is true.
Umm, yes, but isn't while a checking-first-doing-after loop (I don't know how they are officially called in english xD). At the moment number == PlaceHolder, the loop will exit anyway, the actions will no longer be executed.
__________________
Reply With Quote
  #9  
Old November 7th, 2009, 04:40 PM
Xienoph's Avatar
Xienoph Xienoph is offline
Junior Member (Am I really helping anyone?)
 
Join Date: Apr 2009
Posts: 248
Xienoph is starting to make their name known (+75)
Hm? I'm not saying that the loop will never terminate. I'm just explaining that if the code were to exit the loop after running through the loop once, the value is_prime is always set to true. That's why tom_mai keeps seeing prime numbers.
Reply With Quote
  #10  
Old November 7th, 2009, 07:38 PM
tom_mai78101's Avatar
tom_mai78101 tom_mai78101 is offline
The Helper Connoisseur, SC2 Galaxy Editor Newbie
 
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)tom_mai78101 is on their way up (+450)
Don't worry. I finally got it, thanks to both of you.
Reply With Quote
  #11  
Old November 8th, 2009, 05:26 AM
Smith_S9's Avatar
Smith_S9 Smith_S9 is offline
Son, I am disappoint.
 
Join Date: May 2006
Location: Romania
Posts: 4,522
Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)Smith_S9 has a reputation beyond repute (+1000)
Quote:
Originally Posted by Xienoph View Post
Hm? I'm not saying that the loop will never terminate. I'm just explaining that if the code were to exit the loop after running through the loop once, the value is_prime is always set to true. That's why tom_mai keeps seeing prime numbers.
I did not say that it never ends. I said that once the number == PlaceHolder, the program will not execute the functions inside the loop, because it's a while structure. It was another reason for tom_mai's wrong result, see my first post in this thread. The program I posted will work correctly and has the same condition as tom's.
__________________
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 11:19 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.