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
}
}
}
Related
I have a low-level function that will be called millions of times, so it should be very efficient. When I use "gprof" in Linux, I found that a part of the code takes 60% of the total computation of the function (the rest part is to solve the roots of a cubic equation). Here Point is a data structure has x and v, which will be converted to a matrix for later use. The idea is to subtract each row by the first row. The code shows like below
double x[4][3] = {0}, v[4][3] = {0};
for (int i = 0; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = Point[i]->v[j];
x[i][j] = Point[i]->x[j];
}
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
v[i][j] = v[0][j] - v[i][j];
x[i][j] = x[0][j] - x[i][j];
}
}
Can anyone show me the problem of this code? Why it performs so badly?
You can do it all in one pass:
double x[4][3] = {
{ Point[0]->x[0], Point[0]->x[1], Point[0]->x[2] }
};
double v[4][3] = {
{ Point[0]->v[0], Point[0]->v[1], Point[0]->v[2] }
};
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] = x[0][j] - Point[i]->x[j];
v[i][j] = v[0][j] - Point[i]->v[j];
}
}
You could even take that to the next level and put the entire thing into the initializers for x and v.
Or, if x and v in Point are each contiguous arrays:
double x[4][3], v[4][3]; // no init
// fill entire arrays
for (int i = 0; i < 4; ++i){
memcpy(x[0], Point[0]->x, sizeof(x[0]));
memcpy(v[0], Point[0]->v, sizeof(v[0]));
}
for (int i = 1; i < 4; ++i){
for (int j = 0; j < 3; ++j){
x[i][j] -= Point[i]->x[j];
v[i][j] -= Point[i]->v[j];
}
}
I'm currently reading Jumping into C++ by Alex Allain and am stuck on Chapter 14's Practice Problem number 1.
Write a function that builds a two-dimensional multiplication table with arbitrary sizes for two dimensions.
I'm having trouble actually assigning the times table to the array but all these nested loops are giving me a headache! I'm getting an output of "999999999".
My Code:
#include <iostream>
using namespace std;
int main()
{
int **p_p_tictactoe;
p_p_tictactoe = new int*[3];
for (int i = 0; i < 3; i++)
p_p_tictactoe[i] = new int[3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
p_p_tictactoe[i][j] = 1;
for (int y = 0; y < 4; y++)
{
for (int t = 0; t < 4; t++)
{
p_p_tictactoe[i][j] = y * t;
}
}
cout << p_p_tictactoe[i][j];
}
}
cin.get();
for (int i = 0; i < 3; i++)
delete[] p_p_tictactoe[i];
delete[] p_p_tictactoe;
}
How can I create a array with dinamic size like this:
int sentLen = sentences.size();
double a[sentLen][sentLen];
for (int i = 0; i < sentLen; i++)
{
for (int j = 0; j < sentLen; j++)
{
a[i][j] = somefunction(i, j);
}
}
My research led me to malloc which isn't recommended or other too complicated methods. After I realised that size must be constant, I tried using unordered_map, and I have tried the following:
std::unordered_map <int, int, double> a;
for (int i = 0; i < sentLen; i++)
{
for (int j = 0; j < sentLen; j++)
{
a.insert({ i, j, somefunc(i, j) });
}
}
but still unsuccessful.
You don't really want to use arrays.
std::vector<std::vector<double>> a{
sentLen, std::vector<double>{ sentLen, 0.0 } };
for (int i = 0; i < sentLen; ++i)
{
for (int j = 0; j < sentLen; ++j)
{
a[i][j] = somefunc(i, j);
}
}
You're getting an error because you can't use variables as static array sizes. They must be known at compile time. You have to allocate dynamically or use a vector instead.
I have this function meant to initialize a multidimensional 2d (6x6) array to zero. I call the function in main using cout to test it and it outputs garbage. Please help. Thanks!
int** initializeArray(void)
{
typedef int* rollArray; //this line is actually outside of the function in my
//program
int i, j;
rollArray *m = new rollArray[6];
for (i = 0; i < 6; i++)
m[i] = new int[6];
for (i = 0; i < 6; i++)
for (j = 0; j < 6; j++)
m[i][j] = 0;
return m;
}
If the value 6 is known at compile-time, I would suggest using std::array in a nested fashion. For example:
#include <array>
#include <iostream>
int main()
{
std::array<std::array<int,6>,6> a = {0};
for (int i = 0; i < 6; ++i)
{
for (int j = 0; j < 6; ++j)
{
std::cout << a[i][j] << std::endl; // Prints 0.
}
}
return 0;
}
In fact, you won't even need to create a function to initialize your array. Declare your nested array and you are good to go. (If you don't know the dimension at compile-time, you could use std::vector in a similar fashion.)
The problem is with your test.
How can you mess up such a simple test? Just use:
int ** a = initializeArray();
int i,j;
for (i = 0; i < 6; i++) {
for (j = 0; j < 6; j++) {
cout << a[i][j] << " ";
}
cout << endl;
}
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