Hi! This is a real simple and stupid exercise I thought I knew how to handle. The complete heading is:
- Write a function in C whose input is a real number and whose output is the absolute value of the input number.
The following is the code I've developed. It compiles and runs but doesn't do what it is supposed to.
If anyone can point where the error is I would be very grateful.

Thank you in advance.

#include <stdio.h>
#include <math.h>
double outputAbsoluteValue (double *number){
	*number = fabs(*number);
	return *number;
}
void main(){
	
	double myNumber=0.0;
	double absValue=0.0;
	printf("Enter Real Number: ");
	scanf("%f", &myNumber);
	absValue=outputAbsoluteValue(&myNumber);
	printf("\n Its absolute value is %f", absValue);
	fflush(stdin);
	getchar();
	
}

Edited 5 Years Ago by o0OPatriciaO0o: n/a

Well what's it supposed to do? Can you give us an example of how its failing?

Oh by the way, don't use fflush(stdin) its behavior is undefined.

Edited 5 Years Ago by gerard4143: n/a

Try this:

#include <stdio.h>
#include <math.h>
 
double outputAbsoluteValue (double*number){
        *number = fabs(*number);
        return *number;
}
int main(void){
        
        double myNumber = 0.0;
        double absValue = 0.0;
        
        printf("Enter Real Number: ");
        scanf("%lf", &myNumber);
        
        absValue=outputAbsoluteValue(&myNumber);
        
        printf("\n Its absolute value is %f", absValue);
        
        getchar(); 
        return 0;

        
}

1) int main(void)

2) %lf as the format specifier.(or declare variables as float and use %f)

3) Do not use fflush(stdin) as Gerard4143 already mentioned.

Edited 5 Years Ago by myk45: n/a

Comments
What grade are you going to get on HIS homework?

@WaltP

i just added two words and a character. (int, void and l). He had posted the code already!

Thank you myk45 and Gerard4143 for you help :)
It works now as it should.
Just one little question, what does %lf do? I don't think I've seen it before.
Thank you again!

Edited 5 Years Ago by o0OPatriciaO0o: n/a

This question has already been answered. Start a new discussion instead.