pointer matrix error on execution - c++

class Matrix{
double **val;
int rows,cols,errorCode;
public:
Matrix();
Matrix(int);
Matrix(int, int);
~Matrix();
void Print();
void Read();
double Get(int, int);
void Assign(int,int,double);
void Addition(Matrix&);
void Subtraction(Matrix&);
Matrix Multiply(Matrix&);
void Multiply(double);
};
Matrix::Matrix(){
//First array go with rows.
val = new double*[1];
val[1] = new double[1];
rows = 1;
cols = 1;
errorCode = 0;
}
Matrix::Matrix(int n){
rows = n;
cols = n;
val = new double*[rows];
for(int i = 0;i<rows;i++){
val[i] = new double[cols];
}
}
Matrix::Matrix(int n,int m){
rows = n;
cols = m;
val = new double*[rows];
for(int i = 0;i<rows;i++){
val[i] = new double[cols];
}
}
Matrix::~Matrix(){
for(int i=0;i<rows;i++){
delete[] val[i];
}
delete[] val;
}
Matrix Matrix::Multiply(Matrix &a){
if(cols != a.rows){
Matrix b;
b.errorCode=111; //That means dimensions are not valid;
return b;
}
else{
Matrix b(rows,a.cols);
double p;
for(int i=0;i<rows;i++){
for(int j=0;j<a.cols;j++){
p=0;
for(int k=0;k<cols;k++){p += val[i][k]*a.val[k][j];}
b.Assign(i,j,p);
}
}
return b;
}
}
When i compile it seems to be ok, but when i run it doesn't work and i don't understand why...it says "program stoped worked" and returned error 255
If needed i can put all the code i write...
I added constructors...
unfutunatly i can not post images... it says process returned 255, and here is an alert from windows that the program was not working corectly and it stoped working...
Here is all the code if someone have time and if it helps you to understand better..
#include <iostream>
using namespace std;
class Matrix{
double **val;
int rows,cols,errorCode;
public:
Matrix();
Matrix(int);
Matrix(int, int);
~Matrix();
void Print();
void Read();
double Get(int, int);
void Assign(int,int,double);
void Addition(Matrix&);
void Subtraction(Matrix&);
Matrix Multiply(Matrix&);
void Multiply(double);
};
Matrix::Matrix(){
//First array go with rows.
val = new double*[1];
val[0] = new double[1];
rows = 1;
cols = 1;
errorCode = 0;
}
Matrix::Matrix(int n){
rows = n;
cols = n;
val = new double*[rows];
for(int i = 0;i<rows;i++){
val[i] = new double[cols];
}
}
Matrix::Matrix(int n,int m){
rows = n;
cols = m;
val = new double*[rows];
for(int i = 0;i<rows;i++){
val[i] = new double[cols];
}
}
Matrix::~Matrix(){
for(int i=0;i<rows;i++){
delete[] val[i];
}
delete[] val;
}
void Matrix::Read(){
for(int i=0;i<rows;i++){
delete[] val[i];
}
delete[] val;
cout<<"Give the matrix dimensions (rows and cols) : ";
cin>>rows>>cols;
val = new double*[rows];
for(int i = 0;i<rows;i++){
val[i] = new double[cols];
}
cout<<"Give the matrix values: "<<endl;
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){ cin>>val[i][j];}
}
}
void Matrix::Print(){
cout<<"The matrix with "<<rows<<" rows and "<<cols<<" cols, have next values: "<<endl;
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){ cout<<val[i][j]<<" ";}
cout<<endl;
}
}
double Matrix::Get(int n, int m){
return val[n-1][m-1];
}
void Matrix::Assign(int n,int m,double v){
val[n-1][m-1] = v;
}
void Matrix::Addition(Matrix &a){
if(rows != a.rows || cols !=a.cols){
errorCode=111; //That means dimensions are not equal;
}
else{
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){ val[i][j] += a.val[i][j];}
}
}
}
void Matrix::Subtraction(Matrix &a){
if(rows != a.rows || cols !=a.cols){
errorCode=111; //That means dimensions are not equal;
}
else{
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){ val[i][j] -=a.val[i][j];}
}
}
}
void Matrix::Multiply(double x){
for(int i = 0;i<rows;i++){
for(int j = 0;j<cols;j++){ val[i][j] *= x;}
}
}
Matrix Matrix::Multiply(Matrix &a){
if(cols != a.rows){
errorCode=111; //That means dimensions are not equal;
Matrix b;
return b;
}
else{
Matrix b(rows,a.cols);
double p;
for(int i=0;i<rows;i++){
for(int j=0;j<a.cols;j++){
p=0;
for(int k=0;k<cols;k++){p += val[i][k]*a.val[k][j];}
b.Assign(i,j,p);
}
}
return b;
}
}
int main(){
Matrix a,b(2,3);
b.Assign(1,1,0);
b.Assign(1,2,3);
b.Assign(1,3,5);
b.Assign(2,1,5);
b.Assign(2,2,5);
b.Assign(2,3,2);
b.Print();
a.Read();
a.Print();
cout<<endl;
//when i read a i put 3x2 matrix, so the multiplying can be done.
Matrix c=a.Multiply(b);
c.Print();
return 0;
}
problem appear when i try to use function: Matrix Multiply(Matrix&).

