I cant figure out why my program is crashing. Please Help!!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dicky2141
    New Member
    • Mar 2010
    • 1

    I cant figure out why my program is crashing. Please Help!!!

    Code:
    #include <stdio.h> 
    #include <stdlib.h>
    
    void separate(int *first, int *second, int *third);
    
    int main() { 
    
    	int number;
    	int A, B, C;	
    	
    	printf("Enter a positive integer value to be evaluated: ");
    	scanf(" %d", &number);
    	
    	A = number + 0;
    	B = number + 0;
    	C = number + 0;
    	
    	printf("\nThe integer value you've chosen is %d\n\n", number);
    	
    	separate(&A, &B, &C);
    	
    	printf("The value of A is %d\n\n", A);
    	printf("The value of B is %d\n\n", B);
    	printf("The value of C is %d\n\n", C);
    	
    		system("pause");
      
        return 0; 
        
    } 
    
    void separate(int *first, int *second, int *third) {
    	
    	int i, j;
    	int input, sum = 0, total_sum = 0;
      
    	if (*first % 7 == 0 || *first % 11 == 0 || *first % 13 == 0) {
    		*first = 1;
    	}
    	else {
    		*first = 0;
    	}
    	
    	
    	for (i = *third; i > 1; i--) {
    		if (*third % i == 0) {
    			*third = 0;
    		}
    		else *third = 1;
    	} 
    	
    // The problem is somewhere for here on
    //Im trying to add the digits of my imputed variable and return them to my main
    //function.	
    	for (input = *second; input <= 1000; input++) {
    		sum = 0;
    		
    		for (j=0; j<4; j++) {
    		sum = sum + (input / (j*10))%(10) ;
    		}
    		
    		total_sum = total_sum + sum;
    	}
    	
    	*second = total_sum;
    
    }
    Last edited by Banfa; Mar 11 '10, 09:29 AM. Reason: Added [code]...[/code] tags
  • kiruthikaU
    New Member
    • Feb 2010
    • 4

    #2
    The problem in your code is ,
    Code:
    for (input = *second; input <= 1000; input++) {
    sum = 0;
    for (j=0; j<=4; j++) {
    sum = sum + (input / (j*10))%(10) ;
    }
    In the inner for loop j value starts from zero.
    And inside the loop you are performing the operation,
    sum = sum + (input / (j*10))%(10) ;
    If j value is zero j*10 will be (0*10=>0).
    So it will give divide by zero error (i.e) floating point exception.

    So you start the j value from one .

    Comment

    Working...