I have a 49 space one dimensional array declared as int boardArray [49]; and I also have a two dimensional 7x7 array declared as int boardArrayTwo [7][7]' I am trying to use nested for loops to throw the one dimensional array into the two dimensional array here is the code I am using to test it.
for (int i = 0; i > 50; ++i)
{
boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x >= 7; ++x)
{
for (int k = 0; k >= 7; ++k)
{
for (int n = 0; n >= 49; ++n)
{
boardArrayTwo[x][k] = boardArray[n];
cout << boardArrayTwo[x][k] << " " << endl;
}
}
}
I tried running this but nothing happens. Am I doing it wrong?
for (int x = 0; x >= 7; ++x)
{
for (int k = 0; k >= 7; ++k){
for (int n = 0; n >= 49; ++n)
{
this is wrong. x and k should be < 7 (and the third cycle shouldn't be used) :
for (int x = 0; x < 7; ++x)
{
for (int k = 0; k < 7; ++k){
boardArrayTwo[x][k] = boardArray[7*x + k];
EDIT:
like #Fabio Ceconello make me notice in his comment, even the first loop is wrong because of the inverted condition checks, it should be modified this way:
for (int i = 0; i < 49; ++i)
{
boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
Apart from the inverted logic in the loops (which the others mentioned), there's no need for the third inner loop. Just put the attribution in the second inner loop:
boardArrayTwo[x][k] = boardArray[x * 7 + k];
EDIT:
I should also mention that all these literals aren't good practice, and I added one more (7) above. I'd rewrite the code as follows:
#define arrlen(x) (sizeof(x)/sizeof((x)[0]))
for (int i = 0; i < arrlen(boardArray); ++i)
{
boardArray[i] = i;
}
int stride = arrlen(boardArrayTwo[0]);
for (int x = 0; x < arrlen(boardArrayTwo); ++x)
{
for (int k = 0; k < stride; ++k)
{
boardArrayTwo[x][k] = boardArray[stride * x + k];
cout << boardArrayTwo[x][k] << " " << endl;
}
}
caveat: if the arrays aren't declared here (were passed as parameters), arrlen() won't work. But that's another long story...
It looks like your destination array is in row-major order. You could just blast the source array directly into place.
memcpy(boardArrayTwo, boardArray, 49 * sizeof(int));
or if you prefer something in more idiomatic C++:
std::copy(boardArray, boardArray + 49, reinterpret_cast<int*>(boardArrayTwo));
You used i > 50 in your for loop. It should be i < 49 and same for all the other loops.
Also, this won't work. You're setting all of the boardArrayTwo[][] values to boardArray[49] You should instead do something like this:
for (int x = 0; x < 7; ++x)
{
for (int k = 0; k < 7; ++k)
{
boardArrayTwo[x][k] = boardArray[7*x + k];
cout << boardArrayTwo[x][k] << " " << endl;
}
}
or
int count = 0;
for (int x = 0; x < 7; ++x)
{
for (int k = 0; k < 7; ++k)
{
boardArrayTwo[x][k] = boardArray[count];
cout << boardArrayTwo[x][k] << " " << endl;
count++;
}
}
First of all, the second term in the for loop says the for loop would run while that condition is true. So you should use < instead of >= for all your loops.
Second, the loop over n is extra and shouldn't be there. What you need is to go through x and k, then copy the corresponding element from boardArray to boardArrayTwo.
You could do one of these:
int n = 0;
for (int x = 0; x < 7; ++x)
for (int k = 0; k < 7; ++k)
{
boardArrayTwo[x][k] = boardArray[n];
++n;
}
or use a formula to calculate the proper n:
for (int x = 0; x < 7; ++x)
for (int k = 0; k < 7; ++k)
boardArrayTwo[x][k] = boardArray[x*7+k];
I wrote x*7+k because it seems like x is iterating over the rows of the array, each row having 7 elements, says that x*7+kth element of the boardArray represents position [x][k] of boardArrayTwo/
Note
for (int i = 0; i > 50; ++i)
if i is initialized to 0, it won't be greater than 50 and thus it will never enter the loop.
In each of your loops you used greater than or equal (>) to rather than less than (<) or equal to. You should also notice that, as Fabio points out above, the third nested loop is setting boardArrayTwo[x][k] to 0-49 over and over again, 49 times. You will need to use arithmetic to manipulate x and k so that they will be an index into boardArray, and then assign that index to boardArrayTwo[x][k].
It's also important that you are using 0..7 inclusive, which is actually 8 positions. Your array are only of length 7 so you are actually ending up with some garbage values in there.
#include <iostream>
using std::cout;
using std::endl;
int main () {
int boardArray[49];
int boardArrayTwo[7][7];
for (int i = 0; i < 50; ++i)
{
boardArray[i] = i; //fills the array with ints 0 - 48 to test
}
for (int x = 0; x < 7; ++x)
{
for (int k = 0; k < 7; ++k)
{
boardArrayTwo[x][k] = boardArray[x*7 + k];
cout << boardArrayTwo[x][k] << " " << endl;
}
}
}
With any luck (unless I am embarrassing myself) this should do the trick!
EDIT: Special thanks to Fabio!
for(int i=0; i<49; i++)
b[i]=(i+1);
int p=0;
for(int i=0;i<7;i++){
for(int j=0;j<7;j++)
{a[i][j]=b[p];
p++;}
}
beside other errors, third loop is making your code wrong
Related
I am trying to fill an array of 52 with the numbers 0 - 12. Once it hits 12, it needs to go back to 0 - 12 again. You might have already guessed it's a deck of cards. My code is below and doesn't work. It prints 0 - 12 one time, but then prints the address of the array I believe for the remainder of the iterations left.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int myArray[52];
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i] = i;
}
}
for (int k = 0; k < 52; k++)
{
cout << myArray[k] << endl;
}
//system("pause");
return 0;
}
Can someone please help me with this brain fart?
int myints[52];
for (int idx = 0; idx < 52; idx++)
{
myints[idx] = idx % 13;
}
Modulus of 13 will range from 0 to 12.
You're indexing the same first 12 elements of the array in the inner loop for every iteration of the outer loop.
Try changing it to something like this
for (int j = 0; j < 4; j++)
{
for (int i = 0; i < 13; i++)
{
myArray[i + 13 * j] = i;
}
}
So what I am trying to do is multiply one 2d vector by another 2d vector.
I come from Java,Python and C# so I am pretty much learning C++ as I go along.
I have the code down to generate the vector and display the vector but I can't seem to finish the multiplication part.
v1 is another matrix that is already generated.
vector<vector<int> > v2 = getVector();
int n1 = v1[0].size();
int n2 = v2.size();
vector<int> a1(n2, 0);
vector<vector<int> > ans(n1, a1);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < 10; k++) {
// same as z[i][j] = z[i][j] + x[i][k] * y[k][j];
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
displayVector(ans);
My guess for where I am going wrong is in the inner-most loop. I can't figure out what to actually put in place of that 10 I have there now.
When you multiply matrices, the number of columns of the matrix on the left side must equal the number of rows of the matrix on the right side. You need to check that that is true, and use that common number for your size of the k variable:
int nCommon = v1.size();
assert(v2[0].size() == nCommon);
for (int i = 0; i < n1; i++) {
for (int j = 0; j < n2; j++) {
for (int k = 0; k < nCommon ; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
}
}
For you inner loop, you should do something like this
ans[i][j] = 0;
for (int k = 0; k < n2; k++) {
ans[i][j] += v1[i][k] * v2[k][j];
}
I don't know where the 10 comes from.
This is my program in C++, which accepts an 2D array a[m][n]. If an element a[i][j] is zero, then set all the ith row and jth column elements to zero.
This is code sample:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
class SetZero{
public:
static void setZero(int **, int , int);
};
void SetZero::setZero(int ** a, int m, int n){
int i, j, k;
int ** b = new int *[m]; //flags to identify whether set to zero or not.
for(i = 0; i < m; i++){
b[i] = new int[n];
for(j = 0; j < n; j++)
b[i][j] = 1;
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
if(a[i][j] == 0 && b[i][j]){//DUMP here. If I change it to (a+i)[j], then works.
for (k = 0; k < n; k++){
a[i][k] = 0;//but there is NO dump here. Weird!
b[i][k] = 0;
}
for(k = 0; k < m; k++){
a[k][j] = 0;
b[k][j] = 0;
}
j = n;//break. next row loop.
}
for(int i = 0; i < m; i++)
delete[] b[i];
delete[] b;
}
int main(){
int a[4][5];
srand(time(NULL));
for(int i = 0; i < 4; i++){//create an 2D array
for(int j = 0; j < 5; j++){
a[i][j] = rand() % 100;
cout << a[i][j] << " ";
}
cout << endl;
}
SetZero::setZero((int **)a, 4, 5);//type cast.
cout << endl;
for(int i = 0; i < 4; i++){//print result
for(int j = 0; j < 5; j++)
cout << a[i][j] << " ";
cout << endl;
}
return 0;
}
Environment: WIN8 Visual Studio 2012.
Edit:
The program can compile but cannot execute normally. It will stop when it reaches if(a[i][j] == 0 && b[i][j]){
The error message is:
Unhandled exception at 0x012875DD in CCLC.exe: 0xC0000005: Access
violation reading location 0x0000004B.
SetZero::setZero((int **)a, 4, 5)
a is not an array of pointers, it is simply a 2 dimensional array.
notice how the access violation is reading address 0x0000004B? that's 75, a number between 0 and 99 :) because you are treating a 2 dimensional array (which is just a one dimensional array with a neat way of accessing it) as an array of arrays, it is taking one of the values in your array (75) to be the address of a sub array, then trying to read the non existent array at address 75 (or 0x0000004B)
I suggest that you 'flatten' your arrays and work with them as one dimensional arrays, which I find simpler:
void SetZero::setZero(int * a, int m, int n){
int i, j, k;
int * b = new int [m*n]; //flags to identify whether set to zero or not.
for(i = 0; i < m; i++){
b[i] = new int[n];
for(j = 0; j < n; j++)
b[i*n+j] = 1;
}
for(i = 0; i < m; i++)
for(j = 0; j < n; j++)
if(a[i*n+j] == 0 && b[i*n+j]){//DUMP here. If I change it to (a+i)[j], then works.
for (k = 0; k < n; k++){
a[i*n+k] = 0;//but there is NO dump here. Weird!
b[i*n+k] = 0;
}
for(k = 0; k < m; k++){
a[k*n+j] = 0;
b[k*n+j] = 0;
}
j = n;//break. next row loop.
}
delete[] b;
}
int main(){
int a[4*5];
srand(time(NULL));
for(int i = 0; i < 4; i++){//create an 2D array
for(int j = 0; j < 5; j++){
a[i*5+j] = rand() % 100;
cout << a[i*5+j] << " ";
}
cout << endl;
}
SetZero::setZero(a, 4, 5);//type cast.
cout << endl;
for(int i = 0; i < 4; i++){//print result
for(int j = 0; j < 5; j++)
cout << a[i*5+j] << " ";
cout << endl;
}
return 0;
}
One suggestion about the SetZero(). There is a function called memset() which allows you to set all bytes to a specific value given a starting pointer and the range. This function could make your SetZero() function more cleaner:
void * memset ( void * ptr, int value, size_t num );
Fill block of memory. Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).
Parameters
ptr: Pointer to the block of memory to fill.
value: Value to be set. The value is passed as an int, but the function fills the block of memory using the unsigned char conversion of this value.
num: Number of bytes to be set to the value, size_t is an unsigned integral type.
For example, the following code block from your program:
for (k = 0; k < n; k++){
a[i][k] = 0;//but there is NO dump here. Weird!
b[i][k] = 0;
}
can be achieved by memset in a cleaner way:
memset(a[i], 0, n * sizeof(int));
memset(b[i], 0, n * sizeof(int));
I made an array and set the values from 1 to 9 in the initializeBoard function, but for some reason when I print the values they come out 0 to 8. Why is this happening? Shouldn't it print out 1 to 9 since those are the numbers I put in the array in initializeBoard?
int main()
{
initializeBoard();
ticTacToeBoard();
}
void initializeBoard()
{
for (int i = 1; i < 9; i++)
ticTacBoard[i] = i;
cout << endl;
}
void ticTacToeBoard ()
{
for (int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
cout << ticTacBoard[3 * y + x] << " ";
cout << endl;
}
}
You have an off-by-one error. Arrays use zero-based indexing in C++. Your code does not assign a value to the zeroth element of the array.
Try this instead:
for (int i = 0; i < 9; i++)
{
ticTacBoard[i] = i + 1;
}
The loop:
for (int i = 1; i < 9; i++)
{
ticTacBoard[i] = i;
}
will only do 1-8 since it will stop when i++ increases it to 9, so you're not initializing all 9 elements.
You should realistically do the same loop like this:
for (int i = 0; i < 9; i++)
{
ticTacBoard[i] = (i + 1);
}
Problem lies in:
ticTacBoard[i] = i;
Should be:
ticTacBoard[i-1] = i;
Two suggestions:
Shouldn't you initialize ticTacBoard before accessing it with []? Make sure you give it enough memory for all of your slots! (I'm guessing you're doing tic tac toe, so you'll want 3x3 = 9 slots)
Indexing in C++ starts at 0. You want to do for (i=0; i<9; i++)
Hope this helps!
Your i starts at 0, so your first value will be 0. Since the inequality i < 9 breaks when i = 9, i is actually never 9 (the loop exits before your code is actually run).
Try using <= instead of just < to account for i = 9:
for (int i = 1; i <= 9; i++)
{
ticTacBoard[i - 1] = i;
}
Other than that, arrays are indexed starting from 0 in C++ (and virtually every other language), so the first element is array[0], second is array[1], etc.
You'll have to subtract 1 from your array index.
I'm trying to run a 3d array but the code just crashes in windows when i run it, here's my code;
#include <iostream>
using namespace std;
int main(){
int myArray[10][10][10];
for (int i = 0; i <= 9; ++i){
for (int t = 0; t <=9; ++t){
for (int x = 0; x <= 9; ++t){
myArray[i][t][x] = i+t+x;
}
}
}
for (int i = 0; i <= 9; ++i){
for (int t = 0; t <=9; ++t){
for (int x = 0; x <= 9; ++t){
cout << myArray[i][t][x] << endl;
}
}
}
system("pause");
}
can someone throw me a quick fix / explanation
You twice have the line
for (int x = 0; x <= 9; ++t){
when you mean
for (int x = 0; x <= 9; ++x){
Classic copy-and-paste error.
BTW, if you run this in a debugger and look at the values of the variables, it's pretty easy to see what's going on.
David's answer is correct.
Incidentally, convention is to use i,j,and k for nested iterator indices, and also to use < array_length rather than <= array_length -1 as the terminator.
If you do that, then you can make the array size a constant and get rid of some magic numbers.
Also, an assertion at the point where you use the array indices might have pointed you to the error.
The result may look like:
const std::size_t ARRAY_SIZE = 10;
int myArray[ARRAY_SIZE][ARRAY_SIZE][ARRAY_SIZE];
for (std::size_t i = 0; i < ARRAY_SIZE; ++i)
{
for (std::size_t j = 0; j < ARRAY_SIZE; ++j)
{
for (std::size_t k = 0; k < ARRAY_SIZE; ++k)
{
std::assert (i < ARRAY_SIZE && j < ARRAY_SIZE && k < ARRAY_SIZE);
// Do stuff
}
}
}