Jump to content

Hello everyone, so again i have some problems with an exercise from my programming class. So, this exercise is about make a N number of structures and inside of this structures it's going to have a dynamic matrix. So it's going to be like this, it's going to make a question like "How many structures" and then put a N number, from that number it's going to ask for the rows and columns for the N matrices and then print it.

So far i have this code (again sorry if the functions are in spanish:D), and i would like to know what i could add of modify from my code to correct the possible errors or if anyone knows a better way to do it i would be glad to hear it :)

#include <stdio.h>
#include <stdlib.h>
typedef struct{
	int ren;
	int col;
	int **iMat;
}Matriz;
void memoriaStructura(Matriz ***matriz,int *n);
void crearMatriz(Matriz ***matriz,int *n);
void crearMatriz2(int ***iMat,Matriz *matriz);
void llenarMatriz(Matriz **matriz,int **iMat,int *n);
void imprimeMatriz(Matriz **matriz,int **iMat,int *n);
void destruyeMatriz(Matriz **matriz,int **iMat,int *n);
void destruyeEstructura(Matriz **matriz,int *n);
int main(void){
	int n;
	Matriz **matriz;
	memoriaStructura(&matriz,&n);
	crearMatriz(&matriz,&n);
	destruyeEstructura(matriz,&n);
	return 0;
}
void memoriaStructura(Matriz ***matriz,int *n){ //memory allocation
	printf("Numero de estructuras a realizar: ");
	scanf("%d",n);
	*matriz=(Matriz**)malloc(*n*sizeof(Matriz*));
	printf("\n\nIngrese el tamaño de las matrices: \n");
	for(int i=0;i<*n;i++){
		printf("Renglon de matriz %d: ",i+1);
		scanf("%d",&(*matriz)[i]->ren);
	}
	for(int i=0;i<*n;i++){
		printf("Columnas de la matriz %d: ",i+1);
		scanf("%d",&(*matriz)[i]->col);
	}
}
void crearMatriz(Matriz ***matriz,int *n){ //matrix creation
	(*matriz)=(Matriz**)malloc(sizeof(Matriz*));
	for(int i=0;i<*n;i++){
		(*matriz)[i]=(Matriz*)malloc(sizeof(Matriz));
		crearMatriz2(&((*matriz)[i]->iMat),(*matriz)[i]);
	}
}
void crearMatriz2(int ***iMat,Matriz *matriz){ //matrix memory allocation
	int **matTmp=(int**)malloc(matriz->ren*sizeof(int*));
	for(int i=0;i<matriz->ren;i++){
		matTmp[i]=(int*)malloc(matriz->col*sizeof(int));
		}
	*iMat=matTmp;
	}
void llenarMatriz(Matriz **matriz,int **iMat,int *n){ //numbers for the matrices
	for(int i=0;i<*n;i++){
		crearMatriz(&(*matriz)[i],n);
		for(int r=0;r<matriz[i]->ren;r++){
			for(int c=0;c<matriz[i]->col;c++){
				iMat[r][c]=(r*matriz[i]->col)+c;
			}
		}
	imprimeMatriz(&matriz[i],matriz[i]->iMat,n); //i can't seem to make it work this 
	}
}
void imprimeMatriz(Matriz **matriz,int **iMat,int *n){ //print matrices
	for(int i=0;i<*n;i++){
		for(int r=0;r<matriz[i]->ren;r++){
			for(int c=0;c<matriz[i]->col;c++){
				printf("%d ",iMat[r][c]);
			}
		printf("\n");
		}
	printf("\n");
//	destruyeMatriz(matriz[i]->iMat,matriz[i],n);
	}
}
void destruyeMatriz(Matriz **matriz,int **iMat,int *n){ //memory from matrices
	for(int i=0;i<*n;i++){
		for(int r=0;r<matriz[i]->ren;r++){
			free(iMat[i]);
		}
		free(iMat);
	}
}
void destruyeEstructura(Matriz **matriz,int *n){ //free memory for structures
	for(int i=0;i<*n;i++){
		free(matriz[i]);
	}
	free(matriz);
}

Thanks in advance!

 

EDIT: I figure it out how to do the exercise.

Temporary using a Dell Inspiron 14" laptop:

CPU: Intel Core i5 6200U | Memory: 8GB DDR3 | GPU: AMD R5 M315 2GB  | Storage: Western Digital 1TB 5400RPM

 

CPU: Intel Core i5 3570k | Motherboard: Asrock H61M-DGS | Memory: 8GB 1600MHz DDR3 | GPU: MSI GTX 650Ti Boost 2GB | Case: NZXT Source 210 | Storage: Samsung 1TB HDD | PSU: Seasonic X-Series 650W

Link to comment
https://linustechtips.com/topic/597640-dynamic-matrix-in-a-dynamic-structuresolved/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×