prime number detector program

This is a discussion on prime number detector program within the C Programming forums, part of the General Programming Boards category; what is wrong with my program that i cant run smoothly. there is an error in this line result = ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    5

    prime number detector program

    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
    Attached Files Attached Files

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    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)

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    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 10:41 PM. Reason: Added code tags

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    user_entry is a pointer and it doesn't point to anything...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    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"

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,305
    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:
    Code:
    			if(((*user_entry)+1) / i == 0)
    			{
    				return false;
    			}
    Also:
    Code:
    //result weather its a prime number or a prime number
    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.
    Last edited by iMalc; 01-23-2009 at 01:51 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 12:44 AM
  2. prime number.
    By tdoctasuess in forum C Programming
    Replies: 13
    Last Post: 05-13-2004, 08:03 AM
  3. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. Replies: 3
    Last Post: 01-14-2003, 09:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21