Identity matrix: Difference between revisions
Content added Content deleted
m (C code fix) |
|||
Line 645: | Line 645: | ||
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
||
} |
} |
||
int rowsize = atoi(argv[1]); |
|||
if (rowsize < 0) { |
if (rowsize < 0) { |
||
printf("Dimensions of matrix cannot be negative\n"); |
printf("Dimensions of matrix cannot be negative\n"); |
||
exit(EXIT_FAILURE); |
exit(EXIT_FAILURE); |
||
} |
} |
||
int numElements = rowsize * rowsize; |
|||
if (numElements < rowsize) { |
if (numElements < rowsize) { |
||
printf("Squaring %d caused result to overflow to %d.\n", rowsize, numElements); |
printf("Squaring %d caused result to overflow to %d.\n", rowsize, numElements); |
||
Line 657: | Line 657: | ||
int** matrix = calloc(numElements, sizeof(int*)); |
int** matrix = calloc(numElements, sizeof(int*)); |
||
if (!matrix) { |
if (!matrix) { |
||
printf("Failed to allocate %d elements of % |
printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int*)); |
||
abort(); |
abort(); |
||
} |
} |
||
Line 663: | Line 663: | ||
matrix[row] = calloc(numElements, sizeof(int)); |
matrix[row] = calloc(numElements, sizeof(int)); |
||
if (!matrix[row]) { |
if (!matrix[row]) { |
||
printf("Failed to allocate %d elements of % |
printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int)); |
||
abort(); |
abort(); |
||
} |
} |