Max Min

This is a discussion on Max Min within the C Programming forums, part of the General Programming Boards category; Can any one help wiht this project? The code is to evaluate two numbers and find the max and min ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    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

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    > 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
    }

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    6

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

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    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.

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    6

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

  6. #6
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    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
    Last edited by Richie T; 02-16-2006 at 11:23 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    6
    Thank you so much....it does make it easier looking at it the way you have it posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. Variable Min and Max Program
    By EdanD in forum C Programming
    Replies: 8
    Last Post: 06-30-2004, 07:48 PM
  5. Double output!
    By Spurnout in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2002, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21