Identity matrix: Difference between revisions

Content added Content deleted
m (C code fix)
Line 645: Line 645:
exit(EXIT_FAILURE);
exit(EXIT_FAILURE);
}
}
signed int rowsize = atoi(argv[1]);
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);
}
}
volatile int numElements = rowsize * rowsize;
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 %d bytes each\n", numElements, sizeof(int*));
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 %d bytes each\n", numElements, sizeof(int));
printf("Failed to allocate %d elements of %ld bytes each\n", numElements, sizeof(int));
abort();
abort();
}
}