Thread: c programming function. Finding next value

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    38

    Lightbulb c programming function. Finding next value

    I've written up some code to find out if a value entered is a prime number. I have now extended this piece of code by adding another function to find the next prime number. I'm having a lot of trouble getting it working, mainly (i think) because I either can't get it to stop once it has found the next one, or, it's just printing the value+1 as the next value (even though it might not necessarily be prime).

    My code is as follows:

    Code:
    #include <stdio.h>#include <stdlib.h>
    #include <math.h>
    
    
    int isperfect(int);
    int nextperfect(int);
    
    
    int
    main(int argc, char **argv) {
    	int value, sum, nextvalue;
    	printf("Enter two values: ");
    	
    	while(scanf("%d", &value) == 1){
    		if(value<0){
    			printf("Error, negative value!");
    			exit(EXIT_FAILURE);
    		}
    		
    		sum = isperfect(value);
    		if (sum == value){
    			printf("The value %d is perfect.\n", value);
    		}
    		nextvalue = nextperfect(value);
    		printf("The next perfect value is %d.\n\n", nextvalue);
    	}
    	return 0;
    }
    
    
    int
    isperfect(int value){
    	int divisor, sum = 0;
    	
    	for(divisor=1; divisor<value; divisor++){
    		if(value%divisor == 0){
    			sum = sum+divisor;
    			printf("The divisor value is = %d, and the value is %d\n", divisor, value);
    		}
    	}
    		
    	return sum;
    }
    
    
    int
    nextperfect(int value){
    	int nextvalue, sum, nextfound;
    	
    	for(nextvalue = value+1; nextfound == 0 ; nextvalue++){
    		sum = isperfect(nextvalue);
    		if (sum == nextvalue){
    			nextfound = 1;
    		}
    	}
    	
    	return nextvalue;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > int nextvalue, sum, nextfound;
    ...
    > for(nextvalue = value+1; nextfound == 0 ; nextvalue++)

    What is the initial value of nextfound?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by Tigertan View Post
    I've written up some code to find out if a value entered is a prime number.
    No, you've written code to find out if a value is a perfect number.

    Bye, Andreas

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    38
    Quote Originally Posted by AndiPersti View Post
    No, you've written code to find out if a value is a perfect number.

    Bye, Andreas
    Ahh sorry, yes I meant perfect number! I've been writing up all sorts of programs over the past few days!


    Quote Originally Posted by Salem View Post
    > int nextvalue, sum, nextfound;
    ...
    > for(nextvalue = value+1; nextfound == 0 ; nextvalue++)

    What is the initial value of nextfound?
    Why do i need to initialize nextfound?? That's just my guard.. isn't it okay like that?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Local variables that have not been assigned a value, just have a random value. It's unlikely that nextfound would happen to be equal to 0.

    Global int variables ARE assigned the value of zero, when they're created.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding how a function works..
    By sdbuilt in forum C Programming
    Replies: 7
    Last Post: 09-28-2012, 12:34 PM
  2. Replies: 6
    Last Post: 11-04-2010, 12:30 PM
  3. Compiler not finding my function
    By pilotmm in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 04:43 PM
  4. Finding the Minimum using a function
    By BB89 in forum C++ Programming
    Replies: 7
    Last Post: 02-28-2010, 04:36 PM
  5. Usefull function finding form
    By Yarin in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 11-30-2007, 10:29 PM