# Lösungsvorschlag

#include <stdio.h>

#define MAXDIM_M    1000
#define MAXDIM_N    1000

double buffer[MAXDIM_M*MAXDIM_N];

void
initMatrix(long m, long n, double *A, long incRowA, long incColA)
{
long i, j;

for (j=0; j<n; ++j) {
for (i=0; i<m; ++i) {
A[i*incRowA+j*incColA] = j*n+i+1;
}
}
}

void
printMatrix(long m, long n, const double *A, long incRowA, long incColA)
{
long i, j;

for (i=0; i<m; ++i) {
printf("   ");
for (j=0; j<n; ++j) {
printf("%4.1lf ", A[i*incRowA+j*incColA]);
}
printf("\n");
}
printf("\n");
}

int
main()
{
long m = 7;
long n = 8;

//
// Matrix mit Spaltenformat
//
long incRowA = 1;
long incColA = 7;

printf("\nInit A as ColMajor");
printf("\n==================\n");
initMatrix(m, n, buffer, incRowA, incColA);

printf("A = \n");
printMatrix(m, n, buffer, incRowA, incColA);

printf("A^T = \n");
printMatrix(n, m, buffer, incColA, incRowA);

printf("A(2,:) = \n");
printMatrix(1, n, &buffer[1*incRowA], incColA, incRowA);

printf("buffer for A = \n");
printMatrix(1, m*n, buffer, 1, 1);

//
// Matrix im Zeilenformat
//

incRowA = 8;
incColA = 1;

printf("\nInit A as RowMajor");
printf("\n==================\n");
initMatrix(m, n, buffer, incRowA, incColA);

printf("A = \n");
printMatrix(m, n, buffer, incRowA, incColA);

printf("A^T = \n");
printMatrix(n, m, buffer, incColA, incRowA);

printf("A(2,:) = \n");
printMatrix(1, n, &buffer[1*incRowA], incColA, incRowA);

printf("buffer for A = \n");
printMatrix(1, m*n, buffer, 1, 1);

return 0;
}


## Übersetzen und Ausführen

$shell> gcc -Wall -o example1_matrix example1_matrix.c$shell> ./example1_matrix

Init A as ColMajor
==================
A =
1.0  9.0 17.0 25.0 33.0 41.0 49.0 57.0
2.0 10.0 18.0 26.0 34.0 42.0 50.0 58.0
3.0 11.0 19.0 27.0 35.0 43.0 51.0 59.0
4.0 12.0 20.0 28.0 36.0 44.0 52.0 60.0
5.0 13.0 21.0 29.0 37.0 45.0 53.0 61.0
6.0 14.0 22.0 30.0 38.0 46.0 54.0 62.0
7.0 15.0 23.0 31.0 39.0 47.0 55.0 63.0

A^T =
1.0  2.0  3.0  4.0  5.0  6.0  7.0
9.0 10.0 11.0 12.0 13.0 14.0 15.0
17.0 18.0 19.0 20.0 21.0 22.0 23.0
25.0 26.0 27.0 28.0 29.0 30.0 31.0
33.0 34.0 35.0 36.0 37.0 38.0 39.0
41.0 42.0 43.0 44.0 45.0 46.0 47.0
49.0 50.0 51.0 52.0 53.0 54.0 55.0
57.0 58.0 59.0 60.0 61.0 62.0 63.0

A(2,:) =
2.0  3.0  4.0  5.0  6.0  7.0  9.0 10.0

buffer for A =
1.0  2.0  3.0  4.0  5.0  6.0  7.0  9.0 10.0 11.0 12.0 13.0 14.0 15.0 17.0 18.0 19.0 20.0 21.0 22.0 23.0 25.0 26.0 27.0 28.0 29.0 30.0 31.0 33.0 34.0 35.0 36.0 37.0 38.0 39.0 41.0 42.0 43.0 44.0 45.0 46.0 47.0 49.0 50.0 51.0 52.0 53.0 54.0 55.0 57.0 58.0 59.0 60.0 61.0 62.0 63.0

Init A as RowMajor
==================
A =
1.0  9.0 17.0 25.0 33.0 41.0 49.0 57.0
2.0 10.0 18.0 26.0 34.0 42.0 50.0 58.0
3.0 11.0 19.0 27.0 35.0 43.0 51.0 59.0
4.0 12.0 20.0 28.0 36.0 44.0 52.0 60.0
5.0 13.0 21.0 29.0 37.0 45.0 53.0 61.0
6.0 14.0 22.0 30.0 38.0 46.0 54.0 62.0
7.0 15.0 23.0 31.0 39.0 47.0 55.0 63.0

A^T =
1.0  2.0  3.0  4.0  5.0  6.0  7.0
9.0 10.0 11.0 12.0 13.0 14.0 15.0
17.0 18.0 19.0 20.0 21.0 22.0 23.0
25.0 26.0 27.0 28.0 29.0 30.0 31.0
33.0 34.0 35.0 36.0 37.0 38.0 39.0
41.0 42.0 43.0 44.0 45.0 46.0 47.0
49.0 50.0 51.0 52.0 53.0 54.0 55.0
57.0 58.0 59.0 60.0 61.0 62.0 63.0

A(2,:) =
2.0  3.0  4.0  5.0  6.0  7.0  0.0  0.0

buffer for A =
1.0  9.0 17.0 25.0 33.0 41.0 49.0 57.0  2.0 10.0 18.0 26.0 34.0 42.0 50.0 58.0  3.0 11.0 19.0 27.0 35.0 43.0 51.0 59.0  4.0 12.0 20.0 28.0 36.0 44.0 52.0 60.0  5.0 13.0 21.0 29.0 37.0 45.0 53.0 61.0  6.0 14.0 22.0 30.0 38.0 46.0 54.0 62.0  7.0 15.0 23.0 31.0 39.0 47.0 55.0 63.0