function call to print the smallest number?

This is a discussion on function call to print the smallest number? within the C Programming forums, part of the General Programming Boards category; C programming, make it use a function call to print the smallest number? hi guys this is a program which ...

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    3

    Question function call to print the smallest number?

    C programming, make it use a function call to print the smallest number?

    hi guys
    this is a program which prints out the smallest of three numbers within
    the main function that I was asked to write.
    Now the other question i am asked,5. Re-write the program, uses a function call to print the smallest number?
    what do it mean if someone can explain or show me an example please
    i am new to c

    Code:
    # include <stdio.h>
    
    main()
    {
    int a,b,c;
    int temp, min;
    
    a = 100;
    b = 23;
    c = 5;
    
    min = a;
    temp = b;
    if (min < temp)
    {
    min = temp;
    temp = c;
    if (min < temp)
    {
    min = temp;
    printf (\nThe minimum value of these three numbers is , min);
    }
    else 
    {
    printf (\nThe minimum value of these three numbers is , min);
    }
    }
    else
    {
    temp = c;
    f (min < temp)
    {
    min = temp;
    printf (\nThe minimum value of these three numbers is , min);
    }
    else 
    {
    printf (\nThe minimum value of these three numbers is , min);
    }
    }
    
    

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,376
    It's kind of difficult to give an example of this without just giving away the answer. Start by writing a function that receives the numbers declared in "main()", and move the relevant code into that function.

    Based on the assignment, you should have already learned about functions, so refer to your notes for reference. You can also find information here:

    Functions in C - Cprogramming.com

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    3
    Quote Originally Posted by Matticus View Post
    It's kind of difficult to give an example of this without just giving away the answer. Start by writing a function that receives the numbers declared in "main()", and move the relevant code into that function.

    Based on the assignment, you should have already learned about functions, so refer to your notes for reference. You can also find information here:

    Functions in C - Cprogramming.com

    I think I got what you mean, is this right now?
    but the output is below that i get, with out a number.

    The minimum value is




    Code:
    #include<stdio.h>
    
    
    int main()
    {
    printf("\n The minimum value is ", minval());
    }
    int minval(int min)
    {
    int a, b, c;
    int temp;
    
    
    a = 100;
    b = 23;
    c = 5;
    
    
    temp = a;
    min = b;
    
    
    if (temp > min)
    {
        temp = c;
        if (temp > min)
        {
            min = b;
        }
        else
            {min = temp;
        }
    }
    else
    {
        min = temp;
        temp = c;
        if (temp > min)
        {
            min = b;
        }
        else
        {
            min = temp;
        }
    }
    return min;
    }
    Last edited by mrcisco_89; 03-15-2013 at 10:44 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Line #6, what are you missing -- look in the format section.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    printf("\n The minimum value is ", minval());
    }
    int minval(int min)
    Adak told you already about the missing format specifier.

    Another problem is that you defined your function "minval" to take one argument, but in main() you call it without one.

    But as I understand it your function should take three arguments and return the smallest of them.
    Here are two examples for using such a function:
    Code:
    int min = minval(3, 1, 2) // "min" should be 1
    
    int a = 100, b = 23, c = 5;
    int min2 = minval(a, b, c) // "min2" should be 5
    Bye, Andreas

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,311
    Quote Originally Posted by AndiPersti View Post
    But as I understand it your function should take three arguments and return the smallest of them.
    Well, reading the first post in this thread literally, that wasn't technically a requirement. I agree with you, however, that what you describe would be an appropriate approach.


    Reading the first post literally, all that is needed is to move everything within the body of main() into another function, and then call that other function from main(). If I was an instructor, however, a student who gave me that solution would fail - the purpose of programming is effective problem solving, not to see who can pedantically interpret a question to justify a response that is not "fit for purpose".
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 14
    Last Post: 10-07-2012, 06:43 PM
  2. How to determine and print smallest and largest values
    By Anna Lane in forum C Programming
    Replies: 4
    Last Post: 09-28-2010, 07:05 AM
  3. smallest largest number
    By manzoor in forum C++ Programming
    Replies: 10
    Last Post: 05-12-2008, 07:56 AM
  4. smallest digit of a number
    By lakestar in forum C Programming
    Replies: 1
    Last Post: 11-11-2007, 07:57 PM
  5. largest and smallest number
    By wise_ron in forum C Programming
    Replies: 11
    Last Post: 10-05-2006, 03:25 PM

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