Dean
Dean

Reputation: 11

C function calculation issue where i either get an error or wont perform the calculation in my function

guys I am currently having a problem with my code. My code is for converting delta to star conversion, star to delta and quit. However this isnt important just a bit of background. So for the code the calculations all have to be done in one function, and for some reason when I ask the user to enter a value for R1,R2 and R3 and then try to pass that through my function the calculation isnt performed, with the values the user inputted. What I have commented out is not important as these bits work fine

Below is the part of the code i need help with

float conversions (float RA, float R1, float R2, float R3)
{
    //Star to Delta conversion

    RA = ((R1*R2+R2*R3+R3*R1)/R3);
    
}


int main(void){
    float RA,R1,R2,R3;
    
    //printf("Please enter an S to convert Star to Delta, a D to convert Delta to Star, and Q to quit ");
    //char x = UserInput();
    //printf ("You selected %c\n", x);
    
    
    printf("Please enter a value for R1 R2 and R3 seperated by a space: ");
    scanf("%f %f %f", &R1, &R2, &R3);
    
    printf("%f %f %f", R1, R2, R3);  // test to ensure values were being passed to variables

    conversions(R1,R2,R3,RA);
    printf("%f", RA);
}

Upvotes: 0

Views: 53

Answers (2)

Jabberwocky
Jabberwocky

Reputation: 50775

You probably want this:

float conversions (float R1, float R2, float R3)
{
    //Star to Delta conversion
    return (R1*R2+R2*R3+R3*R1)/R3;
}

and call it like this:

RA = conversions(R1,R2,R3);

or maybe this:

void conversions (float R1, float R2, float R3, float *RA)
{
    //Star to Delta conversion
    *RA = ((R1*R2+R2*R3+R3*R1)/R3);    
}

and call it like this:

conversions(R1,R2,R3, &RA);

Upvotes: 1

MED LDN
MED LDN

Reputation: 669

This code can help you

float conversions (float R1,float R2, float R3)
{
  //Star to Delta conversion
  return (R1*R2+R2*R3+R3*R1)/R3;
}


int main(void){
float RA,R1,R2,R3;

//printf("Please enter an S to convert Star to Delta, a D to convert Delta to Star, and Q to quit ");
//char x = UserInput();
//printf ("You selected %c\n", x);


printf("Please enter a value for R1 R2 and R3 seperated by a space: ");
scanf("%f %f %f", &R1, &R2, &R3);

printf("%f %f %f", R1, R2, R3);  // test to ensure values were being passed to variables

printf("\n%f",conversions(R1,R2,R3));
}

Upvotes: 1

Related Questions