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();
	
}

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.

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.

commented: What grade are you going to get on HIS homework? -3

@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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.