Passing 3D dynamic array by reference - c++

I'm trying to create a 3D char array with dynamic memory. I create the char*** point in main then pass it to input and everything works fine until the input function returns and i try to repring the same locaton from main. I get "Access violation reading location." Any suggestions?
void input(char ***a, int f, int n)
{
cin >> f;
cin >> n;
a = new char**[f];
for (int i =0; i <f; ++i)
{
a[i]= new char*[n];
for (int j=0; j <n; ++j)
{
a[i][j] = new char[n];
}
}
for (int i =0; i<f; ++i)
{
for (int j=0; j<n; ++j)
{
for (int k = 0; k <n ;++k)
{
a[i][j][k] = '.';
cout << a[i][j][k];}
}
}
cout <<endl << endl<< a[2][5][5]; //test to see is value is '."
}
int main()
{
char ***station = 0;
int floors=0, n=0;
input(station, floors, n);
cout << endl << endl << station[2][5][5];
}

Related

C++ Program with matrix class ending abruptly

I am writing a matrix class where I need to perform some matrix calculations in the main program. I am not sure why the program is ending abruptly when user chooses a matrix of size more than 2x2 matrix. The std::cin works fine until two rows but program ends after the loop reaches third row. Only part of the code is shown below which is related to my question.
#include<iostream>
#include <vector>
#include <cassert>
using std::vector;
using namespace std;
class Matrix {
private:
int rows;
int cols;
int **vtr;
public:
Matrix(int m = 2, int n = 2)
{
rows = m;
cols = n;
vtr = new int*[m];
for (int i = 0; i < m; i++)
{
vtr[i] = new int [n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
vtr[i][j] = 0;
}
}
}
void read()
{
cout << "Enter the number of rows and columns of Matrix separated by a space: ";
cin >> rows >> cols;
Matrix a(rows, cols);
a.write();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "(" << i << "," << j << ") : ";
cin >>vtr[i][j];
}
}
}
void write()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << vtr[i][j] << " ";
}
cout << endl;
}
cout << endl << endl;
}
};
int main()
{
Matrix A, B, C;
int row, column ;
cout << "For Matrix A" << endl;
A.read();
cout << "For Matrix B " << endl;
B.read();
cout << "For Matrix C" << endl;
C.read();
}
Since the 2D array, vtr is created when declaring the Matrix object, you can move the vtr creation after reading the console input like below.
Matrix(int m = 2, int n = 2)
{
/*rows = m;
cols = n;
vtr = new int*[m];
for (int i = 0; i < m; i++)
{
vtr[i] = new int [n];
}
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
vtr[i][j] = 0;
}
}*/
}
void read()
{
cout << "Enter the number of rows and columns of Matrix separated by a space: ";
cin >> rows >> cols;
vtr = new int*[rows];
for (int i = 0; i < rows; i++)
{
vtr[i] = new int [cols];
}
//Matrix a(rows, cols);
//write();
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
cout << "(" << i << "," << j << ") : ";
cin >>vtr[i][j];
}
}
write(); //Prints the array
}
The three matrixs will be construct when you define Matrix A, B, C. So the matrix size is 2x2. When you call function cin to assign value to some position is not in 2x2 will not work.

Segmentation Fault in dynamically array creation

I was making a cpp program in which it takes two input from the user which determine the size of the 2d array and pass the values to the mat class constructor and dynamically create an array of the user's defined size. But, I don't know why it is not working and showing segmentation fault
#include<iostream>
using namespace std;
class mat{
int **a;
int r, c;
public:
mat(int row, int col){
r = row;
c = col;
for(int i = 0; i < r; i++){
*a = new int[r];
*a++;
}
}
void input(){
for(int i = 0; i < r; i++){
for(int j = 0; i < c; j++){
cin >> a[i][j];
}
}
}
void display(){
for(int i = 0; i < r; i++){
for(int j = 0; i < c; j++){
cout << a[i][j] << "\t";
}
cout << endl;
}
}
};
int main()
{
int r, c;
cout << "enter row :";
cin >> r;
cout << "enter column :";
cin >> c;
mat m(r, c);
m.input();
cout << "array \n";
m.display();
}
I can feel that the issue is with the for loop in the constructor or maybe I am doing it wrong.
The class contains several errors.
The variable a is never initialized. When we try to address the memory pointed to by a we get a segmentation fault. We can initialize it like this a = new int*[r]
We should not change where a point's to, so don't use a++. Otherwise a[i][j] will not refer to the i'th row and the j'th column. We would also want to release the memory at some point.
The inner loop for the columns for(int j = 0; i < c; j++) once entered will never terminate and will eventually produce a segmentation fault. We need to change i < c to j < c.
If we fix these errors, it looks like this:
class mat {
int** a;
int r, c;
public:
mat(int row, int col) {
r = row;
c = col;
a = new int*[r];
for (int i = 0; i < r; i++) {
a[i] = new int[c];
}
}
void input() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cin >> a[i][j];
}
}
}
void display() {
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
cout << a[i][j] << "\t";
}
cout << endl;
}
}
};

