无法交换数组 [2][2] 中的行。似乎它做了一些超出数组的事情。(我怀疑这段代码已经疯了(疯了)
1)我打印它。2) 尝试交换(认为问题就在附近)。3)我再打印一次。(但现在元素的值不像以前那样了)
#include <stdio.h>#
#include <stdlib.h>
#include <time.h>
#define RANGE 99
int main()
{
int l1,l2;
int i,j;
int arr[2][2]; // There should be arr[3][3]. FOR MORE
int temp_line[2]; // DETAILS SEE THE ACCEPTED ANSWER.
srand((unsigned)time(NULL));
/* Filling in */
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
arr[i][j] = 1 + rand()%RANGE;
printf("\n");
}
/* Displaying */
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
printf("%2.d ", arr[i][j]);
printf("\n");
}
printf("\nEnter the No. of lines to swap them.\n"); // Remember about the 0th element.
scanf("%d%d", &l1, &l2); // The 1-st and the 2-nd lines.
/** Swapping lines. PROBLEM! */
for (j = 0; j <= 2; j++)
temp_line[j] = arr[l1][j]; // Remember the 1-st required line.
for (j = 0; j <= 2; j++)
arr[l1][j] = arr[l2][j]; // Copying each element of the 2-nd required line 1-st one.
for (j = 0; j <= 2; j++)
arr[l2][j] = temp_line[j]; // Copying "Remembered" 1-st required line.
/* Displaying */
for (i = 0; i <= 2; i++)
{
for (j = 0; j <= 2; j++)
printf("%2.d ", arr[i][j]);
printf("\n");
}
return 0;
}