Enviado em 02/01/2019 - 11:21h
Tenho o seguinte código:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int **m_malloc(size_t rows, size_t columns){
int **mat=malloc(sizeof(int*)*rows);
for(size_t i=0; i<rows; i++){
mat[i]=malloc(sizeof(int)*columns);
}
return mat;
}
void m_show(int *mat[], size_t rows, size_t columns){
printf("\n");
for(size_t i=0; i<rows; i++){
for(size_t j=0; j<columns; j++){
printf("[%d] ", mat[i][j]);
}
printf("\n");
}
}
void m_fill(int *mat[], size_t rows, size_t columns){
int value;
srand(time(NULL));
for(size_t i=0; i<rows; i++){
for(size_t j=0; j<columns; j++){
value=(int)rand()%100;
mat[i][j]=value;
}
}
}
void m_free(int *mat[], size_t columns){
for(size_t i=0; i<columns; i++){
free(mat[i]);
}
free(mat);
}
int main(void){
int **matrix;
size_t rows, columns;
printf("ROWS >");
scanf("%ld", &rows);
printf("COLUMNS >");
scanf("%ld", &columns);
matrix=m_malloc(rows, columns);
m_fill(matrix, rows, columns);
m_show(matrix, rows, columns);
m_free(matrix, columns);
return 0;
}
//matriz == matrix (in English) ???
localhost@MIG-21:~/Documents/C$ ./app
ROWS >5
COLUMNS >5
[5] [12] [13] [65] [26]
[91] [55] [29] [62] [50]
[46] [92] [60] [41] [29]
[2] [15] [5] [55] [13]
[60] [72] [0] [53] [0]
localhost@MIG-21:~/Documents/C$ ./app
ROWS >9
COLUMNS >3
[77] [35] [54]
[93] [83] [90]
[38] [59] [98]
[10] [58] [45]
[28] [84] [65]
[55] [65] [88]
[56] [50] [77]
[84] [5] [30]
[98] [55] [61]
localhost@MIG-21:~/Documents/C$ ./app
ROWS >1
COLUMNS >4
[30] [40] [16] [30]
Segmentation fault (core dumped)
tux-gpt - Assistente de IA para o Terminal
Instalação e configuração do Chrony
Programa IRPF - Guia de Instalação e Resolução de alguns Problemas
Como instalar no Linux Jogos da Steam só para Windows
Instalando o Team Viewer no Debian Trixie - problema no Policykit
O Que Fazer Após Instalar Ubuntu 25.04
Copiar Layout do Teclado para aplicar em outra Distribuição (5)
Alguém poderia me ajudar a escolher peças pra montar um desktop? [RESO... (34)