Skip to content

Instantly share code, notes, and snippets.

@DieTime
Last active May 30, 2022 19:08
Show Gist options
  • Save DieTime/1bc914053333e6d64b19e16bf6b50d1f to your computer and use it in GitHub Desktop.
Save DieTime/1bc914053333e6d64b19e16bf6b50d1f to your computer and use it in GitHub Desktop.
matrix multiplication
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>
void display(int *matrix, int dimension, const char *name) {
int i, j;
printf("Матрица %s:\n", name);
for (i = 0; i < dimension; i++) {
for (j = 0; j < dimension; j++) {
printf("%d\t", matrix[i*dimension + j]);
}
printf("\n");
}
printf("\n");
}
int main(int argc, char *argv[]) {
setlocale(0, "RUS");
if (argc < 2) {
printf("Не указана размерность матриц\n");
return 1;
}
int N = atoi(argv[1]);
printf("Размерность матриц: %dx%d\n\n", N, N);
int i, j, k;
int* A = (int*)malloc(N * N * sizeof(int));
int* B = (int*)malloc(N * N * sizeof(int));
int* C = (int*)calloc(N * N, sizeof(int));
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
A[i*N + j] = rand() % 100;
B[i*N + j] = rand() % 100;
}
}
display(A, N, "A");
display(B, N, "B");
clock_t time_start = clock();
for (i = 0; i < N; i++) {
for (j = 0; j < N; j++) {
for (k = 0; k < N; k++) {
C[i*N + j] += A[i*N + k] * B[k*N + j];
}
}
}
clock_t time_end = clock() - time_start;
display(C, N, "C");
printf("Затраченное время: %f\n", (double)time_end / CLOCKS_PER_SEC);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment