English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

i was wondering if I have two arrays:

int array1 [15][15]
int array 2 [15][15]

and I want to create a 3rd array

int array3 [15][15]

such that each cell in array3 is the product of the corresponding cells in array1 and array2.

So,

array3 [1][1] = array1 [1][1] * array [1][1];
array3 [1][2] = array1 [1][2] * array [1][2];

etc.

is there an easier way to do this? for example is:

array3 = array1*array2;

a valid way to express this or do I need to creat a loop?

2007-02-26 16:24:49 · 7 answers · asked by cpine505 3 in Computers & Internet Programming & Design

7 answers

Loops would be the way to go

For(int i = 0; i<15;i++){
For(int j = 0; j<15;j++){
Array3[i][j] = Array1[i][j] * Array2[i][j];
}
}

That or something like that should work.

2007-02-26 16:38:02 · answer #1 · answered by andrew f 2 · 1 0

you can use two for loops for that. sometime there are not always what we want. they have to be created so that someone else might use it if he wants. this the function that i m creating for u

int ** returnproduct( int array1[15][15], int array2[15][15])
{
int array[15][15];
for( int i=0;i< 15;i++)
{
for( int j=0;j<15;j++)
{
array [i][j] = array1[i][j] * array[i][j];
}
}
return array;
}

2007-02-27 02:27:09 · answer #2 · answered by Anonymous · 0 0

array3 = array1*array2; This wouldn't be the same as if you added the brackets to it, because now you're saying you want to store only one number in array3. If you have some sort of program you're trying to write, i might understand it better if you posted it or something.

2007-02-27 00:31:08 · answer #3 · answered by Chris 2 · 0 0

With basic C++ operations you cant avoid using the loops. (Two loops infact). But if you are going to need this for long, you may not to create a class and then overload the multiplier operator.

2007-02-27 00:57:53 · answer #4 · answered by manoj Ransing 3 · 0 0

Your question is not so clear. If you want to avoid multiplication cell by cell at all - it is impossible.
If you just want to use simple and clear syntax in your calling code, you can define class/structure as:
class array15x15
{
array15x15(); // default constructor
array15x15(...) // define init ctor as you need
array15x15( const array15x15 & a );
// copy constructor

array15x15 & operator = (const array15x15 & a );
array15x15 & operator *= (const array15x15 & a );
};

operator *= will do required cell-by-cell multiplication and assign result to your object.

This way,
array15x15 a1(...); // init constructor
array15x15 a2(...); // also init constructor
array15x15 a3(a1); // copy constructor
a3 *= a2; // here is you result in a3.

2007-02-27 00:50:34 · answer #5 · answered by alakit013 5 · 0 0

Well like u said it is possible. It took me like 20 minutes to write the code and 10 minutes to debug it. It works to.

#include
using namespace std;
#define MAX 20
int main()
{
int firstArray[MAX];
int secondArray[MAX];
int thirdArray[MAX];
int bigger;
int counter = 0;
int counter2 =0;
char yesNo;


cout << "_______________\nFirst ARRAY\n___________";

while(1){
cout << "Enter a name to be stored in the array:";
cin >> firstArray[counter];
cout << "\nWhat to enter more names(y/n)";
cin >> yesNo;
yesNo = toupper(yesNo);
if(yesNo == 'N' )
break;
else
counter++;
}
cout << endl << endl << "_______________\nSECOND ARRAY\n___________";
while(1){
cout << "Enter a name to be stored in the array:";
cin >> secondArray[counter2];
cout << "\nWhat to enter more names(y/n)";
cin >> yesNo;
yesNo = toupper(yesNo);
if(yesNo == 'N' )
break;
else
counter2++;
}
cout << endl << "1st" << endl;
for(unsigned int loop = 0; loop <= counter; loop++){
cout << firstArray[loop] << endl;

}

cout << endl << "2st" << endl;
for(unsigned int loop2 = 0; loop2 <= counter2; loop2++){
cout << secondArray[loop2] << endl;

}

cout << endl << endl <<"FIRST ARRAY X SECOND ARRAY" << endl;

if(counter > counter2)
bigger = counter;
else
bigger = counter2;


for(unsigned int loop3 = 0; loop3 <= bigger; loop3++){
cout << firstArray[loop3] << "*" << secondArray[loop3] << " = " << firstArray[loop3] * secondArray[loop3];
cout << endl;
}
cout << endl;

system("PAUSE");
return 0;
}

2007-02-27 00:56:51 · answer #6 · answered by Best Helper 4 · 0 0

only god would know the answer to that question.....

2007-02-27 00:27:09 · answer #7 · answered by Anonymous · 0 1

fedest.com, questions and answers