I've been coding for a little over a month now and I have decided to give 2d arrays out. One thing I am getting confused with is how to assign values to the second portion. Like if I have arr2D[x][y] how do I assign inputs to the [y] portion. Since I would have a loop of inputs for [y] until it does not meet condition.
#include <iostream>
#include <fstream>
usingnamespace std;
int x,y,i,n,size,dup=0;
int main()
{
int count=1;
cout<<"Please enter in how many rolls your input files contains."<<endl;
cin>>size;
int arr2D[size][2];
fstream myfile;
myfile.open("dice.txt",ios::in);
for (x=0;x<=size;x++)
{
for(y=0;y<2;y++)//cause this program is taking values of 2 dice rolls so only 2 elements
{
myfile>>arrDice[x][y];//dont know how to input just for[y] so stating what I am trying to input data into.
}
The file I am using is layed out in a format of:
1,3
4,6
8,9
etc,etc.....
If you could give me an example of how you would input data into the second portion of a 2d array that would be of great help! Thank you to all taking time to read and help me with this issue.
Makes a really unstable program. If you want to use that you need a dynamic array which made through pointers.
But as to actually assign value it is really simple say if we use the example
1 2 3 4 5 6 7 8
int main ()
{
int thearray [2][2];
thearray [2][2] =2;
return 0;
}
What this does is assign the value to the index value of 2 and 2. Or to better think about it is to think about a box and divide it in quarters and each of these quarters is representation of the array. So it would be like this...
Upper left of the box = thearray [1][1]
Bottom left of the box = thearray [1][2]
and so forth
So going back to that coding above we placed the value two into the bottom right hand spot of the box. You follow this kind of logic when it come to 2d arrays. Hope that helps and furthermore, you should check out the page on arrays as well if haven't done so already.
thearray [2][2] =2; // This will be out of the array bounds
return 0;
---------------------------------
Upper left of the box = thearray [1][1] ( end of this array )
Bottom left of the box = thearray [1][2] As will this
and so forth
}
An array declared as int thearray[2][2]; can only have values placed at:
thearray [0][0] //array starts with zero, not one
thearray [0][1]
thearray [1][0]
thearray [1][1] // array finish. A 2x2 array.
@gobiking
cout<<"Please enter in how many rolls your input files contains."<<endl;
cin>>size;
int arr2D[size][2];
This code, as is, is illegal. Your compiler should complain, and probably not even compile it.
i haven't tried to compile yet since I couoldn't think of proper way to assign value, but thank you for showing the way! Makes so much sense when thought out as a grid =D.
Now time for me to read up on dynamic arrays apparantly =)
// 2D array.cpp : main project file.
#include <iostream>
#include <fstream>
usingnamespace std;
int x,y,i,n,size,dup=0;
int main()
{
int count=1;
int arr2D[3][3];
for (int x=0;x<3;x++)
{
for(int y=0;y<3;y++)
{
cout << "Enter an integer value for arr2D[" << x << "][" << y << "] " ;
cin >> arr2D[x][y];
}
}
// Now print out the array
cout << endl << "The values entered were:" << endl;
for (x=0;x<3;x++)
{
for( y=0;y<3;y++)
{
cout << arr2D[x][y] << " ";
}
cout << endl; // Go to next line, for next set of values
}
}