Lösungsvorschlag
#include <stdio.h> void initGeMatrix(int m, int n, double *A, int incRowA, int incColA) { int i, j; for (i=0; i<m; ++i) { for (j=0; j<n; ++j) { A[i*incRowA+j*incColA] = i*n + j + 1; } } } void printGeMatrix(int m, int n, double *A, int incRowA, int incColA) { int i, j; for (i=0; i<m; ++i) { for (j=0; j<n; ++j) { printf("%10.4lf ", A[i*incRowA+j*incColA]); } printf("\n"); } printf("\n\n"); } #ifndef DIM_M #define DIM_M 6 #endif #ifndef DIM_N #define DIM_N 8 #endif #ifndef ROWMAJOR #define ROWMAJOR 0 #endif #if (ROWMAJOR==1) # define INCROW_A DIM_N # define INCCOL_A 1 #else # define INCROW_A 1 # define INCCOL_A DIM_M #endif double A[DIM_M*DIM_N]; int main() { initGeMatrix(DIM_M, DIM_N, A, INCROW_A, INCCOL_A); printf("A =\n"); printGeMatrix(DIM_M, DIM_N, A, INCROW_A, INCCOL_A); printf("A^T =\n"); printGeMatrix(DIM_N, DIM_M, A, INCCOL_A, INCROW_A); int mn = (DIM_M>DIM_N) ? DIM_N : DIM_M; printf("diag(A) =\n"); printGeMatrix(mn, 1, A, INCROW_A+INCCOL_A, INCCOL_A); printf("A(2,:) =\n"); printGeMatrix(1, DIM_N, &A[1*INCROW_A], INCROW_A, INCCOL_A); printf("A(:,3) =\n"); printGeMatrix(DIM_M, 1, &A[2*INCCOL_A], INCROW_A, INCCOL_A); printf("A(4:5, 2:4) =\n"); printGeMatrix(2, 3, &A[3*INCROW_A+1*INCCOL_A], INCROW_A, INCCOL_A); printf("A(4:2:8, 2:3:14) =\n"); printGeMatrix(3, 5, &A[3*INCROW_A+1*INCCOL_A], 2*INCROW_A, 3*INCCOL_A); return 0; }
$shell> gcc -Wall -DDIM_M=10 -DDIM_N=16 -DROWMAJOR=0 -o initmatrix initmatrix.c $shell> ./initmatrix A = 1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000 8.0000 9.0000 10.0000 11.0000 12.0000 13.0000 14.0000 15.0000 16.0000 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000 23.0000 24.0000 25.0000 26.0000 27.0000 28.0000 29.0000 30.0000 31.0000 32.0000 33.0000 34.0000 35.0000 36.0000 37.0000 38.0000 39.0000 40.0000 41.0000 42.0000 43.0000 44.0000 45.0000 46.0000 47.0000 48.0000 49.0000 50.0000 51.0000 52.0000 53.0000 54.0000 55.0000 56.0000 57.0000 58.0000 59.0000 60.0000 61.0000 62.0000 63.0000 64.0000 65.0000 66.0000 67.0000 68.0000 69.0000 70.0000 71.0000 72.0000 73.0000 74.0000 75.0000 76.0000 77.0000 78.0000 79.0000 80.0000 81.0000 82.0000 83.0000 84.0000 85.0000 86.0000 87.0000 88.0000 89.0000 90.0000 91.0000 92.0000 93.0000 94.0000 95.0000 96.0000 97.0000 98.0000 99.0000 100.0000 101.0000 102.0000 103.0000 104.0000 105.0000 106.0000 107.0000 108.0000 109.0000 110.0000 111.0000 112.0000 113.0000 114.0000 115.0000 116.0000 117.0000 118.0000 119.0000 120.0000 121.0000 122.0000 123.0000 124.0000 125.0000 126.0000 127.0000 128.0000 129.0000 130.0000 131.0000 132.0000 133.0000 134.0000 135.0000 136.0000 137.0000 138.0000 139.0000 140.0000 141.0000 142.0000 143.0000 144.0000 145.0000 146.0000 147.0000 148.0000 149.0000 150.0000 151.0000 152.0000 153.0000 154.0000 155.0000 156.0000 157.0000 158.0000 159.0000 160.0000 A^T = 1.0000 17.0000 33.0000 49.0000 65.0000 81.0000 97.0000 113.0000 129.0000 145.0000 2.0000 18.0000 34.0000 50.0000 66.0000 82.0000 98.0000 114.0000 130.0000 146.0000 3.0000 19.0000 35.0000 51.0000 67.0000 83.0000 99.0000 115.0000 131.0000 147.0000 4.0000 20.0000 36.0000 52.0000 68.0000 84.0000 100.0000 116.0000 132.0000 148.0000 5.0000 21.0000 37.0000 53.0000 69.0000 85.0000 101.0000 117.0000 133.0000 149.0000 6.0000 22.0000 38.0000 54.0000 70.0000 86.0000 102.0000 118.0000 134.0000 150.0000 7.0000 23.0000 39.0000 55.0000 71.0000 87.0000 103.0000 119.0000 135.0000 151.0000 8.0000 24.0000 40.0000 56.0000 72.0000 88.0000 104.0000 120.0000 136.0000 152.0000 9.0000 25.0000 41.0000 57.0000 73.0000 89.0000 105.0000 121.0000 137.0000 153.0000 10.0000 26.0000 42.0000 58.0000 74.0000 90.0000 106.0000 122.0000 138.0000 154.0000 11.0000 27.0000 43.0000 59.0000 75.0000 91.0000 107.0000 123.0000 139.0000 155.0000 12.0000 28.0000 44.0000 60.0000 76.0000 92.0000 108.0000 124.0000 140.0000 156.0000 13.0000 29.0000 45.0000 61.0000 77.0000 93.0000 109.0000 125.0000 141.0000 157.0000 14.0000 30.0000 46.0000 62.0000 78.0000 94.0000 110.0000 126.0000 142.0000 158.0000 15.0000 31.0000 47.0000 63.0000 79.0000 95.0000 111.0000 127.0000 143.0000 159.0000 16.0000 32.0000 48.0000 64.0000 80.0000 96.0000 112.0000 128.0000 144.0000 160.0000 diag(A) = 1.0000 18.0000 35.0000 52.0000 69.0000 86.0000 103.0000 120.0000 137.0000 154.0000 A(2,:) = 17.0000 18.0000 19.0000 20.0000 21.0000 22.0000 23.0000 24.0000 25.0000 26.0000 27.0000 28.0000 29.0000 30.0000 31.0000 32.0000 A(:,3) = 3.0000 19.0000 35.0000 51.0000 67.0000 83.0000 99.0000 115.0000 131.0000 147.0000 A(4:5, 2:4) = 50.0000 51.0000 52.0000 66.0000 67.0000 68.0000 A(4:2:8, 2:3:14) = 50.0000 53.0000 56.0000 59.0000 62.0000 82.0000 85.0000 88.0000 91.0000 94.0000 114.0000 117.0000 120.0000 123.0000 126.0000 $shell>