Last active
February 10, 2018 23:00
-
-
Save iordic/f89b7dfcbe0bc0d045214f14aaf97915 to your computer and use it in GitHub Desktop.
Dinamic matrix with memory allocation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
int main() { | |
// Declare constant sizes | |
const int rows = 8; | |
const int cols = 6; | |
// Seed for random numbers generation | |
srand(time(NULL)); | |
// Pointer for the matrix | |
int** ptr = (int**) malloc(sizeof(int*) * rows); | |
for (int i = 0; i < rows; i++) { | |
// Initialise pointer for every row | |
ptr[i] = (int*) malloc(sizeof(int) * cols); | |
for (int j = 0; j < cols; j++) { | |
ptr[i][j] = rand() % 10; | |
} | |
} | |
// Printing all the matrix | |
for (int i = 0; i < rows; i++) { | |
for (int j = 0; j < cols; j++) { | |
printf("%d ", ptr[i][j]); | |
} | |
printf("\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment