Solved

finding the minimum of two numbers

Posted on 2009-04-04
10
1,252 Views
Last Modified: 2012-05-06
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

}

Open in new window

0
Comment
Question by:prebek
10 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 250 total points
ID: 24069396
scanf("%d", &numberone);
0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 250 total points
ID: 24069400
You forgot the & in :

        scanf("%d", &numberone);

(and the same for the other scanf)
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24070484
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.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

Expert Comment

by:mayank13
ID: 24105938
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)?numberone:numbertwo)) ;
    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
}
*/
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 24178908
My suggestion :

http:#24069396 (ozo) : because he was quite a bit faster than me :)
0
 
LVL 84

Expert Comment

by:ozo
ID: 24181145
I might suggest split, since the answers seem close enough that they were most likely produced independently
0
 

Author Closing Comment

by:prebek
ID: 31566664
thanks buddy
0

Featured Post

Do email signature updates give you a headache?

Are you constantly making changes to email signatures? Are the images not formatting how you want them to? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to setup iphone app subscription service 3 119
C dll call freezes 5 93
Want to delete all my personal data 13 137
Unable to start eclipse ? 17 128
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now