Help! Function question!

This is a discussion on Help! Function question! within the C Programming forums, part of the General Programming Boards category; Code: main() { int a, b, c, d; double max, min, m, M, x, y; printf("Enter four integers: "); scanf("%d ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    24

    Help! Function question!

    Code:
    main()
    {
    int a, b, c, d;
    double max, min, m, M, x, y;
    printf("Enter four integers: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d %d %d %d", a, b, c, d);
    printf("\n");
    
    max = M;
    min = m;
    
    do
    {
    if(M > max) max = M;
    if(m < min) min = m;
    printf("%d \t %d\n", max, min);
    }while(m>0);
    
    printf("\n\nMaximum of a,b,c,d is: M=%d", max);
    
    printf("\n\nMinimum of a,b,c,d is: m=%d\n\n", min);
    }
    (a) In the main program scan four integers a,b,c,d. Call the
    functions maximum(a,b,c,d) and minimum(a,b,c,d) to find the
    maximum (M) and minimum (m) values among a,b,c,d.

    (b) Call the function mult(M,m) which returns the power M^m,
    evaluated by multiplying M by itself m times. Impose that M^0=1 for all M. If M=0 and m<0, print that 0^m = infinity.

    (Do not evaluate M^m by using pow() function, or by using the for loop).

    I definately have part (a) down, but part (b) is the confusing little guy. The DO command won't read the numbers because I don't know what to put before the
    Code:
    y_max = M
    .Someone help please!
    Last edited by Tarento; 05-18-2006 at 01:32 AM.

  2. #2
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    You have many problems, most of which are found simply by looking at gcc's warning output:
    Code:
    gcc -o foo foo.c -Wall -O2
    foo.c:4: warning: return type defaults to `int' use int main(void)
    foo.c: In function `main':
    foo.c:21: warning: int format, double arg (arg 2) use %f to printf doubles
    foo.c:21: warning: int format, double arg (arg 3) use %f to printf doubles
    foo.c:25: warning: int format, double arg (arg 2) use %f to printf doubles
    foo.c:27: warning: int format, double arg (arg 2) use %f to printf doubles
    foo.c:6: warning: unused variable `x' What are these for?
    foo.c:6: warning: unused variable `y' What are these for?
    foo.c:28: warning: control reaches end of non-void function add return 0 for success 
    foo.c:6: warning: `m' might be used uninitialized in this function You never initialized these 
    foo.c:6: warning: `M' might be used uninitialized in this function You never initialized these 
    Why do you have a do while loop that is repeating for as long as m>0 when you don't ever assign anything to m?

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    I thought that I had assigned m in min = m, but I know that was incorrect, and I don't know the correct way to assign it.

    What program are you using to compile the program?

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    CWR was using gcc compiler - you can specify extra command line
    arguments and it will point out more and more potential errors
    in your code. I'll walk you through your code and show you
    what they mean:

    Code:
    main() /*Should be int main (void)...*/
    {
    int a, b, c, d; /*When variables are created, they are not set to zero by default*/
    double max, min, m, M, x, y; 
    printf("Enter four integers: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d %d %d %d", a, b, c, d);
    printf("\n");
    
    max = M; /*So naturally that'll cause problems here - setting*/
    min = m; /*one uninitiallised variable to another*/
    
    do /*With no actual values in m,  M, max and min, this doesn't make any sense*/
    {
    if(M > max) max = M;
    if(m < min) min = m;
    printf("%d \t %d\n", max, min);
    }while(m>0);
    
    /*Use %f instead of %d*/
    printf("\n\nMaximum of a,b,c,d is: M=%d", max);
    
    printf("\n\nMinimum of a,b,c,d is: m=%d\n\n", min);
    }
    The goal of your program appears to be to find the largest and
    smallest of a set of numbers a, b, c, d - but you don't do any
    operations. For this task, you would better to look up arrays,
    rather that having 4 different variables.

    Also, you did set min = m, and many more assignments llike it.
    The basic problem is that there is no real value in m to assign
    to min. Look over whatever material you are learning from again,
    because you have some pretty serious issues with your
    understanding of how to manipulate data.
    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

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Please learn to format your code:
    Code:
    main()
    {
        int a, b, c, d;
        double max, min, m, M, x, y;
        printf("Enter four integers: ");
        scanf("%d %d %d %d", &a, &b, &c, &d);
        printf("%d %d %d %d", a, b, c, d);
        printf("\n");
    
        max = M;
        min = m;
    
        do
        {
            if(M > max) max = M;
            if(m < min) min = m;
            printf("%d \t %d\n", max, min);
        }while(m>0);
    
        printf("\n\nMaximum of a,b,c,d is: M=%d", max);
    
        printf("\n\nMinimum of a,b,c,d is: m=%d\n\n", min);
    }
    Quote Originally Posted by Tarento
    (a) In the main program scan four integers a,b,c,d. Call the
    functions maximum(a,b,c,d) and minimum(a,b,c,d) to find the
    maximum (M) and minimum (m) values among a,b,c,d.

    (b) Call the function mult(M,m) which returns the power M^m,
    evaluated by multiplying M by itself m times. Impose that M^0=1 for all M. If M=0 and m<0, print that 0^m = infinity.

    (Do not evaluate M^m by using pow() function, or by using the for loop).

    I definately have part (a) down,
    You do??? Where do you "Call the functions maximum(a,b,c,d) and minimum(a,b,c,d)"? I see no functions at all. And I question whether you get correct output. You might want to finish part A before moving on.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    Hey y'all. I think I've gotten it down a bit okay. I'm still working on Part A (Finding min and max numbers), but it's not working completely yet.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int maximum(int a, int b, int c, int d);
    int minimum(int a, int b, int c, int d);
    
    int main(void)
    {
    int a, b, c, d;
    double m, M;
    printf("Enter four integers: ");
    scanf("%d %d %d %d", &a, &b, &c, &d);
    M = maximum(a,b,c,d);
    m = minimum(a,b,c,d);
    printf("\nMaximum of a,b,c,d is: M=%d\n", M);
    
    printf("\nMinimum of a,b,c,d is: m =%d\n\n", m);
    }
    
    
    int maximum(int a, int b, int c, int d)
    {
    int M;
    if(a>b && a>c && a>d)
    return a;
    else if(b>a && b>c && b>d)
    return b;
    else if(c>a && c>b && c>d)
    return c;
    else if(d>a && d>b && d>c)
    return d;
    }
    
    int minimum(int a, int b, int c, int d)
    {
    int m;
    if (a<b && a<c && a<d)
    return a;
    else if (b<a && b<c && b<d)
    return b;
    else if (c<a && c<b && c<d)
    return c;
    else if (d<a && d<b && d<c)
    return d;
    }
    I get that M and m are some number over 100,000,000 when my integers are "1 2 3 4". What's going on?

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    int m, M;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    24
    doh. Much thanks everyone!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CreateThread() function question
    By chiefmonkey in forum C++ Programming
    Replies: 5
    Last Post: 05-15-2009, 07:52 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 08:10 AM
  4. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

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