C++ matrix output using ofstream

I am trying to make the function output numbers in a form of a matrix, not just a line
void SaveMatrix(TMatrix* mat){
ofstream SaveM;
SaveM.open("Matrix.txt", ios::out);
if (SaveM.is_open()){
for (int i=0; i<mat->line; ++i){
for (int j=0; j<mat->column; ++j){
SaveM<< mat->m[i][j]<<" ";
}
}
}else{
cout<<"file is open"<<endl;
}
}
I tried to put this in the second for-cycle, without a result
if(j==mat->column){
SaveM<<endl;
}
The matrix is declared:
struct TMatrix {
double* *m;
int line;
int column;
};
j will never reach mat->column in your inner for loop.
I think you could use something like this:
for (int i = 0; i < mat->line; i++){
SaveM << "[";
for (int j = 0; j < mat->column; j++){
SaveM << mat->m[i][j]);
if (j < sizeMatrix - 1){
SaveM << ", ";
}
}
SaveM << "]" << endl;
}

Gauss Elimination C++ segmentation fault

I'm really stuck with my code for Gauss Elimination in C++, I need to return upper triangular matrix but still only thing I get is Segmentation fault. I know there must be some sort of going of allocated memory but I can't find where.
Code:
#include <iostream>
using namespace std;
double ** allocateDynamicArray(int order){
double ** dynArray = new double *[order];
int cols = order+1;
double *pool = new double [order * cols];
for(int i = 0;i < order; i++, pool += cols){
dynArray[i] = pool;
}
return dynArray;
}
void deallocateDynamicArray(double **dynArray){
delete [] dynArray[0];
delete [] dynArray;
}
void addAndPrintArray(double **dynArray, int order){
cout << "Zadejte prvky pole radu " << order << endl;
for(int i=0; i< order; i++){
for(int j=0; j< order+1; j++){
cout << "Pole[" << i << "][" << j << "]: ";
cin >> dynArray[i][j];
}
}
for(int i=0; i< order; i++){
for(int j=0; j< order+1; j++){
if(dynArray[i][j] < 10 && dynArray[i][j] >= 0){
cout << " ";
}
cout << dynArray[i][j] << " ";
}
cout << endl;
}
}
double ** gaussElimination(double** dynArray, int order){
for(int j=1; j<=order; j++) /*Horni trojuhelnikova matice*/
{
for(int i=1; i<=order; i++)
{
if(i>j)
{
double c=dynArray[i][j]/dynArray[j][j];
for(int k=1; k<=order+1; k++)
{
dynArray[i][k] = dynArray[i][k] - c * dynArray[j][k];
}
}
}
}
return dynArray;
}
int main()
{
cout << "Zadejte rad matice: ";
int order;
cin >> order;
double **arr = allocateDynamicArray(order);
addAndPrintArray(arr, order);
gaussElimination(arr, order);
deallocateDynamicArray(arr);
return 0;
}
Can anyone tell me what's wrong?
The problem is that in C/C++ the first element of an array should have index 0, so your
for(int i=1; i<=order; i++)
should be
for(int i=0; i<order; i++)
in the gaussElimination function.

Segmentation Fault In Probability Calculator

I'm compiling this on linux. It will compile and run, but when I enter values for n and p, this is what my terminal looks like:
7
1.0
Segmentation fault (core dumped)
In this case, 7 is the input for n, and 1.0 is the input for p. I've tried this with different values. The idea is to use Dynamic Programming to fill in a 2D array of probabilities through recursion. Let me know if you need more info, but this is the entirety of the code.
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int n;
double p;
cin >> n;
cin >> p;
cout << n;
cout << p;
cout << "Initializing array.";
double** probability = new double*[n];
for(int i = 0; i < n; ++i)
{
probability[i] = new double[n];
}
//cout << "Beginning filling i loop.";
for(int i = 0; i < n; i++)
{
probability[i][0] = 0;
}
//cout << "Beginning filling j loop.";
for(int j = 0; j < n; j++)
{
probability[0][j] = 1;
}
//cout << "Beginning filling nested loop.";
for(int i = 1; i< n; i++)
{
for(int j = 1; j< n; j++)
{
probability[i][j] = (p * probability[i-1][j]) + ((1-p) * probability[i][j-1]);
}
}
cout << "Probability: ";
cout << probability[n][n];
//cleanup
for(int i = 0; i < n; ++i)
{
delete probability[i] ;
}
delete probability;
return 0;
}
cout << probability[n][n];
probability[][] is an n by n array. The last element is probability[n-1][n-1] , so you are running off the end of the array and invoking undefined behavior.