1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include<stdio.h>
#include<stdlib.h>

int main(){
        int i, j, k;
        int SIZE;
        double *matrix, *eye;
        FILE *mfPtr, *efPtr;

        printf("enter the size of matix:\n");
        scanf("%d",&SIZE);

        matrix = (double*) malloc(SIZE * SIZE * sizeof(double));
        if(NULL == matrix){
                free(matrix);
                printf("Memory allocation failed.\n");
                exit(-1);
        }
        if((mfPtr = fopen("A.mat", "w")) == NULL){
                printf("File could not be opened\n");
        }
        else{
                fprintf(mfPtr, "%d\n", SIZE);
                printf("A.mat is saved\n");
                printf("(size of %d by %d counting matrix)\n", SIZE, SIZE);
                for (i=0; i<SIZE*SIZE; i++){
                        matrix[i] = i+1;
                        fprintf(mfPtr, "%4.4lf\n", matrix[i]);
                }
                fclose(mfPtr);
        }
        free(matrix);

        eye = (double*) malloc(SIZE * SIZE * sizeof(double));
        if(NULL == eye){
                free(eye);
                printf("Memory allocation failed.\n");
                exit(-1);
        }
        if((efPtr = fopen("B.mat", "w")) == NULL){
                printf("File could not be opened\n");
        }
        else{
                fprintf(efPtr, "%d\n", SIZE);
                printf("B.mat is saved\n");
                printf("(size of %d by %d identity)\n", SIZE, SIZE);
                for (i=0; i<SIZE; i++){
                        for(j=0; j<SIZE; j++){
                                if(i==j) eye[i*SIZE + j] = 1;
                                else eye[i*SIZE + j] = 0;
                                fprintf(efPtr, "%4.4lf\n", eye[i*SIZE+j]);
                        }
                }
                fclose(efPtr);
        }
        free(eye);
}