Skip to content

Instantly share code, notes, and snippets.

@haldun
Created March 16, 2014 18:23
Show Gist options
  • Save haldun/9587640 to your computer and use it in GitHub Desktop.
Save haldun/9587640 to your computer and use it in GitHub Desktop.
#include <stdio.h>
void Identity(int *matrix, size_t size)
{
for (size_t i = 0; i < size; ++i) {
for (size_t j = 0; j < size; ++j) {
matrix[i * size + j] = (i == j) ? 1 : 0;
}
}
}
void PrintMatrix(int *matrix, size_t size)
{
for (size_t i = 0; i < size; ++i) {
for (size_t j = 0; j < size; ++j) {
printf("%d", matrix[i * size + j]);
}
printf("\n");
}
}
int main(int argc, char const *argv[])
{
size_t size = 0;
printf("Size of matrix: ");
scanf("%zd", &size);
int matrix[size][size];
Identity(&matrix[0][0], size);
PrintMatrix(&matrix[0][0], size);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment