I would like to convert an array of Integers
2, 3, 4, 8
5, 7, 9, 12
1, 0, 6, 10
to a string with the entries of that matrix appended in clockwise order
“2, 3, 4, 8, 12, 10, 6, 0, 1, 5, 7, 9”.
I have to keep declaration of int * Matrix and char * OutBuffer the way they are
int main()
{
int matrixArray[rowCount][columnCount] =
{ {2, 3, 4, 8},
{5, 7, 9, 12},
{1, 0, 6, 10}};
int * matrix;
string prebuffer;
char * outBuffer;
outBuffer = new (nothrow) char[24];
matrix = &matrixArray[0][0];
BuildStringFromMatrix(matrix, rowCount, columnCount, outBuffer);
}
I declare and address all my pointers before passing them in. However, I am not sure if I am going about allocating memory for the outBuffer to store the characters of prebuffer correctly?
void BuildStringFromMatrix(int* Matrix, int NumRows, int NumColumns, char * OutBuffer)
{
string prebuffer;
bool stringLeft = true;
int i = 0;
while (stringLeft)
{
int clockwiseDir[12] = { 1,1,1,4,1,1,0,4,-4,-1,-1,-1 };
prebuffer = to_string(Matrix[i]) + ", ";
OutBuffer = new char [prebuffer.length() + 1];
cout << prebuffer;
i += clockwiseDir[i];
if (i == 6)
{
prebuffer = to_string(Matrix[i]) + " ";
cout << prebuffer;
stringLeft = false;
}
}
}
**When I do not implement OutBuffer I have no trouble accessing and printing the matrix in clockwise format
But I how would I go about using OutBuffer to reference and print prebuffers contents??
I need numbers to display not unprintable symbols on the ASCII table
Thanks in advance :)
**
Firstly, in your loop under BuildStringFromMatrix function you are not using your i value anywhere.
Second, matrix = matrixArray should do fine.
Related
I was following internet tutorials on this topic, but I have the following situation:
I have a function with the following signature:
void func(long& rows, long& columns, int array[][columns]);
and I'm trying to use the function like this:
int matrix[5][4] = {0, -1, 2, -3,
4, -5, 6, -7,
8, -9, 10, -11,
12, -13, 14, -15,
16, -17, 18, -19};
long rows = 5;
long columns = 4;
func(rows, columns, matrix);
^--- 'No matching function for call to 'func''
What is the problem? Why can't it call the function?
Variable length arrays is not a standard C++ features.
You could declare the function and the array the following way
const size_t columns = 4;
void func( size_t rows, const int array[][columns]);
//...
int matrix[][columns] = { { 0, -1, 2, -3 },
{ 4, -5, 6, -7 },
{ 8, -9, 10, -11 },
{ 12, -13, 14, -15 },
{ 16, -17, 18, -19 } };
func( sizeof( matrix ) / sizeof( *matrix ), matrix);
//...
void func( size_t rows, const int array[][columns] )
{
std::cout << rows << columns << array[0][1];
}
Pay attention to that as the number of columns is well-known there is no sense to pass it to the function. And moreover there is no sense to pass the number of rows and columns by reference.
Have you actually defined func in your program?
The following source code compiles and works fine for me
#include <iostream>
#define ROW 5
#define COLUMN 4
void func(long &rows, long &columns, int array[][COLUMN]);
int main()
{
int matrix[ROW][COLUMN] = {0, -1, 2, -3,
4, -5, 6, -7,
8, -9, 10, -11,
12, -13, 14, -15,
16, -17, 18, -19};
long rows = 5;
long columns = 4;
func(rows, columns, matrix);
return 0;
}
void func(long &rows, long &columns, int array[][COLUMN])
{
std::cout << rows << columns << array[0][1];
}
This question already has answers here:
How to pass a 2D array by pointer in C?
(5 answers)
Closed 2 years ago.
I am trying to pass two dimensional array in function, but is has two errors which I don't know why. I have some articles about passing two dimensional array in function but also can't understand why I fail.
#include <iostream>
using namespace std;
// prototypes
void matrixSwap(double** matrix, int rows, int columns);
int main()
{
const int ROWS = 5;
const int COLUMNS = 5;
double matrix[ROWS][COLUMNS] =
{
{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 0},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
matrixSwap(matrix, ROWS, COLUMNS);
/* it says
1) argument of type "double (*)[5U]" is incompatible with parameter of type "double **"
2) 'void matrixSwap(double *[],int,int)': cannot convert argument 1 from 'double [5][5]' to 'double *[]'
*/
}
void matrixSwap(double** matrix, int rows, int columns) {}
The multidimensional double array matrix you're trying to pass in the function matrixSwap() to the argument double**, actually doesn't represents a multidimensional array.
Use arrays correctly as shown:
#include <iostream>
using namespace std;
const unsigned short MAXROWS = 5;
// prototypes
void matrixSwap(double matrix[][MAXROWS], int rows, int columns);
int main()
{
const int ROWS = 5;
const int COLUMNS = 5;
double matrix[ROWS][COLUMNS] =
{
{ 1, 2, 3, 4, 5},
{ 6, 7, 8, 9, 0},
{11, 12, 13, 14, 15},
{16, 17, 18, 19, 20},
{21, 22, 23, 24, 25}
};
matrixSwap(matrix, ROWS, COLUMNS);
}
void matrixSwap(double matrix[][MAXROWS], int rows, int columns) {}
Just changed into [][MAXROWS] where MAXROWS contains an unsigned integer of value 5.
The declaration:
void matrixSwap(double matrix[][MAXROWS], int rows, int columns)
is equivalent to:
void matrixSwap(double (*matrix)[MAXROWS], int rows, int columns)
Notice that here I've used *matrix and then appended [MAXROWS] which does the same job as matrix[][MAXROWS].
So you may do the same thing in another way as follows:
void matrixSwap(double (*matrix)[MAXROWS], int rows, int columns) {
for (int i = 0; i < columns; i++) {
for (int j = 0; j < rows; j++) {
std::cout << matrix[i][j] << ' ';
}
std::cout << std::endl;
}
}
This will give you the output:
1 2 3 4 5
6 7 8 9 0
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
To see if the matrix is successfully passed into the function by the new argument.
I am using gcc compiler on ubuntu 16 , when I am printing value garbage value is getting displayed
#include <bits/stdc++.h>
int Arrayprint(int r, int l, unsigned int* q)
{
r = 3;
l = 4;
for (int i = 0; i < r; i++) {
for (int j = 0; j < l; j++) {
cout << *(q + sizeof(unsigned int) * (i * l + j)); //Garbage getting diplay
cout << *(q + i + j); //this working
cout << "\t";
}
}
cout << "size of unsigned int : " << sizeof(unsigned int); //4
cout << "size of int : " << sizeof(int); //4
}
int main()
{
unsigned int image[R][L] = { { 1, 2, 3, 4 },
{ 5, 6, 7, 8 },
{ 9, 10, 11, 12 } };
unsigned int* q = (unsigned int*)image;
Arrayprint(R, L, q);
}
From what I can tell, you understand at a low level that the address of the ith element of an array of T is base + sizeof(T) * i. That's correct, and it's good that you know that.
However, C and C++ handle this for you already. When you say q + i or q[i], it's actually compiling that into q + sizeof(T)*i anyway (with the latter also dereferencing the result).
So when you say q[sizeof(int)*i], that's actually compiling into *(q + sizeof(int)*sizeof(int)*i), which is clearly not what you wanted.
Thus, the index in the array you actually access is off by a factor of sizeof(int) and results in an out of bounds error, which is where your strange numbers are coming from.
I am using gcc compiler on ubuntu 16 , when I am printing value
garbage value is getting displayed
Instead of trying to fix what's broken in your raw array arimethics, consider using the standard containers:
#include <iostream>
#include <array>
constexpr size_t R = 3;
constexpr size_t L = 4;
using image_t = std::array<std::array<unsigned int, L>, R>;
void Arrayprint(const image_t& q) {
// range based for loops for convenience
for(auto& row : q) { // get references to each row
for(unsigned int colval : row) { // get the column values
std::cout << colval << "\t"; // print the values
}
std::cout << "\n";
}
}
int main() {
image_t image = {{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}}};
Arrayprint(image);
}
Output:
1 2 3 4
5 6 7 8
9 10 11 12
I'm trying to store simulation data from C++ in HDF5 (later I will analyse this data in Python + Pandas). My goal is try to organise all the data properly in C++ so later I will only have to read it.
My problem is trying to store a dynamic array in different columns of HDF5: I am using H5::VarLenType to store the array. I success, but I am getting the array in one single column, and it is not convenient for me: I need every value in a single column.
I can do it if I used fixed-size arrays, but not using a temporary buffer with the hvl_t datatype. If I use the same approach (iterating over a loop calculating offsets by hand and adding the datatype) with the variable length array I get garbage data.
I have learned this approach in this SO answer
This is my proof of concept, later I will add it to my project.
#include <stddef.h>
#include <cstring>
#include <string>
#include <sstream>
#include <iostream>
#include "H5Cpp.h"
const int MAX_NAME_LENGTH = 32;
const int N_PLACES = 3;
const int N_ROWS = 3;
const std::string FileName("SimulationResults-test.h5");
const std::string DatasetName("SimulationData");
const std::string member_simulation("Simulation");
const std::string member_iteration("Iteration");
const std::string member_time_elapsed("Time_elapsed");
const std::string member_place_states("States");
const std::string member_fired_transition("Fired_transition");
typedef struct {
int simulation;
int iteration;
double time_elapsed;
char fired_transition[MAX_NAME_LENGTH];
int * place_states;
} SimulationData;
typedef struct {
int simulation;
int iteration;
double time_elapsed;
char fired_transition[MAX_NAME_LENGTH]; // MAX_NAME_LENGTH
hvl_t place_states; // N_PLACES
} SimulationData_buffer;
int main(void) {
// Data to write
SimulationData states_simulation[N_ROWS];
SimulationData_buffer states_simulation_buffer[N_ROWS];
// {
// { 1, 0, 0.0, {0, 0, 0}, "T1" },
// { 1, 1, 1.0, {0, 1, 0}, "T2" },
// { 1, 2, 5.0, {0, 0, 1}, "T1" }
// };
for (int i = 0; i< N_ROWS; i++) {
states_simulation[i].simulation = 1;
states_simulation[i].iteration = 0;
states_simulation[i].time_elapsed = 0.0;
// states_simulation[i].fired_transition = "T1";
strncpy(states_simulation[i].fired_transition, "T1",
sizeof(states_simulation[i].fired_transition) - 1);
states_simulation[i].fired_transition[sizeof(states_simulation[i].fired_transition) - 1] = 0;
states_simulation[i].place_states = new int[N_PLACES];
states_simulation[i].place_states[0] = 0;
states_simulation[i].place_states[1] = 10;
states_simulation[i].place_states[2] = 20;
}
// Number of rows
hsize_t dim[] = {sizeof(states_simulation) / sizeof(SimulationData)};
// Dimension of each row
int rank = sizeof(dim) / sizeof(hsize_t);
// defining the datatype to pass HDF5
H5::CompType mtype(sizeof(SimulationData_buffer));
mtype.insertMember(member_simulation,
HOFFSET(SimulationData, simulation),
H5::PredType::NATIVE_INT);
mtype.insertMember(member_iteration,
HOFFSET(SimulationData, iteration),
H5::PredType::NATIVE_INT);
mtype.insertMember(member_time_elapsed,
HOFFSET(SimulationData, time_elapsed),
H5::PredType::NATIVE_DOUBLE);
mtype.insertMember(member_fired_transition,
HOFFSET(SimulationData, fired_transition),
H5::StrType(H5::PredType::C_S1, MAX_NAME_LENGTH));
auto vlen_id_places = H5::VarLenType(H5::PredType::NATIVE_INT);
// Set different columns for the array <-------------------------
// auto offset = HOFFSET(SimulationData, place_states);
// for (int i = 0; i < N_PLACES; i++) {
// std::stringstream ss;
// ss << "Place_" << i+1;
// auto new_offset = offset + i*sizeof(int);
// std::cout << offset << " -> " << new_offset << std::endl;
// mtype.insertMember(ss.str(),
// new_offset,
// H5::PredType::NATIVE_INT);
// }
// Set the column as an array <-----------------------------------
mtype.insertMember("Places", HOFFSET(SimulationData, place_states), vlen_id_places);
// Filling buffer
for (int i = 0; i < N_ROWS; ++i) {
states_simulation_buffer[i].simulation = states_simulation[i].simulation;
states_simulation_buffer[i].iteration = states_simulation[i].iteration;
states_simulation_buffer[i].time_elapsed = states_simulation[i].time_elapsed;
strncpy(states_simulation_buffer[i].fired_transition,
states_simulation[i].fired_transition,
MAX_NAME_LENGTH);
states_simulation_buffer[i].place_states.len = N_PLACES;
states_simulation_buffer[i].place_states.p = states_simulation[i].place_states;
}
// preparation of a dataset and a file.
H5::DataSpace space(rank, dim);
H5::H5File *file = new H5::H5File(FileName, H5F_ACC_TRUNC);
H5::DataSet *dataset = new H5::DataSet(file->createDataSet(DatasetName,
mtype,
space));
H5::DataSet *dataset2 = new H5::DataSet(file->createDataSet("Prueba2",
mtype,
space));
// Write
dataset->write(states_simulation_buffer, mtype);
dataset2->write(states_simulation_buffer, mtype);
delete dataset;
delete file;
return 0;
}
Can be compiled with g++ h5-test-dynamic.cpp -lhdf5 -lhdf5_cpp -o h5-test-dynamic.
As said before I need one column per value, not an array in a single column. I don't know why it is not working as I have set the pointers and the offsets for the hvl_t variable properly. If I toggle on the block of code which handles the offsets and the datatypes manually and toggle off the one immediately later, I get garbage values.
This is what I get
[(1, 0, 0., b'T1', 3, 0, -971058832),
(1, 0, 0., b'T1', 3, 0, -971058800),
(1, 0, 0., b'T1', 3, 0, -971058768)]
And this is the best I can get
[(1, 0, 0., b'T1', array([ 0, 10, 20], dtype=int32)),
(1, 0, 0., b'T1', array([ 0, 10, 20], dtype=int32)),
(1, 0, 0., b'T1', array([ 0, 10, 20], dtype=int32))]
What is the problem in this code ? It shows memory dump error in runtime
#include<iostream>
using namespace std ;
int main()
{
int A[3][4] = {{3, 1, 8, 11}, {4, 12, 9, 10}, {7, 5, 2, 6}};
int **p = A;
P[1][2] = 99;
cout<<A[1][2] ;
}
Change your int **p = A[0][0] to int *p = &A[0][0]. In the next line, write the following *p = *((int*)p + 1 * NUM_OF_COLUMNS + 2) = 99;, where NUM_OF_COLUMNS is the number 4, instead of the P[1][2] = 99;. Correct the spelling of main as well as uppercase/lowercase of variables. Also add a return 0; at the end since you have an int main() and not a void.
you seem new to c++ or programming with a question like this one don't feel bad because pointers can be tricky and if you don't know you don't know. I am pretty sure this will help you. Remember to pick the best answer :).
#include <iostream>
using namespace std;
int main() {
int A[3][4] = { { 3, 1, 8, 11 }, { 4, 12, 9, 10 }, { 7, 5, 2, 6 } };
cout << "Before pointer change A[1][2] = " << A[1][2] << endl;
int *p; //Set pointer
p = &A[1][2]; //Set memory address to pointer don't forget '&'
*p = 99; //Change integer
cout << "After pointer change A[1][2] = " << A[1][2] << endl;
return 0; // you need a 'return 0;' because your main is int
}