I am working on a study guide for my final exam and need help with this question. Thanks in advance for the help.

Write a value-returning function called "prod" that has parameters of two int arrays "arr1" and "arr2" of size"length". "Length" is also a parameter. This function should return the product of all components of "arr2" for which the corresponding components of "arr1" are negative.

Here is what i have so far, but dont know if its what I need.

int prod(arr1[], arr2[], length);
{
if (arr1[] <0)
product = arr2[]*arr2[];

return product; 

}

Recommended Answers

All 4 Replies

Here's my understanding of your question:

You will need a for loop that runs from zero to length-1 (i.e. the first element of the arrays to the last element.

Inside of this for loop, for each element of array 1 that is negative, you will need to multiply your running product by the same element in array 2 ( *= ).

For instance:
A1 = 1 5 -3 -4 2 -2
A2 = 8 6 4 5 6 3

elements 2, 3 and 5 in array 1 are negative, so your end result should be the product of elements 2, 3 and 5 from array 2, or 4*5*3, or, as you would be going through it in a for loop (with a if statement to check the value of the A1 value, of course):

result = 1;
result *= A2[2]
result *= A2[3]
result *= A2[5]

If you're in a class where this is going to be on the final, you should have an adequate grasp of for loops to be able to accomplish that.

well i m a student of first semester but u can try this

int prod(arr1[],arr2[],length)
{
    int num=1;

    for (int i=0;i<=(length-1);i++)
    {
        if (arr[i]<0)
        {
            for (int j=0;j<length;j++)
            {    num=arr1[i]*arr2[j];
                return num;
            }
        }    
    }

As per the rules/customs of daniweb, code tags should be used when posting code, but more importantly, one should not give away code/do people's homework for them. Doubly so when it's nonsense.

Thanks this helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.