• 0

C++ Help with program


Question

Ok, so for a school project I have coded the following classes:

matriz.h

#ifndef MATRDATOS_H_
#define MATRDATOS_H_

#include <stdio.h>
#include <iostream>

// Clases de Error Handler
class MatrizFueradePlano: public exception { }; // Index out of the Array
class MatrizValorNoValido: public exception { }; // Unsupported value ( supported only 0 or 1 )

class MatrizDatos {
public:
	MatrizDatos( int SizeX, int SizeY ); // Normal Constructor
	MatrizDatos( MatrizDatos & mat ); // Copy Constructor
	~MatrizDatos(); // Destructor
	int LengthX() { return MaxX; } // Size in X (rows)
	int LengthY() { return MaxY; } // Size in Y (cols)
	void SetValor( int indexA, int indexB, int valor ); // Set Data
	int GetValor( int indexA, int indexB ); // Get Data
	void ImprimeMatriz(); // Print Matrix
private:
	int **matriz;
	int MaxY;
	int MaxX;
};

#endif

matriz.cpp

#include "matriz.h"

MatrizDatos::MatrizDatos( int SizeX, int SizeY ) {
	int i, j;

	MaxX = SizeX; // Define Size
	MaxY = SizeY;
	matriz = new int*[MaxX]; // Create Matrix of MaxX Rows
	for( i = 0; i < MaxX; i++ ) {
 ?matriz[i] = new int[MaxY]; // Create Matrix (i) of MaxY Cols
	}

	for( i = 0; i < MaxX; i++ ) {
 ?for( j = 0; j < MaxY; j++ ) {
 ?	matriz[i][j] = 0; // Make default value to 0
 ?}
	}
}

MatrizDatos::MatrizDatos( MatrizDatos & mat ) {
	int i, j;

	// Duplicate
	MaxX = mat.LengthX();
	MaxY = mat.LengthY();
	matriz = new int*[MaxX];
	for( i = 0; i < MaxX; i++ )
 ?matriz[i] = new int[MaxY];

	// Copy Data
	for( i = 0; i < MaxX; i++ ) {
 ?for( j = 0; j < MaxY; j++ ) {
 ?	matriz[i][j] = mat.GetValor( i, j );
 ?}
	}

}

int MatrizDatos::GetValor( int indexA, int indexB ) {
	// Try to see if index is valid in X (rows)
	if( (indexA < 0) || (indexA >= MaxX) ) {
 ?throw MatrizFueradePlano();
	}

	// Try to se if index is valid in Y (cols)
	if( (indexB < 0) || (indexB >= MaxY) ) {
 ?throw MatrizFueradePlano();
	}

	// Return correct data
	return matriz[indexA][indexB];
}

void MatrizDatos::SetValor( int indexA, int indexB, int valor ) {
	// Try to see if index is valid in X (rows)
	if( (indexA < 0) || (indexA >= MaxX) ) {
 ?throw MatrizFueradePlano();
	}

	// Try to see if index is valid in Y (cols)
	if( (indexB < 0) || (indexB >= MaxY) ) {
 ?throw MatrizFueradePlano();
	}

	// Try to see correct value 0 or 1
	if( (valor != 0) && (valor != 1) ) {
 ?throw MatrizValorNoValido();
	}

	// Set Data
	matriz[indexA][indexB] = valor;
}

void MatrizDatos::ImprimeMatriz() {
	int i, j;
	for( i = 0; i < MaxX; i++ ) {
 ?for( j = 0; j < MaxY; j++ ) {
 ?	printf("%d ", matriz[i][j] );
 ?}
 ?printf("\n");
	}
}

// Destructor
MatrizDatos::~MatrizDatos() {
	int i;
	for( i = 0; i < MaxX; i++ )
 ?delete [] *matriz;
	delete [] matriz;
}

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "matriz.h"

int main( int argc, char **argv ) {
	int randval, i, j, realval, errores;
	int superior, inferior, derecha, izquierda;

	MatrizDatos *mymat;
	// Initialize Data
	mymat = new MatrizDatos( 20, 20 );
	errores = 0;
	randval = 0;
	realval = 0;
	printf("Programa de Matrices!\n");
	for( i = 0; i < mymat->LengthX(); i++ ) {
 ?for( j = 0; j < mymat->LengthY(); j++ ) {
 ?	
 ?	// Get Random Value
 ?	srand( (unsigned int)clock());
 ?	randval = rand() % 10;
 ?
 ?	// Generate Valid Value
                // This code is from my Professor
 ?	if( randval < 5 ) realval = 0;
 ?	else realval = 1;

 ?	// Find out adjacent values Top, Bottom, Left, Right
 ?	try {
 ? ?superior = mymat->GetValor( i + 1, j );
 ?	} catch( MatrizFueradePlano &m ) {
 ? ?errores++;
 ?	}
 ?	try {
 ? ?inferior = mymat->GetValor( i - 1, j );
 ?	} catch( MatrizFueradePlano &m ) {
 ? ?errores++;
 ?	}
 ?	try {
 ? ?derecha = mymat->GetValor( i , j + 1 );
 ?	} catch( MatrizFueradePlano &m ) {
 ? ?errores++;
 ?	}
 ?	try {
 ? ?izquierda = mymat->GetValor( i, j - 1 );
 ?	} catch( MatrizFueradePlano &m ) {
 ? ?errores++;
 ?	}
 ?	
 ?	// Check values
 ?	try {
 ? ?if( ( realval != superior ?) && ( realval != inferior ) && ( realval != derecha ) && ( realval != izquierda ) ) {
 ? ?	mymat->SetValor( i, j, realval );
 ? ?} else {
 ? ?	//realval = mymat->GetValor( i, j );
 ? ?	//realval = ( realval == 1 ) ? 0 : 1;
 ? ?	//mymat->SetValor( i, j, realval );
 ? ?}
 ?	} catch( MatrizFueradePlano &n ) {
 ? ?errores++;
 ?	} catch( MatrizValorNoValido &j ) {
 ? ?printf("Valor no valido\n");
 ?	}
 ?}
	}
	mymat->ImprimeMatriz();
	//printf("Errores generados: %d\n", &errores);
	system("pause");
	return 0;
}

the problem:

the first run it does this: (output)

1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0 0 0 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 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 :wacko:r />

So, does any one know why it does this? I'm kind of stuck :wacko:

Edited by GatorV
Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

Oops sorry, yes it's spanish let me translate, and the purpose of the class this is a begining program to simulate the growth of a diamond, its going to use mapping functions to translate a 3D object into a array o arrays...

Link to comment
Share on other sites

  • 0

The output should be like the second one:

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0

0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1

This is only for a start, because nex month I have to do it with 3D Arrays :wacko:

Link to comment
Share on other sites

This topic is now closed to further replies.