what is wrong with my program that i cant run smoothly. there is an error in this line
result = prime_num(&user_entry); //prime number detector function
what is wrong with my program that i cant run smoothly. there is an error in this line
result = prime_num(&user_entry); //prime number detector function
1. It's not recursive
2. Why not paste it here? It's only ~40 lines
3. You only need to check that it's not divisible from 2 to sqrt(*user_entry) + 1
4. result is bool don't try and print it using %d (see your printf)
this is my edited codes
Code:#include<stdio.h> #include<stdlib.h> bool prime_num (int *user_entry); //function prototype int main(void) { int *user_entry = 0; //users input integer bool result; //result weather its a prime number or a prime number printf("enter an integer: "); scanf("%d", user_entry); //request user for input result = prime_num(&user_entry); //prime number detector function if(result == 0) { printf("\n\n%d is a prime number is: ", user_entry); //if prime number print this } else { printf("\n\n%d is not a prime number", user_entry); //if not a prime number print this } return 0; //end of program } bool prime_num (int *user_entry) //prime number detector function { if(*user_entry == 0 || *user_entry == 1) { return false; } for(int i = 2; i < *user_entry; i++) //recursive prime number testing { if(((*user_entry)+1) / i == 0) { return false; } if(*user_entry % i == 0) { return false; } } return true; } //end of function
Last edited by Salem; 01-22-2009 at 11:41 PM. Reason: Added code tags
user_entry is a pointer and it doesn't point to anything...
Please re-edit your post to include [code][/code] tags, read the FAQ for furthur information.
For now all I'll say is that you want "whether", not "weather" in your comment.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
user_entry should be int, not int*
don't you pay attention to warnings? - you are passing wrong type to prime_num function
your prime_num function does not need to receive pointer to int - it does not modify the argument - so you should pass just int
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Thanks for doing that - much better!
As many before you have done, you've misunderstood what it commonly means when a function takes a pointer as one of its [out] parameters.
Usually (and in this case), it means that you need to pass it a pointer to something. You're just declaring an uninitialised pointer (a pointer that doesn't point to anything) and giving it that. The function is none-the-wiser, and will simply try and put the data you've requested wherever that pointer says to put it. This is why it is crashing. What you're supposed to do is just pass the address of something, by putting the & symbol in front of a variable. The address of something is a pointer to that thing. The data will then go right into that thing.
This advice applies to your use of scanf.
I'm also puzzled as to what this part is supposed to be for:Also:Code:if(((*user_entry)+1) / i == 0) { return false; }Aside from what I've mentioned earlier, you should re-read that entire comment. I think "composite" is what you want in one of those places. Remember that if this is an assignment, then you will give a bad impression to your grader if you have such poorly written comments. Whilst they may not be supposed to grade you on the comments, in practice it usually weights on the grade you'll receive.Code://result weather its a prime number or a prime number
Last edited by iMalc; 01-23-2009 at 02:51 AM.