Thread: elements of arrays; functions

  1. #1
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157

    Question elements of arrays; functions

    ok needing clarification on basics of arrays:


    // assume array a and n (length of array) are of int type in
    all functions //


    function 1)

    needing largest element of array a


    int f (int a[], int n)
    { int largest =0;
    largest = a[n-1];
    return largest;
    }

    // I wrote this then thought we must assume user is prompted to input own element values, in that case how do you sort for largest element ?? //


    function 2)

    needing average of all elements of array a

    int f(int a[], int n)
    { int sum=0, average =0;
    int i;

    for (i=0; i<n; i++)
    { sum += a[i];
    }
    average = (sum/2);
    return average;
    }

    // now must assume user is prompted to input own element values//


    function 3)

    needing the # of positive elements in array a

    int f(int a[], int n)
    { int num=0, element=0;
    int i;

    for (i=0; i<n; i++)

    // okay here is where I am stumped; do I need to only use one counter ?? do I immediately test if remainder, then sum only those with no remainder???? not sure of how to code that //


    Thanks for any help you can give.
    It's been a few months since writing any programs in C.
    Sue B.

    dazed and confused


  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    OK...

    In (1), you return the value of the largest-indexed element. Is this really what you want, or do you want the maximum value stored in the array?

    In (2), you divide sum by 2. You want to divide by n.

    In (3), just try (in your for loop) the line "num += (a > 0);", then "return num;" outside the for loop. You don't need a second counter.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Yes, for function 1:

    This should be a comparison-then-reassignment with a "temp" variable. Think "if"...

    In function 3:

    >>>"...then sum only those with no remainder???? not sure of how to code that ...">>>

    Are you trying to get the number of POSITIVE or EVEN elements??
    __________________________________________________
    And why not embed the user input into the function. Otherwise, every time you call that function you will have to duplicate the user input phase for that specific routine?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User sballew's Avatar
    Join Date
    Sep 2001
    Posts
    157
    ok in 1) I think the result to return is the largest element, (the biggest positive) not the last element (which would have the largest element name ie A0...A100, A100 being the largest here)

    So using a temp variable and then comparison would work huh???


    2) oh what a dumbo I am; dividing by 2 doesn't give average for n number of elements... duh.


    3) again, I need result of # of "positives" not "even", so I guess using modulator and getting no remainder makes difference, I need to just compare to >0 and have a loop to add those that are >0; how do you code for those =< 0???

    Would using an If-else work here??
    Sue B.

    dazed and confused


  5. #5
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    1) Yes, that will work.

    2) OK.

    3) See my earlier post! The expression (a > 0) will evaluate to 1 if the number is positive, and to 0 otherwise. So for every positive number in the array, num will be incremented by one. Nonpositive numbers won't change num. Of course, you can use "if (a > 0) num++;" instead if you want.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    To find the largest in the array if(element[i]>temp){temp=element[i];} //temp will always hold the largest number in the array

    If you're just counting the number of positive numbers in the array then if(a[i]>0){num++;}
    else{neg_num++;}//counts negative too
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    ...sorry, if(a[i]>0){num++;}
    ......else if(a[i]<0){neg_num++;}//counts negative too
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-04-2008, 08:27 PM
  2. Multidimensional arrays and functions.
    By miniwhip in forum C Programming
    Replies: 6
    Last Post: 09-12-2007, 01:27 PM
  3. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  4. functions and arrays
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 03-14-2002, 09:57 AM
  5. passing multidimensional arrays to functions
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 11-17-2001, 03:27 AM