# elements of arrays; functions

Printable View

• 09-01-2001
sballew
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.
• 09-01-2001
Procyon
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.
• 09-02-2001
Sebastiani
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?
• 09-02-2001
sballew
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??
• 09-02-2001
Procyon
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.
• 09-03-2001
Sebastiani
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
• 09-03-2001
Sebastiani
...sorry, if(a[i]>0){num++;}
......else if(a[i]<0){neg_num++;}//counts negative too