There's an error in your default constructor:
val = new double*[1];
val[1] = new double[1];
1 is not a valid index for val, this should be:
val[0] = new double[1];

Related

I don't understand what's wrong

Here is a my own Matrix class:
class Matrix {
private:
int row; // rows
int col; // columns
double** matrix; // pointer to this matrix
public:
Matrix();
Matrix(int row, int col);//creating matrix based on two params
Matrix(double** matx, int row, int col); //Here is a problem
Matrix(Matrix& N);//copy constructor
~Matrix();
//first, second and fourth constructors works good
int getRow();
int getCol();
void changeSize(int row, int col);
void randValues();
void writeValues();
};
Here is a body of constructor, This constructor need take a exists matrix as parameter and create a new one based on the matrix provided (it's NOT a copy constructor)
Matrix::Matrix(double** matx, int row, int col){
//allocated memory for new matrix
matrix = new double*[row];
for (int i = 0; i < row; i++){
matrix[i] = new double[col];
}
// //copy values to new matrix
for(int i=0; i<row; i++)
{
for(int k = 0; k < col; k++)
{
matrix[i][k] = matx[i][k];
}
}
};
int main(){
double** test = new double *[5];
for(int i = 0; i < 5; i++){
test[i] = new double;
}
for(int i = 0; i < 5; i++){
for(int k = 0; k < 5; k++){
test[i][k] = 0.11;
cout << test[i][k] << " ";//just for make me sure if is ok
}
cout << "\n";
}
Matrix *matx = new Matrix(test,5,5);
matx->writeValues();
return 0;
}
And when I run a program they write on console lots of zeros values and few garbage and of the end is: Process returned -1073741819 (0xC0000005) execution time : 2.162 s

"terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc" But it is strange, I have a lot of available memory

I wrote a class, and than I used this to create another one and used both in a test program. My second class uses dynamic allocation so I followed the recommendations and create The Big Three, a creator, a copy creator and a destructor. But when I try to use a class friend operator the program stops and return the message above. Let me show some code:
class matrix
{
public:
int n;
complex **M;
matrix(int dim); //construtor
matrix(int dim, double a);//construtor
matrix(const matrix &obj);//construtor de copia (necessario para passar objetos para funções)
~matrix(); //destrutor
friend matrix operator *(matrix a, matrix b);
};
matrix::matrix(int dim)
{
int i = 0;
n = dim;
M = new complex*[dim];
cout << "criando objeto \n";
for(i=0;i<n;i++)
{
M[i] = new complex [dim];
}
}
matrix::matrix(int dim, double a)
{
int i = 0, j = 0;
n = dim;
M = new complex*[dim];
cout << "criando objeto \n";
for(i=0;i<n;i++)
{
M[i] = new complex [dim];
}
for(i=0;i <dim; i++){
for(j=0; j<dim; j++){
M[i][j] = complex(a,0);
}
}
}
matrix::matrix(const matrix &obj)
{
int i = 0, j = 0;
M = new complex*[n];
cout << "criando copia de objeto \n";
for(i=0;i<n;i++)
{
M[i] = new complex [n];
}
for(i=0;i <n; i++){
for(j=0; j<n; j++){
M[i][j] = obj.M[i][j];
}
}
n = obj.n;
}
matrix::~matrix()
{
int i;
cout << "destruindo obejto \n";
for(i=0;i<n;i++)
delete [] M[i];
delete M;
}
matrix operator *(matrix a, matrix b)
{
int i,j,k;
matrix mult(a.n, 0);
bool dim;
if(a.n == b.n) dim = true;
else dim = false;
if(dim == true)
{
for(i=0; i < a.n; i++){
for(j=0; j < a.n; j++){
for(k=0; k < a.n; k++){
mult.M[i][j] = mult.M[i][j] + a.M[i][k]*b.M[k][j];
}
}
}
return mult;
}
else return zeros(a.n);
}
Well, I really don't know why I'm getting this error message, I would be grateful if anyone could help me to solve this out.

How to create a function to change a value in a 2D dynamic array

I've been tasked with creating a class for a two dimensional dynamic array and I am stuck at a part where I need to create a member function which I can call upon to change a specific position in the 2D array to a value of my choosing.
class TwoD
{
public:
void setRowCol(int numRow, int numCol, double value);
TwoD();
TwoD(int row, int col);
void fillArray();
void outputArray();
private:
double** dArray = new double* [MaxRow];
int MaxRow;
int MaxCol;
};
TwoD::TwoD()
{
MaxCol = 3;
MaxRow = 3;
for (int i = 0; i < 3; i++)
{
dArray[i] = new double[3];
}
}
void TwoD::setRowCol(int numRow, int numCol, double value)
{
dArray[numRow][numCol] = value;
}
So the part I am having trouble with is the last part with the function setRowCol. I think that the problem is that it's not passing a 'deep' copy of the array but rather the original array itself.
Make the MaxRow as static variable & initialize it to appropriate value(3) before using it to create dArray.
Without std::vector, and using double**, you may have something like:
class TwoD
{
public:
TwoD(int row, int col) : maxRow(row), maxCol(col) {
dArray = new double* [maxRow];
for (int i = 0; i != maxRow; ++i) {
dArray[i] = new double[maxCol] {};
}
}
~TwoD() {
Delete();
}
TwoD(const TwoD& rhs) : maxRow(rhs.maxRow), maxCol(rhs.maxCol) {
dArray = new double* [maxRow];
for (int i = 0; i != maxRow; ++i) {
dArray[i] = new double[maxCol];
for (int j = 0; j != maxCol; ++j) {
dArray[i][j] = rhs.dArray[i][j];
}
}
}
TwoD& operator = (const TwoD& rhs) {
if (&rhs == this) {
return *this;
}
Delete();
maxRow = rhs.maxRow;
maxCol = rhs.maxCol;
dArray = new double* [maxRow];
for (int i = 0; i != maxRow; ++i) {
dArray[i] = new double[maxCol];
for (int j = 0; j != maxCol; ++j) {
dArray[i][j] = rhs.dArray[i][j];
}
}
}
void setRowCol(int numRow, int numCol, double value) {
dArray[numRow][numCol] = value;
}
private:
void Delete() {
for (int i = 0; i != maxRow; ++i) {
delete [] dArray[i];
}
delete [] dArray;
}
private:
double** dArray;
int maxRow;
int maxCol;
};

Access violation in my 2d array class

When I debug the following code, I am told that there is an access violation. I tried changing this->arr[i][j] in the assignment operator to *(this->arr[i][j]) but that didn't work either as I was told that it was an illegal indirection.
Header
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
public:
Matrix(int rSize=3, int cSize=3);
Matrix(const Matrix& m);
~Matrix();
bool setValue(int rSize, int cSize, int value);
bool getValue(int rVal, int cVal, int& value)const;
Matrix& operator= (const Matrix& m);
private:
int rowSize;
int columnSize;
int** arr;
};
Source
#include<iostream>
#include<cmath>
#include"Matrix.h"
using namespace std;
Matrix::Matrix(int rSize,int cSize)
{
columnSize = cSize;
rowSize = rSize;
arr = new int* [rowSize];
for(int i=0; i<rowSize; i++)
arr[i] = new int[columnSize];
for(int j=0; j<rowSize; j++)
{
for(int k=0; k<columnSize; k++)
arr[j][k] = 0;
}
}
Matrix::Matrix(const Matrix& m)
{
columnSize = m.columnSize;
rowSize = m.rowSize;
arr = new int* [rowSize];
for(int i=0; i<rowSize; i++)
{
arr[i] = new int [columnSize];
}
for(int i=0; i<rowSize; i++)
{
for(int j=0; j<columnSize; j++)
arr[i][j] = m.arr[i][j];
}
}
Matrix::~Matrix()
{
for(int i = 0; i < rowSize; ++i)
delete [] arr[i];
delete [] arr;
}
bool Matrix::setValue(int rVal, int cVal, int value)
{
if((rVal<0)||(cVal<0)||(rVal>rowSize-1)||(cVal>columnSize-1))
return false;
arr[rVal][cVal] = value;
return true;
}
bool Matrix::getValue(int rVal, int cVal, int& value)const
{
if((rVal<0)||(cVal<0)||(rVal>rowSize-1)||(cVal>columnSize-1))
return false;
value = arr[rVal][cVal];
return true;
}
Matrix& Matrix::operator= (const Matrix& m)
{
if(&m == this)
return(*this);
if(((this->rowSize)!= m.rowSize) || ((this->columnSize) != m.columnSize))
{
for(int i=0; i<rowSize; i++)
delete []arr[i];
delete[]arr;
rowSize = m.rowSize;
columnSize = m.columnSize;
arr = new int* [m.rowSize];
for(int r=0; r<rowSize; r++);
}
for(int j=0; j<rowSize; j++)
{
for(int k=0; k<columnSize; k++)
this->arr[j][k] = m.arr[j][k];
}
return(*this);
}
Driver
#include<iostream>
#include"Matrix.h"
using namespace std;
void main()
{
Matrix m(4, 5);
Matrix m2(m);
m2.setValue(1,2,12);
int x;
m2.getValue(1,2,x);
Matrix m3;
m3 = m2;
}
Matrix& Matrix::operator= (const Matrix& m)
for(int r=0; r<rowSize; r++);
You forgot to allocate :
arr[r] = new int [columnSize];

