So I want to initialize an int 2d array very quickly, but I can't figure out how to do it. I've done a few searches and none of them say how to initialize a 2D array, except to do:
int [SOME_CONSTANT][ANOTHER_CONSTANT] = {{0}};
Basically, I've got 8 vertices, and I'm listing the 4 vertices of each face of a cube in an array. I've tried this:
int[6][4] sides = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
But that tells me that there's an error with 'sides', and that it expected a semi-colon. Is there any way to initialize an array quickly like this?
Thanks!
You have the [][] on the wrong side. Try this:
int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
Keep in mind that what you really have is:
int **sides
(A pointer to a pointer of ints). It's sides that has the dimensions, not the int. Therefore, you could also do:
int x, y[2], z[3][4], ...;
I think You meant to say
int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
int array[n][m] behaves just like int array[n * m].
In fact, array[i][j] = array[m * i + j] for all i, j.
So int array[2][3] = {1, 2, 3, 4, 5, 6}; is a valid declaration and, for example,
array[1][1] = array[3 * 1 + 1] = array[4] = 5.
int sides[6][4] = {{0, 1, 2, 3}, {4, 5, 6, 7}, {0, 4, 7, 3}, {7, 6, 2, 3}, {5, 1, 2, 6}, {0, 1, 5, 4}};
I'm not a regular c++ programmer but I looks like int sides[6][4] seems to compile while int[6][4] sides fails. Languages like C# lets you have the [][] on either sides but apparently c++ doesn't.
int sides[6][4] = ... should do the trick. This sounds like you may be coming from a Java (or other language) background so I do recommend a C++ book The Definitive C++ Book Guide and List for more details.
Yes, the intended type of sides is int[6][4], but C++ has confusing syntax sometimes. The way to declare said array is:
int sides[6][4] = {/*stuff*/};
You run into this with function pointers too, but even worse:
int (*myfuncptr)(int); //creates a function pointer called myfuncptr
With function pointers though, you can do this:
typedef int (*func_ptr_type)(int);
func_ptr_type myfuncptr;
Unfortunately, there's no corresponding magic trick for arrays.
i would make a array outside of function and just assign it it to your local. this will very likely invoke memcpy or just inline memory copying loop
this is the fastest you can get
Related
I want my data structure to look something like this when populated:
[[1, 2, 3] [3, 3, 3] [4, 4, 4]]
[[5, 4, 5] [3, 4, 5] [3, 3, 3]]
I'm not sure where to go from here. I've tried doing:
vector<vector<vector<int> > > x;
But I'm having trouble populating it. I think it's basically a matrix of vectors? I'm just not sure how to go about it.
EDIT: Sorry, I should've specified a bit more.
The core functionality of my program is to create multiple vector of size 3 and push them 1 by 1 in a 30x3 matrix of vectors. The matrix will be initially completely empty.
I want to be able to push a vector at a specific row, similar to something like this
vector<int> y{1, 2, 3};
x.at(row).push_back(y);
I'm not sure if that's the correct syntax of how to approach that but I want that to be the final functionality. My goal is to create a 30x3 matrix with each index in the matrix being a vector of size 3
It seems like you want this:
std::vector<std::vector<std::vector<int>>> x(30, std::vector<std::vector<int>>(3, std::vector<int>(3)));
The constructor I am using takes (std::size_t n, T t) as in n elements of type T.
If you want to replace a vector in a given cell, you can use:
x[2][4] = {2, 7, 9};
Or you can replace an entire row of 3like this:
x[1] = {{2, 7, 9}, {1, 1, 8}, {6, 2, 1}};
use {} instead of []:
std::vector<std::vector<std::vector<int>>> x{
{{1, 2, 3}, {3, 3, 3}, {4, 4, 4}},
{{5, 4, 5}, {3, 4, 5}, {3, 3, 3}}
};
Hi guys i was looking around some old threads but i can't find anything that works for me. I need to shift second row in my array with cpp 98 from this
int mat[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16}};
to this
int mat[4][4] = {{1, 2, 3, 4},
{5, 6, 7, 8},
{12, 9 , 10, 11},
{13, 14, 15, 16}};
I don't want to print out anything just switching places in array, Thank you
One very easy method is this, first create a temporary array to store the initial values,
int temp[4] = { mat[2][3], mat[2][0], mat[2][1], mat[2][2] };
Then use std::memcpy to copy the data into mat[2],
std::memcpy(mat[2], temp, sizeof(int) * 4);
Bonus: You can use a scope to save some memory. It would be like this,
int mat[4][4] = { {1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12},
{13, 14, 15, 16} };
...
{
int temp[4] = { mat[2][3], mat[2][0], mat[2][1], mat[2][2] };
std::memcpy(mat[2], temp, sizeof(int) * 4);
}
I have the following code:
int *exceptions[7];
int a[] = {1, 4, 11, 13};
int b[] = {5, 6, 11, 12, 14, 15};
int c[] = {2, 12, 14, 15};
int d[] = {1, 4, 7, 9, 10, 15};
int e[] = {1, 3, 4, 5, 7, 9};
int f[] = {1, 2, 3, 7, 13};
int g[] = {0, 1, 7, 12};
exceptions[0] = a;
exceptions[1] = b;
exceptions[2] = c;
exceptions[3] = d;
exceptions[4] = e;
exceptions[5] = f;
exceptions[6] = g;
Size of exception[0] and exception[1] should be 4 and 6 respectively.
Here's my code:
short size = sizeof(exceptions[1]) / sizeof(exceptions[1][0]);
But I'm getting 2 for every row. How can I solve this problem?
short size = sizeof(exceptions[1]) / sizeof(exceptions[1][0]);
effectively does the same as
short size = sizeof(int*) / sizeof(int);
On a 64 bit platform, that yields most probably 2.
How can I solve this problem?
Use some c++ standard container like std::vector<std::vector<int>> instead:
std::vector<std::vector<int>> exceptions {
{1, 4, 11, 13},
{5, 6, 11, 12, 14, 15},
{2, 12, 14, 15},
{1, 4, 7, 9, 10, 15},
{1, 3, 4, 5, 7, 9},
{1, 2, 3, 7, 13},
{0, 1, 7, 12},
}
Your statement will become:
short size = exceptions[0].size();
size = exceptions[1].size();
(for whatever that's needed)
The best remedy would be to use vector provided in standard template library. They have a size() function which you can use and they are much more versatile than array.
I am making a TicTacToe program and I'm trying to use OOP techniques. Within my 'Board' class I am wanting the program to store each way a set of moves can be won.
I hope this can be demonstrated here:
Board.h
#pragma once
class Board
{
private:
int winningRows[8][3]; //Variable in question
public:
static const char X = 'X'; //Game piece 'X'
static const char O = 'O'; //Game piece 'O'
static const char EMPTY = ' '; //Empty game piece
static const char TIE = 'T'; //Game is tie
static const char NOONE = 'N'; //Nobody has won game yet
static const int numbOfSquares = 9; //Number of squares on the board
int InitializeWinningCombinations();
void FindWinner();
};
Board.cpp
#include "stdafx.h"
#include "Board.h"
int Board::InitializeWinningCombinations()
{
/*
The playing board
0, 1, 2
3, 4, 5
6, 7, 8
*/
//All possible ways player can win game
winningRows[8][3] = {
//Horizontal
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
//Vertical
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
//Diagonal
{2, 4, 6},
{0, 4, 8}
};
//return winnigRows[8][3];
}
void Board::FindWinner()
{
//I am wanting to get the variable here so I can play around with it later.
int winningRows = InitializeWinningCombinations();
}
I could just have the 'winningRows' variable inside the 'FindWinnner' function but from my understanding it is best to abstract as much as possible and have it as a member of the 'Board' class
Thank you for your time.
winningRows[8][3] = {
//Horizontal
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
//Vertical
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
//Diagonal
{2, 4, 6},
{0, 4, 8}
};
Is an attempted array assignment not an initialization and it cannot be done. You can initialize the array in a constructor like
Board() : winningRows{
//Horizontal
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
//Vertical
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
//Diagonal
{2, 4, 6},
{0, 4, 8}
} {}
Live Example
You'd have to change the signature to
int** InitializeWinningCombinations();
Then you could call it as
int** winningRows = InitializeWinningCombinations();
I have a list of lists of numbers. I add them into one list by adding all of the first elements together, all of the second elements together, etc. For example, if my list were { {1,2,3}, {1,2,3}, {1,2,3,4} } I would want to end up with {3,6,9,4}. How do I do this in Mathematica?
a = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3, 4}};
Total#PadRight#a
{3, 6, 9, 4}
Among its many useful features, Flatten will transpose a 'ragged' array (see here for a nice explanation, or check out the 'applications' subsection of the documentation on Flatten)
Total /# Flatten[#, {{2}}] &#{{1, 2, 3}, {1, 2, 3}, {1, 2, 3, 4}}
{3, 6, 9, 4}
If all the rows were the same length then adding the rows would do this.
So make all the rows the same length by appending zeros and then add them.
lists = {{1, 2, 3}, {1, 2, 3}, {1, 2, 3, 4}};
max = Max[Length /# lists]; min = Min[Length /# lists];
zeros = Table[0, {max - min}];
Plus ## Map[Take[Join[#, zeros], max] &, lists]