Last active
May 19, 2021 06:32
-
-
Save bokunodev/456f61171674c2e0759e4005d948b46f to your computer and use it in GitHub Desktop.
Print 2D matrix
This file contains hidden or 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 <stdint.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
void print_padding(int n); | |
void print_2d_matrix(int* m, int size); | |
const int N = 5; | |
int main(int argc, char const* argv[]) { | |
int* my_matrix = malloc(N * N * sizeof(int)); | |
for (int i = 0; i < N; i++) { | |
for (int j = 0; j < N; j++) { | |
my_matrix[i * N + j] = j; | |
} | |
} | |
print_2d_matrix(my_matrix, N); | |
return 0; | |
} | |
void print_2d_matrix(int* m, int size) { | |
printf("[\n"); | |
for (int i = 0; i < size; i++) { | |
printf(" "); | |
print_padding(i * 3); | |
for (int j = i; j < size; j++) { | |
printf("[%d]", m[i * N + j]); | |
} | |
printf("\n"); | |
} | |
printf("]\n"); | |
} | |
void print_padding(int n) { | |
for (int i = 0; i < n; ++i) { | |
putchar(' '); | |
} | |
} |
This file contains hidden or 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
CC?=gcc | |
DEFAULT_CFLAGS=-std=c11 -Og -Wall -Wextra -Wshadow | |
define GCC_FLAGS | |
-Wanalyzer-file-leak \ | |
-Wanalyzer-malloc-leak \ | |
-Wanalyzer-double-free \ | |
-Wanalyzer-double-fclose \ | |
-Wanalyzer-null-argument \ | |
-Wanalyzer-use-after-free \ | |
-Wanalyzer-write-to-const \ | |
-Wanalyzer-free-of-non-heap \ | |
-Wanalyzer-null-dereference \ | |
-Wanalyzer-stale-setjmp-buffer \ | |
-Wanalyzer-tainted-array-index \ | |
-Wanalyzer-shift-count-negative \ | |
-Wanalyzer-shift-count-overflow \ | |
-Wanalyzer-possible-null-argument \ | |
-Wanalyzer-write-to-string-literal \ | |
-Wanalyzer-mismatching-deallocation \ | |
-Wanalyzer-possible-null-dereference \ | |
-Wanalyzer-exposure-through-output-file \ | |
-Wanalyzer-unsafe-call-within-signal-handler \ | |
-Wanalyzer-use-of-pointer-in-stale-stack-frame | |
endef | |
ifeq ($(CC), gcc) | |
DEFAULT_CFLAGS+=$(GCC_FLAGS) | |
endif | |
all: | |
@$(CC) -o main *.c $(DEFAULT_CFLAGS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment