prebek
asked on
finding the minimum of two numbers
I am trying to make a function that finds the minimum of two integers. I compuled the code below without errors but when I run the program I get a "bus error." Any one know what I did incorrectly?
#include <stdio.h>
double min(double x, double y);
int main ()
{
int numberone, numbertwo;
printf("Enter a number: ");
scanf("%d", numberone);
printf("\nEnter a another number: ");
scanf("%d", numbertwo);
printf("\nThe smaller of those two integers is %d", min(numberone, numbertwo));
return 0;
}
double min(double x, double y)
{
if (x < y)
y = x;
if (y < x)
x = y;
return x;
// I should be able to return either value since they will be the same by the end
// of the function
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
prebek, usually, when two experts post the same solution at about the same time, it's customary to either split the points among both posts, or to accept the first post as the answer (since it was fastest). As I mentioned elsewhere, I'll ask to get this re-opened for this reason.
1. You should use scanf ( %d, &<variable>) to read input.
2. You can optimize your minimum finding code by just one line using ternary operator ( ? : ) :
#include <stdio.h>
// double min(double x, double y); <----- no need for this function.
int main ()
{
int numberone, numbertwo;
printf("Enter a number: ");
scanf("%d", numberone);
printf("\nEnter a another number: ");
scanf("%d", numbertwo);
// printf("\nThe smaller of those two integers is %d", min(numberone, numbertwo)); <----- replacing
printf("\nThe smaller of those two integers is %d",
((numberone>numbertwo)?num berone:num bertwo)) ;
return 0;
}
/* No need for this function.
double min(double x, double y)
{
if (x < y)
y = x;
if (y < x)
x = y;
return x;
// I should be able to return either value since they will be the same by the end
// of the function
}
*/
2. You can optimize your minimum finding code by just one line using ternary operator ( ? : ) :
#include <stdio.h>
// double min(double x, double y); <----- no need for this function.
int main ()
{
int numberone, numbertwo;
printf("Enter a number: ");
scanf("%d", numberone);
printf("\nEnter a another number: ");
scanf("%d", numbertwo);
// printf("\nThe smaller of those two integers is %d", min(numberone, numbertwo)); <----- replacing
printf("\nThe smaller of those two integers is %d",
((numberone>numbertwo)?num
return 0;
}
/* No need for this function.
double min(double x, double y)
{
if (x < y)
y = x;
if (y < x)
x = y;
return x;
// I should be able to return either value since they will be the same by the end
// of the function
}
*/
I might suggest split, since the answers seem close enough that they were most likely produced independently
ASKER
thanks buddy