|
 |

November 6th, 2009, 05:18 AM
|
 |
The Helper Connoisseur, SC2 Galaxy Editor Newbie
|
|
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
|
|
|
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.
|

November 6th, 2009, 07:12 AM
|
 |
Son, I am disappoint.
|
|
Join Date: May 2006
Location: Romania
Posts: 4,522
|
|
First of all, it is supposed to be
Code:
if (is_prime == true)
Which you can anyway put directly as
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.
|

November 6th, 2009, 07:29 AM
|
 |
The Helper Connoisseur, SC2 Galaxy Editor Newbie
|
|
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
|
|
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.
|

November 6th, 2009, 08:12 AM
|
 |
Son, I am disappoint.
|
|
Join Date: May 2006
Location: Romania
Posts: 4,522
|
|
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.
|

November 6th, 2009, 10:12 AM
|
 |
The Helper Connoisseur, SC2 Galaxy Editor Newbie
|
|
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
|
|
|
Thank you. (Too many Visual Studio 2008 documentations...by the way.)
|

November 6th, 2009, 12:12 PM
|
 |
Junior Member (Am I really helping anyone?)
|
|
Join Date: Apr 2009
Posts: 248
|
|
|
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.
|

November 6th, 2009, 06:30 PM
|
 |
The Helper Connoisseur, SC2 Galaxy Editor Newbie
|
|
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
|
|
|
Thanks for the alternative.
|

November 7th, 2009, 02:19 AM
|
 |
Son, I am disappoint.
|
|
Join Date: May 2006
Location: Romania
Posts: 4,522
|
|
Quote:
Originally Posted by Xienoph
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.
|

November 7th, 2009, 04:40 PM
|
 |
Junior Member (Am I really helping anyone?)
|
|
Join Date: Apr 2009
Posts: 248
|
|
|
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.
|

November 7th, 2009, 07:38 PM
|
 |
The Helper Connoisseur, SC2 Galaxy Editor Newbie
|
|
Join Date: May 2007
Location: Taipei, Taiwan
Posts: 3,981
|
|
|
Don't worry. I finally got it, thanks to both of you.
|

November 8th, 2009, 05:26 AM
|
 |
Son, I am disappoint.
|
|
Join Date: May 2006
Location: Romania
Posts: 4,522
|
|
Quote:
Originally Posted by Xienoph
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.
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|