First, I want to create a two dimensional array. It is 50 rows by 2 columns.
The first column contains an ID #: 1, 2, 3, 4,...50
The second column contains associated values: 1,3,5,etc (odd numbers). I'm not inputting any values - I want to use a loop that will go through the 50 rows and assign values. It should look like this:
ID Value
1 1
2 3
3 5
4 7...
Is my code below right? I have an inkling that my logic is slightly off somewhere :\
This piece is a part within a class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class ZY
{
public:
void start();
void end();
class zy: ZY
{
constint row = 50;
constint col = 2;
int myArray[row][col];
for (int i = 1; i < row; i++)
for (int j = 1; j < row; j++)
myArray [i][] = i+2;
myArray [][j] = 1+2*j;
}
}
im just a beginner aswell but that looks pretty messy,
I personally would use (but it could be wrong as all hell)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class ZY
{
public:
void start();
void end();
class zy: ZY
{
int myArray[50] [2];
for (int i = 0, int j = 0; i < 50, j < 2 ; i++, j++)
{
myArray [i][j] = i + 2, 1 + j*2;
(though I don't know if this bit's working code)
}
}
}
I tried the code above, and it seems to compile and build on MS VS.However, I don't get an output, so I"m not sure at all. Can anyone give me tips/pointers in what to do in this case?
In addition, could I take the same code and place it in a sub-class of a class?
Thanks for all your help - i'm v. novice!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#include <iostream>
usingnamespace std;
int main()
{
constint row = 50;
constint col = 2;
int array1 [row][col];
for (int i = 0, j = 0; i < row, j <col ; i++, j++)
{
array1[i][j] = i+1, 1+j*2;
cout << array1[i][j] << endl;
}
system ("pause");
return 0;
}
Looks to me like ur for loop would only run twice because you only have 2 columns.
Try splitting your program into two loops; nest one inside the other, loop 50 values into the first array and then the other 50 into the next one.
Something like:
1 2 3 4 5 6
for (int i=0; i<col; i++) {
for (int j=0; j<row; j++) {
array[i][j] = value, value;
cout << array etc.
}
}
you can't do them at the same time because your column gets full faster than your row.
Dont fret with the rows and columns stuff, its just a way to explain it to make it sound easier for beginners.
However you describe it the data is at point [x] [y].
I just realized that 2d arrays must be filled in by rows first and then column. What I want is to have the ID's in one column and the associated values in the next, but there is a pattern that is associated with the first column of ID's : 1,2,3, etc. and for the second column, it'd just contain the odd integers: 1, 3, 5, etc.
Umz, I took yours but can't seem to get an output -- can anyone test on their end to see if you're getting an output?
class MyClass {
int **p2DArray;
constint Rows = 50;
constint Cols = 2;
public:
// Constructor
MyClass() {
// Allocate Memory
p2DArray = newint*[Rows];
for (int i = 0; i < Rows; ++i)
p2DArray[i] = newint[Cols];
}
// De-Constructor
~MyClass() {
// De-Allocate so we don't have memory leak
for (int i = 0; i < Rows; ++i)
delete [] p2DArray[i];
delete [] p2DArray;
}
// Do Something
void doSomething() {
p2DArray[someRow][someCol] = 12;
}
}
Note: My code doesn't follow OO-Encapsulation. It's merely there to illustrate 2 methods of having a 2D Array in a class. Yes I am aware that I could make Method 2 a single-dimensional array by using (Rows*Cols), but meh.
I just realized that 2d arrays must be filled in by rows first and then column. What I want is to have the ID's in one column and the associated values in the next, but there is a pattern that is associated with the first column of ID's : 1,2,3, etc. and for the second column, it'd just contain the odd integers: 1, 3, 5, etc.
1 2 3 4 5
for (int i = 0; i < Rows; ++i) {
for (int j = 0; j < Cols; ++j) {
my2DArray[i][j] = 1 + (i*cols) + (j*2);
}
}
You cannot store 2 values in the location. The location is mapped by 2 Values (Row and Col). At Location Array[Row][Col] is 1 Value.
array2[i][j] = i, 2*j+2;
Is crap, you cannot store 2 values at 1 memory location.
I think your problem is a design one, more than a code one.
A solution to what you are trying to do. But not using a 2D array as it's pointless.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
using std::cout;
using std::endl;
int main() {
constint rows = 50;
int myArray[rows] = {0}; // Only need 1D Array
for (int i = 0; i < rows; ++i)
myArray[i] = 1 + (i*2);
for (int i = 0; i < rows; ++i)
cout << (i+1) << ": " << myArray[i] << endl;
return 0;
}
Right only one value goes in per run, my bad I did it wrong.
Just use the loop, enter the first 50 values on the first run then the second values on the one after.
If you are only going to associate two values, I would not use a 2D array at all. If you forsee increasing the dimensions later, then my suggestion will not be applicable.
The STL has an associative container called a map. It relates two things: a key and a value. Since each of 1-50 is unique and can be related to a single odd number value, this container is ideal.