-
Max Min
Can any one help wiht this project? The code is to evaluate two numbers and find the max and min of those numbers using functions. I am new to C proramming and have been stumped on this.
Code:
#include <stdio.h>
#include <stdlib.h>
float maximum(float, float);
float minimum(float, float);
void prn_info (void);
main()
{
//variables
float first_numb = 0.0;
float second_numb = 0.0;
int validate;
//input
printf("Enter First Number: ");
validate =scanf("%f", &first_numb);
if (validate==0)
printf("\nInvalid number\n");
else
printf("\nNumber accepted\n");
printf("Enter Second Number: ");
validate =scanf("%f", &second_numb);
if (validate==0)
printf("\nInvalid number\n");
else
printf("\nNumber accepted\n");
}
float maximum(float first_numb, float second_numb)
float minimum(float first_numb, float second_numb)
{
if
return first_numb < second_numb;
printf("\nThe Maximum number is %f", second_numb);
printf("\nThe Minimum number is %f", first_numb));
else
return first_numb < second_numb;
printf("\nThe Maximum number is %f", first_numb);
printf("\nThe Minimum number is %f", second_numb));
getch();
}
thanks
-
> float maximum(float first_numb, float second_numb)
> float minimum(float first_numb, float second_numb)
You're trying to declare both functions at the same time.
Define them separately
Code:
float maximum(float first_numb, float second_numb) {
// some code
}
float minimum(float first_numb, float second_numb) {
// some code
}
-
Max Min
Ok that makes sense, but I am still not geting the idea I guess. This is how I recoded. I am still unsure about the //some code mentioned in last post and how the function is called.
Code:
if
{printf("\nMax is %f and Min is %f", maximum(second_numb, first_numb))}
else
{printf("\nMax is %f and Min is %f", minimum(first_numb, second_numb))}
getch();
}
float maximum(float first_numb, float second_numb)
return first_numb < second_numb;
float minimum(float first_numb, float second_numb)
{
return first_numb > second_numb;
}
-
You are calling it right, but you need braces enclosing a function definition, like you did around minimum(). You didn't do so on maximum():
Code:
> float maximum(float first_numb, float second_numb)
>
> return first_numb < second_numb;
There you need braces at the start and end of the function.
-
Max Min
What is the logic behind the Max Min functions? Are they supposed to perform a calculation and return the value to the main function? Please see example code.
thanks
Code:
printf("\nMax is %f", maximum(first_numb, second_numb));
printf("\nMin is %f", minimum(first_numb, second_numb));
getch();
}
float maximum(float first_numb, float second_numb)
{if (first_numb < second_numb);
return second_numb;}
{else return first_numb;}
float minimum(float first_numb, float second_numb)
{if (first_numb > second_numb);
return first_numb;}
{else return second_numb; }
-
yes the functions do whatever things you design them to -
in this case compare two numbers, and the do return a value
to main, which you have set up to be printed on screen. thats
all fine, but what you need is to develop a style that helps you
find errors. i've taken your last post, i've done nothing to the
actual code whatsoever and instead just displayed it better
and you should be able to see that there is a problem with the
braces in your functions, although they match up, the else
statements are not in the functions.
Code:
printf("\nMax is %f", maximum(first_numb, second_numb));
printf("\nMin is %f", minimum(first_numb, second_numb));
getch();
}
float maximum(float first_numb, float second_numb)
{
if (first_numb < second_numb);
return second_numb;
}
{
else return first_numb;
}
float minimum(float first_numb, float second_numb)
{
if (first_numb > second_numb);
return first_numb;
}
{
else return second_numb;
}
also, you don't need the semicolons after the if statements, you
could put the return statement in braces, but it is not necessary
because the if statement only executes the return, you would
need braces definitely if you wanted to run more than one line of
code for the if statement. just thought you should know
-
Thank you so much....it does make it easier looking at it the way you have it posted.