Another beginning function

This is a discussion on Another beginning function within the C Programming forums, part of the General Programming Boards category; You did not fix your scanf() problem. As I said, scanf requires an the address of a variable. Line 34 ...

  1. #16
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    You did not fix your scanf() problem. As I said, scanf requires an the address of a variable. Line 34 of last post.

  2. #17
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Code:
    //Jeremiah A. Walker//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", n);
    			primes++;
    		}//end if
    	}//end while
    
    
    	printf("I found %ld primes %ld is the \"nth\" prime.\n", primes, n);
    	
    }//end main
    It works now, but its not giving me primes. Can't believe I had & in the wrong place...

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,207
    Quote Originally Posted by laserlight View Post
    Hmm... but in that case, even line 1 should contain an error.
    Indeed. The OP did not suggest the diagnostics listed were the only ones emitted by the compiler though.

    Given that the code in the first post only has the string "false" within a comment, and does not contain the string "true" at all, it is a fair bet that information provided is incomplete.
    Right 98% of the time, and don't care about the other 3%.

  4. #19
    Registered User
    Join Date
    Jan 2013
    Posts
    68
    Code:
    //Jeremiah A. Walker
    //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 false;
    		}//end if
    	}//end for
    	return true;
    }//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", n);
    			primes++;
    		}//end if
    	}//end while
    
    
    	printf("I found %ld primes %ld is the \"nth\" prime.\n", primes, n);
    	
    }//end main
    Works now. Thanks for all your help.

    Please enter a number(n), and the program will return with the "nth" number of primes: 100 2
    3
    5
    7
    11
    13
    17
    19
    23
    29
    31
    37
    41
    43
    47
    53
    59
    61
    67
    71
    73
    79
    83
    89
    97
    101
    103
    107
    109
    113
    127
    131
    137
    139
    149
    151
    157
    163
    167
    173
    179
    181
    191
    193
    197
    199
    211
    223
    227
    229
    233
    239
    241
    251
    257
    263
    269
    271
    277
    281
    283
    293
    307
    311
    313
    317
    331
    337
    347
    349
    353
    359
    367
    373
    379
    383
    389
    397
    401
    409
    419
    421
    431
    433
    439
    443
    449
    457
    461
    463
    467
    479
    487
    491
    499
    503
    509
    521
    523
    541
    I found 100 primes 541 is the "nth" prime.


Page 2 of 2 FirstFirst 12
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