Another beginning function

This is a discussion on Another beginning function within the C Programming forums, part of the General Programming Boards category; Hi: Ran into another problem in studying my programing. This program is designed to compute a number of primes. Instead ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    68

    Another beginning function

    Hi:

    Ran into another problem in studying my programing. This program is designed to compute a number of primes. Instead of computing any primes, however, it throws a segmentation block. I suspect that it may because I am returning 1 and 0 instead of true and false, but I did that because when I return true and false I get these errors.

    ThousandthPrime.c:10:10: error: ‘false’ undeclared (first use in this function)
    ThousandthPrime.c:10:10: note: each undeclared identifier is reported only once for each function it appears in
    ThousandthPrime.c:20:9: error: ‘true’ undeclared (first use in this functio

    Code:
    //ThousandthPrime:  Calculate the thousandthprime.
    #include <stdio.h>
    //primality function which will determine if a number is prime.
    long Primality(long n)
    {
    //check to see if the number is a multiple of 2(ie the number is even) and is not equal to 2
    	if(n % 0 && n != 2)
    	{
    		return 0;  //if the number is even or equal to 2, return false and do not count these numbers
    	}//end if
    //once we determine that a number is not even or equal to 2, we can check the odds.
    	for(long i = 3; i * i <= n; i += 2) 
    	{
    		if(n % i == 0)
    		{
    			return 0;
    		}//end if
    	}//end for
    	return 1;
    }//end function primality
    
    
    int main()
    {
    	long primes = 0;// counts the number of primes for us.
    	long n = 1; //variable that we will run through primality function
    	long y = 0; //variable input by the user to tell how many primes to find.
    
    
    	printf("Please enter a number(n), and the program will return with the \"nth\" number of primes");
    	scanf("%ld", y);
    
    
    	//while loop calls the primality functions until "y"  primes are found
    	while(primes < y)
    	{
    		n++;
    		Primality(n);
    		if(Primality(n))
    		{
    			printf("%ld", n);
    			primes++;
    		}//end if
    	}//end while
    
    
    	printf("I found %ld primes %ld is the \"nth\" prime", primes, n);
    	
    }//end main
    Code:
    Please enter a number(n), and the program will return with the "nth" number of primes:  3
    Segmentation fault (core dumped)
    What am I doing wrong?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,837
    You may want to try to increase your compiler warning level and then fix these warnings.
    ||=== c_homework, Debug ===|
    main.c|4|warning: no previous declaration for ‘Primality’ [-Wmissing-declarations]|
    main.c||In function ‘Primality’:|
    main.c|7|warning: division by zero [-Wdiv-by-zero]|
    main.c||In function ‘main’:|
    main.c|31|warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]|
    ||=== Build finished: 0 errors, 3 warnings ===|
    Jim

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Quote Originally Posted by jimblumberg View Post
    You may want to try to increase your compiler warning level and then fix these warnings.


    Jim

    And how would I do these things?

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Also, I don't get:

    main.c|31|warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]|

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    scanf() requires a memory location (address) to store the value of what it reads/converts. When you use the name of a variable that is an integer or float type, the name is evaluated for the value it holds. So you are passing y to scanf which is 0 and scanf expects an a valid address. So, you need to use the address operator '&' when a function expects an address, namely &y.

    Your other big problem is
    Code:
    if( n % 0 ... )
    Mod is derived from doing division and you cannot of course divide by zero.

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    But that statement says
    Code:
    if (n % i == 0)

  7. #7
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    How can I return true/false without getting the error?

  8. #8
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Look at line 7.

    edit:

    You are not getting the error from the return value, however, you should probably define your function so that it returns 'int' instead of 'long'. If you insist on long, then you should append your constants with 'L', because constant integers are by default of type 'int'. I would change the return value to int though.
    Last edited by Tclausex; 02-04-2013 at 05:22 PM.

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Line 8 on my editor, but I mistyped the first function. Thanks.

    Code:
    ThousandthPrime.c: In function ‘Primality’:
    ThousandthPrime.c:10:10: error: ‘false’ undeclared (first use in this function)
    ThousandthPrime.c:10:10: note: each undeclared identifier is reported only once for each function it appears in
    ThousandthPrime.c:17:11: error: ‘true’ undeclared (first use in this function)
    ThousandthPrime.c: In function ‘main’:
    ThousandthPrime.c:30:2: warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]
    ThousandthPrime.c:33:15: warning: comparison between pointer and integer [enabled by default]

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    It's perfectly fine to return 0 or 1 if your compiler doesn't support bool.
    That just leaves you with looking up how to use scanf properly. I'm sure there are millions of examples on the internet.

    Once you've done that, post what should hopefully be your working code.
    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"

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    1,209
    Quote Originally Posted by jwall View Post
    How can I return true/false without getting the error?
    To use true and false, you need to declare them with

    Code:
    #include <stdbool.h>

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    6,454
    I suspect the OP is confused because the strings true and false are within single-line comments, not within code (statements or declarations).

    The cause of the error therefore depends on the age of the compiler.

    Older C compilers do not support single line (starting with // ) style comments, and will interpret the comments as code. The solution to that is to use the traditional multi-line comment (starting with /* and ending with */ ).

    A rarer problem is that the editor (or IDE) being used to create the source files has embedded a newline or other character within the comment (presumably before the word "false" occurs) which causes the compiler to interpret subsequent text as code. The solution in that case is to remove the embedded character. The techniques to view such an embedded character and to remove it depend on the editor though.

    The scanf() call on line 31 also needs to be changed to "scanf("%ld", &y)". Adding that ampersand (which I have highlighted in red) will address the segmentation fault.
    Right 98% of the time, and don't care about the other 3%.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,993
    Quote Originally Posted by grumpy
    Older C compilers do not support single line (starting with // ) style comments, and will interpret the comments as code.
    Hmm... but in that case, even line 1 should contain an error.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Revised code
    Code:
    //ThousandthPrime:  Calculate the thousandthprime.
    #include <stdio.h>
    #include <stdbool.h>
    
    
    //primality function which will determine if a number is prime.
    long Primality(long n)
    {
    //check to see if the number is a multiple of 2(ie the number is even) and is not equal to 2
    	if(n % 2 == 0 && n != 2)
    	{
    		return false;  //if the number is even or equal to 2, return false and do not count these numbers
    	}//end if
    //once we determine that a number is not even or equal to 2, we can check the odds.
    	for(long i = 3; i * i <= n; i += 2) 
    	{
    		if(n % i == 0)
    		{
    			return true;
    		}//end if
    	}//end for
    	return false;
    }//end function primality
    
    
    int main()
    {
    	long primes = 0;// counts the number of primes for us.
    	long n = 1; //variable that we will run through primality function
    	long y = 0; //variable input by the user to tell how many primes to find.
    
    
    	printf("Please enter a number(n), and the program will return with the \"nth\" number of primes:  ");
    	scanf("%ld", y);
    
    
    	//while loop calls the primality functions until "y"  primes are found
    	while(primes < &y)
    	{
    		n++;
    		Primality(n);
    		if(Primality(n))
    		{
    			printf("%ld", n);
    			primes++;
    		}//end if
    	}//end while
    
    
    	printf("I found %ld primes %ld is the \"nth\" prime", primes, n);
    	
    }//end main
    Warnings I get--how do I increase my compiler warning level? I am using gcc on linux if it matters
    Code:
    ThousandthPrime.c: In function ‘main’:ThousandthPrime.c:32:2: warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]
    ThousandthPrime.c:35:15: warning: comparison between pointer and integer [enabled by default]
    This is what happens when I run my code.

    Code:
    Please enter a number(n), and the program will return with the "nth" number of primes:  9
    Segmentation fault (core dumped)

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    2,837
    You need to study the documentation for scanf(), and realize scanf() requires the address of the variable. Read thru the previous posts, several tell you how to fix this issue. This is probably one of the causes of your segmentation fault. Although I show one more error:
    ||=== c_homework, Debug ===|
    main.c|7|warning: no previous declaration for ‘Primality’ [-Wmissing-declarations]|
    main.c||In function ‘main’:|
    main.c|34|warning: format ‘%ld’ expects argument of type ‘long int *’, but argument 2 has type ‘long int’ [-Wformat]|
    main.c|38|error: comparison between pointer and integer|
    ||=== Build finished: 1 errors, 2 warnings ===|

    Warnings I get--how do I increase my compiler warning level? I am using gcc on linux if it matters
    How are you compiling your code now? Are you using an IDE or are you compiling from the command line? If you compile from the command line show your compile line. Also this link may be useful in finding the correct command line compile options: gcc Warning options. If you look at the error/warning messages I posted you can some of the types of errors/warning switches I used, -Wformat, -Wmissing-declarations.


    Jim

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just Beginning
    By gamblingman in forum General Discussions
    Replies: 38
    Last Post: 10-11-2012, 01:19 AM
  2. how to return function to its beginning
    By ceddsoboss in forum C Programming
    Replies: 3
    Last Post: 10-05-2010, 08:48 PM
  3. Beginning ASM.
    By Krak in forum C++ Programming
    Replies: 4
    Last Post: 12-06-2004, 05:27 PM
  4. beginning C++
    By datainjector in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2002, 01:27 AM
  5. In the beginning...
    By CAP in forum Game Programming
    Replies: 21
    Last Post: 05-29-2002, 01:40 PM

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