Issues with segmentation fault

This is a discussion on Issues with segmentation fault within the C Programming forums, part of the General Programming Boards category; Hi I am currently running into a problem that is confusing. The program takes two values, a base and an ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    Issues with segmentation fault

    Hi

    I am currently running into a problem that is confusing.
    The program takes two values, a base and an exponent. It calculates the number. It seems to work fine except when I input a negative value for the exponent; the program will stop and state segmentation fault. Any help would be awesome. Thanks in advance.


    Code:
    #include <stdio.h>
    #include <math.h>
    
    
    
    
    void run(void);
    float getnumb2(void);
    float getnumb1(void);
    float power( float semisol, float number, float exponent );
    
    int main(){
    
    run();
    
    
    
    return (0);
    }
    
    void run(void){
    float x, y, answer;
    printf("\nThis program will take a number and an exponent. For example, the program will preform x to the power of y.\n");
    x=getnumb1();//gets the first the number; the base
    y=getnumb2();//gets the exponent
    answer=power(x, x, y);//a recursive function that solves the equation// 
    
    printf("\n%f to the power of %f is %f\n\n\n",x,y,answer);
    }
    
    float getnumb1(void){//gets the first the number; the base
    float x;
    printf("\nPlease enter a value for x(the base): ");
    scanf("%f",&x);
    return x;
    }
    
    float getnumb2(void){//gets the exponent
    float y;
    printf("\nPlease enter a value for y(the exponent): ");
    scanf("%f",&y);
    return y;
    }
    
    float power(float semisol, float number, float exponent){//a recursive function that solves the equation/ x to the power of y or 'number' to the power of 'to'
    
    if (exponent  == 0){
    semisol=1;
    return semisol;
    }
    else if (exponent == 1){
    return semisol;
    }
    else if (exponent > 1){
    semisol= semisol * number;
    exponent = exponent - 1;
    semisol=power(semisol, number,  exponent);
    return semisol;
    }
    else if(exponent < -1){
    semisol= 1 / (semisol * number);
    semisol = power(semisol, number, exponent);
    return semisol;
    }
    else if(exponent == -1){
    return semisol;
    }
    
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    Never mind, I think I figured it out.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    else if(exponent < -1){
    semisol= 1 / (semisol * number);
    semisol = power(semisol, number, exponent);
    return semisol;
    }
    Never ends.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    First of all, fix your indentation because as is the posted code is very hard to read.
    For negative exponents, add one to the exponent before calling power() recursively.
    Code:
    else if (exponent < -1) {
        semisol= 1 / (semisol * number);
        semisol = power(semisol, number, exponent + 1);
        return semisol;
    }

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    11
    Thanks, it works better. Now, I just have to figure out the math part of it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 06:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 01:53 PM

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