arayssss...

can i use

1
2
y= (myArray2[j])*(myArray1[i]);
z= (myArray2[j])+(myArray1[i]);



to find multiplication and addition between two arrays in the following code...?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
using namespace std;

int main()
{
   int sizearray = 0;
   int *myArray1, *myArray2;
	float y, z;

   std::cout<<"Enter the amount of numbers you want to enter : ";
   std::cin>>sizearray;

	std::cout<<endl<<endl;

	std::cout<<"Number in first group : "<<endl;
   myArray1 = new int[sizearray];

   for (int i = 0; i < sizearray; i ++)
		{
      	std::cout<<"Please enter number " << i + 1 << ": ";
      	std::cin>>myArray1[i];
   	}

	std::cout<<endl<<endl;

	std::cout<<"Number in second group : "<<endl;
   myArray2 = new int[sizearray];

   for (int j = 0; j < sizearray; j ++)
		{
      	std::cout<<"Please enter number " << j + 1 << ": ";
      	std::cin>>myArray2[j];
   	}

	std::cout<<endl<<endl;

	y= (myArray2[j])*(myArray1[i]);
	z= (myArray2[j])+(myArray1[i]);

	system ("PAUSE");
	return 0;
}


help me... i have been defeated with array(s)... -_-
You cannot multiply the entire array because that doesn't really make sense.

You can only multiply/add individual elements in an array. If you want to do this for multiple elements, you'll have to put it in a loop.

Example:

1
2
3
4
5
6
7
8
9
10
int array1[3] = {1,2,3};
int array2[3] = {4,5,6};
int result[3];

for( int i = 0; i < 3; ++i )
{
  result[i] = array1[i] + array2[i];
}

// result is now {5,7,9} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
#include <iostream>


using std::cin;
using std::cout;
using std::endl;


int main()
{
    int size_array = 0;
    int *myArray1, *myArray2;
    float y, z;

    cout << "Enter the amount of numbers you want to enter : ";
    cin  >> size_array;

    cout << endl << endl;

    myArray1 = new int[size_array];

   for (int i = 0; i < size_array; i++)
   {
       cout << "Please enter number(s) in array #1" << ": ";
       cin  >> myArray1[i];
   }

    myArray2 = new int[size_array];

   for (int i = 0; i < size_array; i++)
   {
       cout << "Please enter number(s) in array #2" << ": ";
       cin  >> myArray2[i];
   }

    cout << "---Multiplication---" << endl;

    for (int i = 0; i < size_array; i++)
    {
        y = (myArray1[i])*(myArray2[i]);
    	cout << y << endl;
    }

    delete [] myArray1; 
    delete [] myArray2; 

    return 0;
}


I took the liberty of re-writing the program (only for multiplication). You don't seem to be using the global namespace in the program. Anyway, summing the elements can be achieved in similiar fashion.
why must
1
2
delete [] myArray1; 
delete [] myArray2;


i cannot understand it... :'(
Because you have to free the memory you've allocated with operator new after the execution of the program. Remember, for every new there must be a delete.
but in my coding, i do not use operator new ....
1
2
 
myArray1 = new int[size_array];


Maybe i should've written "for every new[] there must be a delete[]".
Er, yes you did. Line 16 and line 27:

1
2
myArray1 = new int[sizearray];
myArray2 = new int[sizearray];


The problem with your first example (besides the memory leak GrayWolf fixed) was that the variables i and j you declared in your for loops to input are no longer in scope after the for loops exit. So you have to use another loop to multiply each element and output it, like he did on line 39. Notice he uses the same variable in all 3 loops?
Last edited on
i want to make this coding more flexible, means, user can enter decimal numbers..
so, i just need to change int *myArray1, *myArray2; to float *myArray1, *myArray2; right? but, why message error : comparison between distinct pointer types `float*' and `int*' lacks a cast and error : cannot convert `int*' to `float*' in assignment ...?
Firs of all You have ability to create the objects so you could do something like this myArray1+myArray2
Yes I know that is not for beggining of C++ but it can be done in object orjented programming, but it is more logical any way, and one more thing there is big problem for me when it comes to the statement that array is the set of same elements, it is possible to use records and unions in combination so that You can have array of non simmilar elements...
OK..
Topic archived. No new replies allowed.