function call for a average function

This is a discussion on function call for a average function within the C++ Programming forums, part of the General Programming Boards category; I'm having problems with the last function cal in main "avg(amax, amin, 6, 80, 92, 77, 55, 99)". It returns ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    1

    Unhappy function call for a average function

    I'm having problems with the last function cal in main
    "avg(amax, amin, 6, 80, 92, 77, 55, 99)". It returns garbage for the average value "a" and the variables that get passed by reference are incorect to. It's should find the average value of
    6, 80, 92, 77, 55, 99 and max the value "99" and the min value
    "6". Note the fuction defintion is at the end of the file.



    #include<iostream.h>
    #include<stdarg.h>

    // functions prototyes(s)
    int avg(int, ...);
    double davg(int, ...);
    int avg(int&, int&, int, ...);

    void main()
    {
    cout << "The average of 6 integer test scores is " << avg(5, 81, 92, 73, 84, 95) << endl;
    cout << "Press ENTER to continue..." << endl;
    cin.get();


    cout << "The average of 6 float test scores is "
    << davg(5.1, 81.0, 92.7, 73.4, 84.3, 95.6) << endl
    << "Press ENTER to continue..." << endl;
    cin.get();


    int a, amax, amin;
    a = avg(amax, amin, 6, 80, 92, 77, 55, 99);
    cout << "The average value is " << a << endl
    << "The maximum value is " << amax << endl
    << "the minimum value is " << amin << endl;
    } // main





    // returns average, and passes max and min back through argument list
    int avg(int& mx, int& mn, int n, ...)
    {
    va_list list; // assign the name "list" to the variable length list of integers

    va_start(list, n ); // tell C++ that the list to begines AFTER the "n"
    int num; // store the numbers from the list in "num" as they are "read"

    // create the toltal of "n" numers in the list
    int toltal = 0; // truck the toltal of the numbers in the list
    for ( int i = 0; i < n; i++)
    {
    num = va_arg(list, int); // set n equal to the next number inthe list, as an int
    if ((i == 0) || (mn > num)) // update min value
    mn = num;
    if ((i == 0) || (mx < num)) //update max value
    mx = num;
    toltal = toltal + num; // incriment the toltal
    }
    va_end(list); // close the list

    // compute and return the average
    return toltal / n;
    }
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >// returns average, and passes max and min back through argument list
    >int avg(int& mx, int& mn, int n, ...)

    Based on the above function definition, the third parameter should be n, the number of integers to average. So this:
    >a = avg(amax, amin, 6, 80, 92, 77, 55, 99);

    Should either be:
    a = avg(amax, amin, 5, 80, 92, 77, 55, 99);

    Or if you want to average six integers:
    a = avg(amax, amin, 6, 80, 92, 77, 55, 99, 200);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Moving Average Question
    By GCNDoug in forum C Programming
    Replies: 4
    Last Post: 04-24-2007, 12:05 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM

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