How do I create a four-dimensional array of pointers? [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I am trying to create a four-dimensional array of pointers. The type of the array is a Gurobi type "GRBVar". However, I am unsure of the syntax to use.
GRBVar**** testArray = new GRBVar*** [numBuses];
for (int i = 0; i < numBuses; i++) {
testArray[i] = new GRBVar** [maxRoute];
for (int j = 0; j < maxRoute; j++) {
testArray[j] = new GRBVar* [numJobs];
for (int k = 0; k < numJobs; k++) {
testArray[k] = new GRBVar[numJobs];
}
}
}
The above causes an error. How can I fix it?

Literally you can create it like GRBVar* array[10][10][10][10], but it's better to use nested std::vector for this purpose, like std::vector<GRBVar*>, std::vector<std::vector<GRBVar*>> etc, and moreover - use aliases, like
using Jobs = std::vector<GRBVar*>;
using Routes = std::vector<Jobs>;
using Buses = std::vector<Routes>;
And then initialize it:
Buses buses;
for (int i = 0; i < numBuses; i++) {
Routes routes;
for (int j = 0; j < maxRoute; j++) {
Jobs jobs;
for (int k = 0; k < numJobs; k++) {
jobs.push_nack(new GRBVar);
}
routes.push_back(jobs);
}
buses.push_back(routes);
}

You need to increase the [] levels in your nested for loops to assign the pointers to their proper objects.
#include <iostream>
typedef struct SomeObject { int r; } *GRBVar; // this keeps the syntax as close as possible to the original question
int main()
{
int numBuses = 2;
int maxRoute = 3;
int numJobs = 4;
SomeObject* someobjects = new SomeObject[numBuses * maxRoute * numJobs * numJobs];
// This creates and initializes a 4D array of pointers to SomeObjects
GRBVar**** testArray = new GRBVar ***[numBuses];
for (int i = 0; i < numBuses; i++) {
testArray[i] = new GRBVar **[maxRoute];
for (int j = 0; j < maxRoute; j++) {
testArray[i][j] = new GRBVar *[numJobs];
for (int k = 0; k < numJobs; k++) {
testArray[i][j][k] = new GRBVar[numJobs];
for (int m = 0; m < numJobs; m++)
testArray[i][j][k][m] = someobjects++;
}
}
}
testArray[0][0][0][0]->r = 1;
testArray[0][0][0][1]->r = 2;
testArray[0][0][1][0]->r = 3;
std::cout << testArray[0][0][0][0]->r << " " << testArray[0][0][0][1]->r << " " << testArray[0][0][1][0]->r << "\n";
}

Related

when I trying to implement the counting sort I got this error " the problem caused the program to stop working correctly Windows [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
when I trying to implement the counting sort I got this error " the problem caused the program to stop working correctly Windows will close the program and notify you if a solution is available "
void CountingSort(int *A,int size) {
int SizeC = Max(A, size);
int* B = new int[size];
int* C = new int[SizeC+1];
for (int i = 0; i < SizeC; i++) {
C[i] = 0;
}
for (int i = 0; i < size; i++) {
C[A[i]]++ ;
}
for (int i = 0; i <SizeC; i++)
{
C[i] += C[i - 1];
}
for (int j = size - 1; j >= 0; j--) {
B[C[A[j]]] = A[j];
C[A[j]] --;
}
for (int i = 0; i < size; i++) {
cout << B[i] << "\t" << endl;
}
delete[] C;
delete[] B;
}
this is the error
i < SizeC and i <SizeC should be i <= SizeC. Otherwise, the elements with value SizeC won't be treated properly.
C[i] += C[i - 1]; with i = 0 will result in out-of-range read of C[-1]. The initialization of corresponding for loop should be int i = 1, not int i = 0.
The decrementing C[A[j]] --; should happen before B[C[A[j]]] = A[j];, or out-of-range write of B[size] will happen.
This program won't work well when the array A contains negative values.
Fixed code (negative values are still not supported):
void CountingSort(int *A,int size) {
int SizeC = Max(A, size);
int* B = new int[size];
int* C = new int[SizeC+1];
for (int i = 0; i <= SizeC; i++) {
C[i] = 0;
}
for (int i = 0; i < size; i++) {
C[A[i]]++ ;
}
for (int i = 1; i <= SizeC; i++)
{
C[i] += C[i - 1];
}
for (int j = size - 1; j >= 0; j--) {
C[A[j]] --;
B[C[A[j]]] = A[j];
}
for (int i = 0; i < size; i++) {
cout << B[i] << "\t" << endl;
}
delete[] C;
delete[] B;
}

How to Increment values of an an array using a nested-loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
So I need help. The problem is that an int arr[5] = {0};
I know the array has the values {0,0,0,0,0} filling the whole array. At the end of the code the array must have the values {1,2,3,4,5} inside it. To solve it must use a nested for loop.
I have tried the following code
Sorry if there is an error in the way this question is formatted this is my first time and using mobile.
int arr[5] = {0};
for(int j = 1; j<6; j++)
{
for(int i = 0; i<5; i++)
{
arr[i] = j;
}
}
I don't know what the benefit of that but If you have to use nested for loop, the following is an option
int main()
{
int arr[5] = {0};
for(int j = 0; j < 5; ++j)
{
for(int i = 1; i < 2; ++i)
{
arr[j] = j+i;
}
}
for(int i{};i<5;++i) std::cout << arr[i] << ", ";
}
Or like #JaMit comment suggests
int main()
{
int arr[5] = {0};
for(int j = 0; j < 5; ++j)
{
for(int i = 0; i < j+1; ++i)
{
arr[j]++;
}
}
for(int i{};i<5;++i) std::cout << arr[i] << ", ";
}
I'm guessing your professor means you should generate the final results using only incrementation. In other words, I'm guessing you're not allowed to assign values after the int arr[5] = {0}; line (so an expression like arr[i] = j; is not allowed).
In that case, you could solve the problem as follows:
int arr[5] = {0};
for (int i = 0; i < 5; i++) {
for (int j = i; j < 5; j++) {
arr[j]++;
}
}
Now we have only used incrementation and the final result is {1, 2, 3, 4, 5}.

Assinging a pointer-to-pointer dynamic array with values

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 do I create a 2d array pointer with my own class as type?

I am trying to create a 2d array pointer with my own class, Tile, as type. I have looked at the code example at How do I declare a 2d array in C++ using new?. The following code works perfectly:
int** ary = new int*[sizeX];
for(int i = 0; i < sizeX; ++i)
ary[i] = new int[sizeY];
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
ary[i][j] = 5;
for(int i = 0; i < 8; i++)
for(int j = 0; j < 8; j++)
cout << ary[i][j];
However when I try to change type from int to my own class, Tile, I get an
No viable overloaded '='
error in XCode, and I can't figure out what this means. I use the following code:
Tile** t;
t = new Tile*[8];
for(int i = 0; i < 8; ++i)
t[i] = new Tile[8];
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
t[i][j] = new Tile(new NoPiece());
}
}
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
cout << (t[i][j].get_piece()).to_string();
}
}
Here is the code for Tile.cpp:
#include "Tile.h"
Tile::Tile() {
}
Tile::Tile(Piece p) {
piece = &p;
}
Piece Tile::get_piece() {
return *piece;
}
And the code for Tile.h:
#include <iostream>
#include "Piece.h"
class Tile {
Piece * piece;
public:
Tile();
Tile(Piece p);
Piece get_piece();
};
The difference between two code snippets is that the one using int treats array elements like values, i.e. assigns
ary[i][j] = 5;
while the one using Tile treats array elements like pointers:
t[i][j] = new Tile(new NoPiece()); // new makes a pointer to Tile
Change the assignment to one without new to fix the problem:
t[i][j] = Tile(new NoPiece());
There is nothing wrong to making a 2D array of pointers, too - all you need is to declare it as a "triple pointer", and add an extra level of indirection:
Tile*** t;
t = new Tile**[8];
for(int i = 0; i < 8; ++i)
t[i] = new Tile*[8];
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
t[i][j] = new Tile(new NoPiece());
}
}
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
cout << (t[i][j]->get_piece()).to_string();
}
}
// Don't forget to free the tiles and the array
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++) {
delete t[i][j];
}
delete[] t[i];
}

multidimensional array function outputting garbage?

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;
}