Write a function that accepts an array of integers and determines the product of each two integers. Although you can create a fixed size array in main, your function should work no matter what size of array is passed to the function.

MY code:

#include <iostream>
using namespace std;

void Product(float matrix[4][4], int rows, int columns)
{
int iRow, iColumn, ia, ib;
for(iRow = 0; iRow < rows; iRow++)
for(iColumn = 0; iColumn < columns; iColumn++)
{
for(ia = iRow; ia < rows; ia++)
for(ib = iColumn + !!(ia == iRow); ib < columns; ib++)
cout << ("\t ", matrix[iRow][iColumn] * matrix[ia][ib]) << endl;
cout << ("");
}
}

My code was incorrect and my teacher gave me this note:

If the array [3 7 8 2 -3] is passed to the function, it prints to the screen [3 21 56 16 -6]. (note the first element of the array is not modified) Pass the array to the function and then print the resulting array to the console. (you do not need to store the result in a new array)

please help me fix my function! thank you.

What do you need to find exactly? Do you need to find just 3*7, 3*8, 3*2, and 3*-3 or do you need to continue on to 7*3, 7*8, etc...?

I expect you should be able to do this with 2 loops. I'm not really sure why you think you need 4.

You passed a 2 dimensional array, your instructors example used a single dimensional array. Your instructors example prints out the first element of the array and then the product of two adjacent elements in the array, that is current element times the next element, as long as there is a next element. Using the appropriate loop controls this can be accomplished using a single loop.

i am not really good with arrays, also when I do the multiplication it does not give me back in array form

declare the function by indicating an array type to return first, then the name of function and then the list of parameters to send to the function including the array type and array name plus an int to indicate size of the array.

In the body of the function:
1) declare an array of the size passed in using dynamic memory (using the keyword new)to act as the array to return to the calling function
2) assign the first element of the array passed in to the first element of the return array
3) declare a for loop using an counter of type int to loop from the second element in the array and continuing while the counter is less than the size of the array incrementing the counter by one each time through the loop.
4) in the body of the loop multiple adjacent elements of the array passed in based on the current value of the counter and assign that product to the element of the return array having an index equal to the counter's value.

Note that according to the instructors memo you don't need to return an array "(you do not need to store the result in a new array)", you can just output the results to the screen

Edited 6 Years Ago by Lerner: n/a

This article has been dead for over six months. Start a new discussion instead.