Reputation: 71
I am trying to declare 2d arrays dynamically and fill them with random numbers and then create a function that will compare the elements in two 2d arrays and if they were equal it will return true
However, i keep getting error when trying to call the boolean function.
#include <iostream>
#include <cstdlib>
using namespace std;
bool isEqual(int *arr1[], int *arr2[], bool &eq, int row, int col){
for(int r = 0; r<row;r++)
{
for(int c= 0; c<col;r++)
{
if(arr1[r][c]==arr2[r][c])
eq = true;
}
}
return eq;
}
int main()
{
const int R = 3;
int * arr2D_a[R];
int * arr2D_b[R];
int C;
cout << "Enter number of columns: ";
cin >> C;
for (int r = 0; r < R; r++) {
arr2D_a[r] = new int [C];
arr2D_b[r] = new int [C];
}
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
arr2D_a[r][c] = rand() % 2;
arr2D_b[r][c] = rand() % 2;
}
}
bool result = false;
isEqual(arr2D_a,arr2D_b,result,R,C);
if (result==true)
cout << "\nThe 2 array are the same!\n";
else
cout << "\nThe 2 array are the differernt!\n";
for (int c = 0; c < C; c++) {
delete[] arr2D_a[C];
delete[] arr2D_b[C];
}
for (int r = 0; r < R; r++) {
delete[] arr2D_a[r];
delete[] arr2D_b[r];
}
system("pause");
}
Upvotes: 2
Views: 1210
Reputation: 1151
EDIT i took the liberty of rewriting your code. the code i posted, compiled in VS2017.
your compare seems kind off
#include <iostream>
#include <cstdlib>
using namespace std;
bool isEqual(int* arr1[], int* arr2[], const int row, const int col) {
for (int r = 0; r < row; r++)
{
for (int c = 0; c < col; c++)
{
if (arr1[r][c] != arr2[r][c])
return false;
}
}
return true;
}
int main()
{
const int R = 3;
int * arr2D_a[R];
int * arr2D_b[R];
int C;
cout << "Enter number of columns: ";
cin >> C;
for (int r = 0; r < R; r++) {
arr2D_a[r] = new int[C];
arr2D_b[r] = new int[C];
}
for (int r = 0; r < R; r++) {
for (int c = 0; c < C; c++) {
int value = rand();
arr2D_a[r][c] = value % 2;
arr2D_b[r][c] = value % 2;
}
}
bool result = isEqual(arr2D_a, arr2D_b, R, C);
if (result)
cout << "\nThe 2 array are the same!\n";
else
cout << "\nThe 2 array are the differernt!\n";
for (int r = 0; r < R; r++) {
delete[] arr2D_a[r];
arr2D_a[r] = 0;
delete[] arr2D_b[r];
arr2D_b[r] = 0;
}
return 0;
}
EDIT 2
I totally removed the bool in the function call...
EDIT 3
to leave this program for good you have to provide a return value
another mistake was, you must not make the second delete loop. since you have not dynamically allocated this memory.
EDIT 4
reworked the function to please all the compilers =)
EDIT 5
i hope its the last edit for this answer^^ i fixed the memory issue. i checked it with dr. memory and he says, everything is ok :D
Upvotes: 4
Reputation:
The answer above fixes most of the issues, but you will get segfault
her.
for (int c = 0; c < C; c++) {
delete[] arr2D_a[c];
delete[] arr2D_b[c];
}
if you put something greater than 3 up here
std::cin >> C;
What you need to do is leave the second loop:
for (int r = 0; r < R; R++) {
delete[] arr2D_a[r];
delete[] arr2D_b[r];
}
because you allocated C
amount of space in every arr2D_a[r]
and arr2D_b[r]
.
Upvotes: 2