|
//
// 行列
//
#include <stdio.h>
#include <stdlib.h>
double **malloc_gyouretu(int m, int n);
void malloc_gyouretu2(double ***p_gyouretu, int m, int n);
void input_gyouretu(double **gyouretu, int m, int n);
void output_gyouretu(double **gyouretu, int m, int n);
void free_gyouretu(double **gyouretu, int m);
int main(void)
{
double **gyouretu1, **gyouretu2;
int m1, m2, n1, n2;
printf("行列Aの行数を入力して下さい: \n");
scanf("%d", &m1);
printf("行列Aの列数を入力して下さい: \n");
scanf("%d", &n1);
printf("行列Bの行数を入力して下さい: \n");
scanf("%d", &m2);
printf("行列Bの列数を入力して下さい: \n");
scanf("%d", &n2);
gyouretu1 = malloc_gyouretu(m1, n1);
gyouretu2 = malloc_gyouretu(m2, n2);
// malloc_gyouretu2(&gyouretu1, m2, n2);
// malloc_gyouretu2(&gyouretu2, m2, n2);
input_gyouretu(gyouretu1, m1, n1);
input_gyouretu(gyouretu2, m2, n2);
output_gyouretu(gyouretu1, m1, n1);
output_gyouretu(gyouretu2, m2, n2);
free_gyouretu(gyouretu1, m1);
free_gyouretu(gyouretu2, m2);
return EXIT_SUCCESS;
}
double **malloc_gyouretu(int m, int n)
{
double **gyouretu;
int i;
gyouretu = malloc(sizeof(double *) * m);
if (gyouretu == NULL) {
printf("要素の確保に失敗しました");
exit(EXIT_FAILURE);
}
for (i = 0; i < m; i++) {
gyouretu[i] = malloc(sizeof(double) * n);
if (gyouretu[i] == NULL) {
printf("要素の確保に失敗しました");
exit(EXIT_FAILURE);
}
}
return gyouretu;
}
void malloc_gyouretu2(double ***p_gyouretu, int m, int n)
{
int i;
*p_gyouretu = malloc(sizeof(double *) * m);
if (*p_gyouretu == NULL) {
printf("要素の確保に失敗しました");
exit(EXIT_FAILURE);
}
for (i = 0; i < m; i++) {
(*p_gyouretu)[i] = malloc(sizeof(double) * n);
if ((*p_gyouretu)[i] == NULL) {
printf("要素の確保に失敗しました");
exit(EXIT_FAILURE);
}
}
}
void input_gyouretu(double **gyouretu, int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
gyouretu[i][j] = 0.0; // 仮
// scanf("%lf", &gyouretu[i][j]);
}
void output_gyouretu(double **gyouretu, int m, int n)
{
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf(" %f", gyouretu[i][j]);
putchar('\n');
}
putchar('\n');
}
void free_gyouretu(double **gyouretu, int m)
{
int i;
for (i = 0; i < m; i++)
free(gyouretu[i]);
free(gyouretu);
}
|