user1728737
user1728737

Reputation: 59

How can I properly print the function call?

It comes out as 0.0. I want it to come out as a number other than 0, unless the input number was 0 of course. I tried a few things. Here is the current code.

#include <stdio.h>                                     /* Necessary header */
#include <stdlib.h>


int main()
{
    double Initial;
    double Post;

    printf("Enter a number with a decimal: ");
    scanf("%lf", &Initial);
    printf("Enter another number using the same format: ");
    scanf("%lf", &Post);

    ComputeMinimum(Initial, Post);
    ComputeMaximum(Initial, Post);

    printf("Of %1.1lf and %1.1lf ", Initial, Post);
    printf("the minimum is %1.1lf ", ComputeMinimum(Initial, Post));
    printf("and the maximum is %1.1lf.", ComputeMaximum(Initial, Post));

    return 0;
}

    double ComputeMaximum(double B, double A)
{
    return (A > B) ? A : B;
}

double ComputeMinimum(double a, double b)
{
    return (a < b) ? a : b;
}

I have already tried the following. ALso, I am supposed to make the return type for the functions double, not sure how though.

int main()
{
    double Initial;
    double Post;

    printf("Enter a number with a decimal: ");
    scanf("%lf", &Initial);
    printf("Enter another number using the same format: ");
    scanf("%lf", &Post);

    double minimum = ComputeMinimum(Initial, Post);
    double maximum = ComputeMaximum(Initial, Post);

    printf("Of %1.1lf and %1.1lf ", Initial, Post);
    printf("the minimum is %1.1lf ", minimum);
    printf("and the maximum is %1.1lf.", maximum);

    return 0;
}

Upvotes: 0

Views: 141

Answers (2)

For me also it worked i.e. the above program in which you are catching the output in maximum and minimum as double. I think above mention of declaring the prototype is also correect or you can directly put the definition of the both the function above the main program, then in that case you don't even need prototype since it will work as prototype for the compiler as well

Upvotes: 0

Piotr Zierhoffer
Piotr Zierhoffer

Reputation: 5139

I don't know, it works perfectly fine for me.

You should add function declarations before main, so it gets:

#include <stdio.h>                                     /* Necessary header */
#include <stdlib.h>

double ComputeMinimum(double a, double b);
double ComputeMaximum(double a, double b);

int main()
{
....

As for your next question, the return type of these functions IS double, so you don't have to change anything. The problem was that without the function prototypes a compiler does not know it will be double, so it assumes int.

Please enable compiler warnings, they are really helpful and you should always read them carefully.

Upvotes: 1

Related Questions