Error occurring when trying to compare two .txt images in C++

As part of my University assignment we are required to compare two images in the form of .txt one is the normal image, the other is a jumbled version of the image.
Both images are 512x512 and we are asked to consider that they have been broken into 16x16 blocks which means each block is 32x32.
To compare the two images i'm using a sum of squared differences algorithm on blocks of the image. Starting from the top left and working my way round until an appropriate math is found. However this is where my issue is occurring, after successfully taking a single block from the unjumbled image and comparing it to another block from the jumbled image, the second iteration causes the following error:
Windows has triggered a breakpoint in Assignment1.exe.
This may be due to a corruption of the heap, which indicates a bug in
Assignment1.exe or any of the DLLs it has loaded.
My question is what is causing the error in my code?
The following is my header file called Matrix.h it converts the 2d matrix obtained from the txt file to a 1d image.
#pragma
#ifndef MATRIX_H
#define MATRIX_H
class Matrix
{
protected:
int M;
int N;
double* data;
Matrix(){M = 0; N = 0;data = 0;} //Constructor to avoid error
public:
Matrix(int sizeR, int sizeC, double* input_data); //Constructor
Matrix(int sizeR, int sizeC);
~Matrix(); //Destructor
Matrix(const Matrix& existingMatrix); //Copy Constructor
double get (int i , int j) const; //Returns value at specified location
const void set(int i, int j, double& val); //Changes value at specified location
int getM() const; //Return value of M
int getN() const; //Return value of N
Matrix getBlock(int startRow, int endRow, int startColumn, int endColumn); //Return section of Matrix
Matrix operator + (const Matrix& B);
Matrix operator = (const Matrix& B);
Matrix operator - (const Matrix& B);
Matrix operator * (const Matrix& B);
Matrix operator / (const Matrix& B);
Matrix operator ++ ();
double operator () (int i, int j);
void out();
double sum();
};
class BinaryImage
:public Matrix
{
public:
BinaryImage(int sizeR, int sizeC, double* input_data, double thresh);
~BinaryImage();
BinaryImage(const Matrix& rhs, double thresh);
BinaryImage(const BinaryImage& existingBinIm);
const void set(int i, int j, double& val);
};
#endif
This next section is my Main, the method we are using in the case of the error is called shuffledLogo().
#include<iostream>
#include<Windows.h>
#include<fstream>
#include<string>
#include"Matrix.h"
using namespace std;
void shuffledLogo();
double* readTXT(char* fileName, int sizeR, int sizeC);
void writePGM(char* fileName, Matrix& toWrite, int Q);
int main()
{
int selection;
cout<<"Select a program to run:"<<endl<<"1 - Logo Reorganisation"<<endl<<"2 - Where's Wally"<<endl<<"Selection: ";
cin>>selection;
cout<<endl;
switch(selection)
{
case 1:
cout<<"Logo Reorganisation Commencing..."<<endl;
shuffledLogo();
break;
case 2:
cout<<"Where's Wally Commencing..."<<endl;
break;
default:
cout<<"Selection invalid"<<endl;
break;
}
Sleep(1000);
return 0;
}
//Consructor
Matrix::Matrix(int sizeR, int sizeC, double* inputData)
{
M = sizeR;
N = sizeC;
data = new double [M*N];
for (int ii = 0; ii < M*N; ii++)
{
data[ii] = inputData[ii];
}
}
Matrix::Matrix(int sizeR, int sizeC)
{
M = sizeR;
N = sizeC;
data = new double [M*N];
for (int ii = 0; ii < M*N; ii++)
{
*(data+ii) = 0;
}
}
//Destructor
Matrix::~Matrix()
{
delete [] data;
}
//Copy Constructor
Matrix::Matrix(const Matrix& existingMatrix)
{
M = existingMatrix.getM();
N = existingMatrix.getN();
data = new double[M*N];
for (int ii = 0; ii < M; ii++)
{
for (int jj = 0; jj < N; jj++)
{
int k = ii*N+jj;
data[k] = existingMatrix.get(ii,jj);
}
}
}
//Pass by constant value
double Matrix::get (int i , int j) const
{
int k = i*N + j;
return data[k];
}
//Pass by Refrence
const void Matrix::set(int i, int j, double& val)
{
int k = i*N + j;
val = data[k];
}
//Return Value of M
int Matrix::getM() const
{
return M;
}
//Return Value of N
int Matrix::getN() const
{
return N;
}
//Return Section of the Matrix.
Matrix Matrix::getBlock(int startRow, int endRow, int startColumn, int endColumn)
{
int Row = endRow-startRow;
int Column = endColumn - startColumn;
double* block = new double[(Row)*(Column)];
int n = endColumn-startColumn;
for (int ii = startRow; ii < endRow; ii++)
{
for (int jj = startColumn; jj < endColumn; jj++)
{
int k = ii*n+jj;
block[k] = data[ii*N+jj];
}
}
Matrix t(Row,Column,block);
delete [] block;
return t;
}
//Allows for addion of Matricies, Operation Overloading.
Matrix Matrix::operator +(const Matrix& B)
{
Matrix C = Matrix(M, N, 0);
double temp;
for (int ii = 0; ii < M; ii++)
{
for (int jj = 0; jj < N; jj++)
{
temp = data[ii*N+jj] + B.get(ii,jj);
C.set(ii,jj, temp);
}
}
return C;
}
//Makes x and y equal, Opperation Overloading.
Matrix Matrix::operator =(const Matrix& B)
{
if (this == &B)
{
return *this;
}
else
{
M = B.getM();
N = B.getN();
delete [] data;
data = new double [M*N];
for (int ii = 0; ii < M; ii++)
{
for (int jj = 0; jj < N; jj++)
{
data[ii*N+jj] = B.get(ii,jj);
}
}
return *this;
}
}
//Allows for subtraction of matricies, Operation Overloading.
Matrix Matrix::operator -(const Matrix& B)
{
Matrix C = Matrix(M, N);
double temp;
for (int ii = 0; ii < M-1; ii++)
{
for (int jj = 0; jj < N-1; jj++)
{
temp = data[ii*N+jj] - B.get(ii,jj);
C.set(ii,jj, temp);
}
}
return C;
}
//Allows for multiplication of Matricies, Operation Overloading.
Matrix Matrix::operator *(const Matrix& B)
{
Matrix C = Matrix(M, B.getN());
double temp;
for (int ii = 0; ii < M; ii++)
{
for (int jj = 0; jj < N; jj++)
{
temp = data[ii*N+jj] * B.get(ii,jj);
C.set(ii,jj, temp);
}
}
return C;
}
//Allows for addition of Matricies, Operation Overloading.
Matrix Matrix::operator /(const Matrix& B)
{
Matrix C = Matrix(M, B.getN(), 0);
double temp;
for (int ii = 0; ii < M; ii++)
{
for (int jj = 0; jj < N; jj++)
{
temp = data[ii*N+jj] / B.get(ii,jj);
C.set(ii,jj, temp);
}
}
return C;
}
//Incrmentation of all values in Matrix by 1, Operation Overloading.
Matrix Matrix::operator ++()
{
for (int ii = 0; ii < M*N; ii++)
{
data[ii] = data[ii]++;
}
return *this;
}
//Allows calling of "get" function indirectly.
double Matrix::operator() (int i, int j)
{
return data[i*N+j];
}
double Matrix::sum()
{
double total = 0.0;
for (int ii = 0; ii < M*N; ii++)
{
total = total + data[ii];
}
return total;
}
void Matrix::out()
{
for (int ii = 0; ii < M*N; ii++)
{
if(data[ii] == 255)
cout<<"1 ";
else
cout<<data[ii]<<" ";
}
}
BinaryImage ::BinaryImage(int sizeR, int sizeC, double*input_data, double thresh)
:Matrix(sizeR, sizeC, input_data)
{
for(int ii = 0; ii < M*N; ii++)
{
if (data[ii] > thresh)
{
data[ii] = 1;
}
else
{
data[ii] = 0;
}
}
}
BinaryImage::BinaryImage(const Matrix& rhs, double thresh)
:Matrix(rhs)
{
for(int ii = 0; ii < M*N; ii++)
{
if (data[ii] > thresh)
{
data[ii] = 1;
}
else
{
data[ii] = 0;
}
}
}
BinaryImage::BinaryImage(const BinaryImage& rhs)
{
M = rhs.getM();
N = rhs.getN();
data = new double[M*N];
for (int ii = 0; ii<M; ii++){
for (int jj = 0; jj<N; jj++){
data[ii] = rhs.get(ii, jj);
}
}
}
const void BinaryImage::set(int i, int j, double& val)
{
int k = i*N + j;
val = data[k];
}
void shuffledLogo()
{
char* filePath2 = "E:\\logo_shuffled.txt";
double* basIm2 = readTXT(filePath2, 512, 512);
Matrix logoWithNoise(512, 512, basIm2);
cout<<"done"<<endl;
//Reads in the Shuffled Image
char* filePath = "E:\\logo_shuffled.txt";
double* basIm = readTXT(filePath, 512, 512);
Matrix logoShuffled(512, 512, basIm);
cout<<"done"<<endl;
Matrix logoUnshuffled(512,512);
double Score = 0.0; //Current "Pixels" score
double bestScore = 0.0; //Best "Pixel" score
int bestX, bestY = 0;
//For Loop to begin Sum of Squared Differences
for (int x = 0; x < 480; x+=32)
{
for (int y = 0; y < 480; y += 32)
{
Matrix subWithNoise = logoWithNoise.getBlock(x,(x+32),y,(y+32));
//subShuffled.out();
bestScore = 70000.0;
for (int xx = 0; xx< 480; xx+=32)
{
for (int yy = 0; yy < 480; yy+=32)
{
//Calculating the Score starts here
cout<<"loop started"<<endl;
int t = yy+32;
int l = xx+32;
Matrix subShuffled = logoShuffled.getBlock(xx,l, yy, t);
cout<<"submade"<<endl;
Matrix diff = subWithNoise - logoWithNoise;
Matrix product = diff*diff;
Score = diff.sum();
if (Score < bestScore)
{
bestScore = Score;
bestX = xx;
bestY = yy;
}
cout<<"loop ended"<<endl;
}
}
//For loop to put the best option into the final Matrix
for (int ii = bestX; ii < (bestX+=32); ii++)
{
for (int jj = bestY; jj < (bestY+=32); jj++)
{
double temp = logoShuffled.get(ii,jj);
logoUnshuffled.set(ii,jj,temp);
}
}
}
}
//Creates file
char* newFile = "finished.pgm";
writePGM(newFile, logoUnshuffled, 1);
}
//Reads in file and from specified location and builds it
double* readTXT(char* fileName, int sizeR, int sizeC)
{
double* input_data = new double[sizeR*sizeC];
int i =0;
ifstream currentFile(fileName);
if (currentFile.is_open())
{
while(currentFile.good())
{
if (i>sizeR*sizeC-1) break;
currentFile >> *(input_data+i);
i++;
}
currentFile.close();
}
else
{
cout<<"File path not found"<<endl;
}
return input_data;
delete [] input_data;
}
void writePGM(char* fileName, Matrix& toWrite, int Q)
{
int x = toWrite.getM();
int y = toWrite.getN();
unsigned char *image;
ofstream myfile;
image = (unsigned char *) new unsigned char [x*y];
// convert the integer values to unsigned char
for(int i = 0; i<x; i++)
{
for (int j = 0; j<y; j++)
{
image[i*y+j]=(unsigned char)toWrite.get(i,j);
}
}
myfile.open(fileName, ios::out|ios::binary|ios::trunc);
if (!myfile)
{
cout << "Can't open file: " << fileName << endl;
exit(1);
}
myfile << "P5" << endl;
myfile << y << " " << x << endl;
myfile << Q << endl;
myfile.write( reinterpret_cast<char *>(image), (x*y)*sizeof(unsigned char));
if (myfile.fail())
{
cout << "Can't write image " << fileName << endl;
exit(0);
}
myfile.close();
delete [] image;
}
I have taken the liberty of placing my folders in a public dropbox file for you to all see if you'd like to get a better look at the error. I understand that my code might be messy and poorly displayed so if anyone has any suggestions on how to make it better please say.
Dropbox to program files