My program runs without any problem. The problem I see with my code is that the array may be going out of the array or is lost when it is being pass to the setfunction. When I print the values of my two dimensional array, I print out garbage. There is no problem when I put the information into my two dimensional array in the main function. A friend of mine told me that you cannot use a two dimensional array with a class. If that is true, can anyone tell me why? Thanks!
#include <iostream>
#include <iomanip>
using namespace std;
class KenKen
{
private:
int array[4][4];
public:
void setArray(int a[4][4])
{
array[4][4] = a[4][4];
}
int getArray()
{
return array[4][4];
}
void print()
{
for (int row=0; row<4; row++)
{
for (int column=0; column<4; column++)
{
cout << setw(3) << right << "||" << array[row][column] << "||";
}
cout << endl;
}
}
};
int main()
{
int k[4][4];
int value;
KenKen kenken;
for (int row=0; row<4; row++)
{
for (int column=0; column<4; column++)
{
cin >> value;
k[row][column] = value;
}
}
/************************************************************************
for (int row=0; row<4; row++)
{
for (int column=0; column<4; column++)
{
cout << setw(3) << right << "||" << k[row][column] << "||";
}
cout << endl;
}
**************************************************************************/
The problem is that you're not setting the array, you're setting element [4][4], which, by the way, is outside the array (the last element is [3][3]).
To copy an array, you have to either copy it element by element, use std::copy() if it's suitable, or call memcpy() if the element type is a POD type. But it's not possible to copy an array by just assigning it.
getArray() also doesn't return the array.
A friend of mine told me that you cannot use a two dimensional array with a class.
int main()
{
int k[4][4];
int value;
KenKen kenken;
for (int row=0; row<4; row++)
{
for (int column=0; column<4; column++)
{
cin >> value;
k[row][column] = value;
}
}
/************************************************************************
for (int row=0; row<4; row++)
{
for (int column=0; column<4; column++)
{
cout << setw(3) << right << "||" << k[row][column] << "||";
}
cout << endl;
}
**************************************************************************/
kenken.setArray(k);
kenken.print();
return 0;
}
Also, I did not understand why you said getArray() doesn't return the array. Do you mean that it wasn't necessary because I have a print function as part of the class?
Please think this through more carefully. getArray returns an int. That is a single, integral value not an array. the return statement is attempting to access a single element of the array and return it and only it. I think that Helios already pointed out that your indexers are off by one. The range of the indices always starts at 0. Therefore the last value of the array would be array[3][3].
Please use to learn tags. In fact I would appreciate it if you would edit your posts and properly format the code within tags. That way if you have further questions people can more easily read your code. http://cplusplus.com/articles/firedraco1/
Finally please scan the articles forum as there are a few articles on arrays and 2d arrays that you need to read.