C 언어로 2차원 배열을 간략하게 파악한 분
이 기사에서하고 싶은 것
C 언어로 2차원 배열에 대한 이해와 작성을 하고 싶다.
2차원 배열이란?
↓이런 것
행이 있고,
열이 있고,
세로에도 가로에도 값이 넣어지는 표. 어쩌면.
이것을 C 언어로 만듭니다.
포인터, malloc (말록)이 나오지만,
가볍게 흘리는 기분으로 괜찮습니다.
matrix.c#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(int argc, char *argv[])
{
int n, i, j;
int **matrixA, **matrixB, **matrixC;
double st, en;
if (argc < 2)
{
n = 64;
}
else
{
n = atoi(argv[1]);
}
matrixA = malloc(sizeof(int *) * n);
matrixB = malloc(sizeof(int *) * n);
matrixC = malloc(sizeof(int *) * n);
for (i = 0; i < n; i++)
{
matrixA[i] = malloc(sizeof(int) * n);
matrixB[i] = malloc(sizeof(int) * n);
matrixC[i] = malloc(sizeof(int) * n);
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
matrixA[i][j] = rand() % 10 + 1;
matrixB[i][j] = rand() % 10 + 1;
// printf("matrixB[%d][%d] = %d\n", i, j, matrixA[i][j]);
}
}
st = omp_get_wtime();
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
matrixC[i][j] = matrixA[i][j] * matrixB[j][i];
// printf("matrixC[%d][%d] = %d\n", i, j, matrixC[i][j]);
}
}
en = omp_get_wtime();
printf("Etime=%.6f\n", en - st);
// printf("%d\n", n);
// printf("argc = %d\n", argc);
free(matrixA);
free(matrixB);
free(matrixC);
return 0;
}
free 제대로 할 수 없는 것 같다. . .
있을지 어떨지는 두고,
이런 느낌입니다.
내용
우선 A, B, C를 정의하고
matrix.cint **matrixA, **matrixB, **matrixC;
A, B, C 행을 배열 (malloc로)
matrix.cmatrixA = malloc(sizeof(int *) * n);
matrixB = malloc(sizeof(int *) * n);
matrixC = malloc(sizeof(int *) * n);
각 배열에도 배열을 만들어 완성! ! (malloc에 의해)
matrix.cfor (i = 0; i < n; i++)
{
matrixA[i] = malloc(sizeof(int) * n);
matrixB[i] = malloc(sizeof(int) * n);
matrixC[i] = malloc(sizeof(int) * n);
}
이런 느낌에 이미지하면 좋지 않아?
실제로는 다른 것 같지만.
그렇다면 누군가를 위해 될 수 있다고 생각합니다! !
수고하셨습니다.
Reference
이 문제에 관하여(C 언어로 2차원 배열을 간략하게 파악한 분), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Retsuki/items/9620e3ec9bc1d159c462
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
↓이런 것
행이 있고,
열이 있고,
세로에도 가로에도 값이 넣어지는 표. 어쩌면.
이것을 C 언어로 만듭니다.
포인터, malloc (말록)이 나오지만,
가볍게 흘리는 기분으로 괜찮습니다.
matrix.c#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(int argc, char *argv[])
{
int n, i, j;
int **matrixA, **matrixB, **matrixC;
double st, en;
if (argc < 2)
{
n = 64;
}
else
{
n = atoi(argv[1]);
}
matrixA = malloc(sizeof(int *) * n);
matrixB = malloc(sizeof(int *) * n);
matrixC = malloc(sizeof(int *) * n);
for (i = 0; i < n; i++)
{
matrixA[i] = malloc(sizeof(int) * n);
matrixB[i] = malloc(sizeof(int) * n);
matrixC[i] = malloc(sizeof(int) * n);
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
matrixA[i][j] = rand() % 10 + 1;
matrixB[i][j] = rand() % 10 + 1;
// printf("matrixB[%d][%d] = %d\n", i, j, matrixA[i][j]);
}
}
st = omp_get_wtime();
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
matrixC[i][j] = matrixA[i][j] * matrixB[j][i];
// printf("matrixC[%d][%d] = %d\n", i, j, matrixC[i][j]);
}
}
en = omp_get_wtime();
printf("Etime=%.6f\n", en - st);
// printf("%d\n", n);
// printf("argc = %d\n", argc);
free(matrixA);
free(matrixB);
free(matrixC);
return 0;
}
free 제대로 할 수 없는 것 같다. . .
있을지 어떨지는 두고,
이런 느낌입니다.
내용
우선 A, B, C를 정의하고
matrix.cint **matrixA, **matrixB, **matrixC;
A, B, C 행을 배열 (malloc로)
matrix.cmatrixA = malloc(sizeof(int *) * n);
matrixB = malloc(sizeof(int *) * n);
matrixC = malloc(sizeof(int *) * n);
각 배열에도 배열을 만들어 완성! ! (malloc에 의해)
matrix.cfor (i = 0; i < n; i++)
{
matrixA[i] = malloc(sizeof(int) * n);
matrixB[i] = malloc(sizeof(int) * n);
matrixC[i] = malloc(sizeof(int) * n);
}
이런 느낌에 이미지하면 좋지 않아?
실제로는 다른 것 같지만.
그렇다면 누군가를 위해 될 수 있다고 생각합니다! !
수고하셨습니다.
Reference
이 문제에 관하여(C 언어로 2차원 배열을 간략하게 파악한 분), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Retsuki/items/9620e3ec9bc1d159c462
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
int main(int argc, char *argv[])
{
int n, i, j;
int **matrixA, **matrixB, **matrixC;
double st, en;
if (argc < 2)
{
n = 64;
}
else
{
n = atoi(argv[1]);
}
matrixA = malloc(sizeof(int *) * n);
matrixB = malloc(sizeof(int *) * n);
matrixC = malloc(sizeof(int *) * n);
for (i = 0; i < n; i++)
{
matrixA[i] = malloc(sizeof(int) * n);
matrixB[i] = malloc(sizeof(int) * n);
matrixC[i] = malloc(sizeof(int) * n);
}
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
matrixA[i][j] = rand() % 10 + 1;
matrixB[i][j] = rand() % 10 + 1;
// printf("matrixB[%d][%d] = %d\n", i, j, matrixA[i][j]);
}
}
st = omp_get_wtime();
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
matrixC[i][j] = matrixA[i][j] * matrixB[j][i];
// printf("matrixC[%d][%d] = %d\n", i, j, matrixC[i][j]);
}
}
en = omp_get_wtime();
printf("Etime=%.6f\n", en - st);
// printf("%d\n", n);
// printf("argc = %d\n", argc);
free(matrixA);
free(matrixB);
free(matrixC);
return 0;
}
우선 A, B, C를 정의하고
matrix.c
int **matrixA, **matrixB, **matrixC;
A, B, C 행을 배열 (malloc로)
matrix.c
matrixA = malloc(sizeof(int *) * n);
matrixB = malloc(sizeof(int *) * n);
matrixC = malloc(sizeof(int *) * n);
각 배열에도 배열을 만들어 완성! ! (malloc에 의해)
matrix.c
for (i = 0; i < n; i++)
{
matrixA[i] = malloc(sizeof(int) * n);
matrixB[i] = malloc(sizeof(int) * n);
matrixC[i] = malloc(sizeof(int) * n);
}
이런 느낌에 이미지하면 좋지 않아?
실제로는 다른 것 같지만.
그렇다면 누군가를 위해 될 수 있다고 생각합니다! !
수고하셨습니다.
Reference
이 문제에 관하여(C 언어로 2차원 배열을 간략하게 파악한 분), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Retsuki/items/9620e3ec9bc1d159c462텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)