#include <stdio.h>
#define ROW 20
#define COL 20
struct matrix
{
double coef[ROW][COL];
int nr;
int nc;
};
typedef struct matrix MTRX;
int swap(MTRX *A, int swap1, int swap2);
void main ()
{
MTRX A = {{{1., 2., 3.},{4., 5., 6.},{7., 8., 9.}},3,3};
int i, j, swappedrows;
printf("Enter the rows that you would like to swap.\n");
scanf("%i %i", &i, &j);
swappedrows = swap(&A, i, j);
if (swappedrows)
{
for (i = 0; i < A.nr; i++)
{
printf("\n");
for (j = 0; j < A.nc; j++)
{
printf("%2.0f", A.coef[i][j]);
}
}
}
return;
}
int swap(MTRX *A, int swap1, int swap2)
{
int i;
double temp;
if (swap1 >= A->nr || swap2 >= A->nr)
{
printf("The rows cannot be swapped. One or more of the rows seleted does not exist.\n");
return 0;
}
for (i = 0; i < A->nc; i++)
{
temp = A->coef[swap1][i];
A->coef[swap1][i] = A->coef[swap2][i];
A->coef[swap2][i] = temp;
}
return 1;
}