Multiplication of elements of two array issue.

Suppose you want to multiply the elements of two array, whose size are entered by the user. I wrote this one whose result is (elements of first array are "1,2,3,4,5" and elements of the second array are "10,20,30"):
10
20
30
20
40
60
30
60
755642422
842281524
757926452
757935405
757935405
757935405
757935405

Which is not correct.

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include<iostream>
#include<vector>
#include<string>

using namespace std;

int* fn(int*&, int*&, int*&, int&, int&);
int* fn(int*& ptr1, int*& ptr2, int*& ptr3, int& b, int& c){

  int counter{0};
  
  for (int i = 0; i < b; ++i) {
    for (int j = 0; j < c; ++j) {

      ptr3[counter] = ptr1[i] * ptr2[j];
      counter++;
    }
  }

  return ptr3;  
};

int main()
{
  int sizeOfArray1{0};
  int sizeOfArray2{0};
  int sizeOfResultingArray{0};
  
  int *array1{nullptr};
  int *array2{nullptr};
  int *resultingArray{nullptr};
  array1 = new int;
  array2 = new int;
  resultingArray = new int;
  
  cout << "How many integer you want to store in array1?" << "\n";
  cin >> sizeOfArray1;
  
  cout << "Enter the integers that you want to store in array1." << "\n";
  for (int i = 0; i < sizeOfArray1; ++i) {
    cin >> array1[i];
  }
  
  cout << "How many integer you want to store in array2?" << "\n";
  cin >> sizeOfArray2;
  
  cout << "Enter the integers that you want to store in array2." << "\n";
  for (int i = 0; i < sizeOfArray2; ++i) {
    cin >> array2[i];
  }
  
  sizeOfResultingArray = sizeOfArray1 * sizeOfArray2;
    
  for (int i = 0; i < sizeOfResultingArray; ++i) {
    *(resultingArray + i) = 0;
  }
  
  resultingArray = fn(array1 , array2, resultingArray, sizeOfArray1, sizeOfArray2);

  cout << "-------------------------------------------------------------------" << "\n";
  
  for (int i = 0; i < sizeOfResultingArray; ++i) {
    cout << resultingArray[i] << "\n";
  }

  delete [] array1;
  delete [] array2;
  delete [] resultingArray;
      
  return 0;
}

I can not find the issue in this code and i do not understand where do
"755642422
842281524
757926452
757935405
757935405
757935405
757935405"
come from.
What is wrong with this code? Can you help me please.
There is a problem with new as the size isn't specified - hence only 1 element is allocated. Thus there is memory overrun error. Also, why not use vectors?

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
#include <iostream>

using namespace std;

void fn(const int* ptr1, const int* ptr2, int* ptr3, size_t b, size_t c) {
	for (size_t i = 0, counter = 0; i < b; ++i)
		for (size_t j = 0; j < c; ++j)
			ptr3[counter++] = ptr1[i] * ptr2[j];
};

int main()
{
	size_t sizeOfArray1 {};
	size_t sizeOfArray2 {};

	cout << "How many integer you want to store in array1? ";
	cin >> sizeOfArray1;

	auto array1 {new int[sizeOfArray1]};

	cout << "Enter the integers that you want to store in array1.\n";
	for (size_t i = 0; i < sizeOfArray1; ++i)
		cin >> array1[i];

	cout << "How many integer you want to store in array2? ";
	cin >> sizeOfArray2;

	auto array2 {new int[sizeOfArray2]};

	cout << "Enter the integers that you want to store in array2.\n";
	for (size_t i = 0; i < sizeOfArray2; ++i)
		cin >> array2[i];

	const auto sizeOfResultingArray {sizeOfArray1 * sizeOfArray2};
	auto resultingArray {new int[sizeOfResultingArray] {}};

	fn(array1, array2, resultingArray, sizeOfArray1, sizeOfArray2);

	cout << "-------------------------------------------------------------------\n";

	for (size_t i = 0; i < sizeOfResultingArray; ++i)
		cout << resultingArray[i] << '\n';

	delete[] array1;
	delete[] array2;
	delete[] resultingArray;
}

Last edited on
1
2
3
4
5
6
7
8
9
10
int* array1 = new int; // array1 points to one int
// The size of "array" is 1
// that integer can be dereferenced:
*array1
*(array1 + 0)
array1[0]

array1[1] // ERROR: out-of-range

delete [] array1; // ERROR: memory was not allocated with new [] 
Last edited on
Topic archived. No new replies allowed.