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
#include<iostream>
#include<vector>
#include<string>
usingnamespace 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 = newint;
array2 = newint;
resultingArray = newint;
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?
#include <iostream>
usingnamespace std;
void fn(constint* ptr1, constint* 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 {newint[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 {newint[sizeOfArray2]};
cout << "Enter the integers that you want to store in array2.\n";
for (size_t i = 0; i < sizeOfArray2; ++i)
cin >> array2[i];
constauto sizeOfResultingArray {sizeOfArray1 * sizeOfArray2};
auto resultingArray {newint[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;
}
int* array1 = newint; // 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 []