几周前我使用数组编写了这个程序,现在我需要使用指针而不是数组。我不完全确定如何去做,所以任何提示将不胜感激!谢谢!:D
这是代码:
#include <stdio.h>
int showArray(int row);
int exchangeRow(int row1, int row2);
int x, y;
int array[10][10];
int j;
int k;
int inputrow;
int inputcolumn;
int scanrow;
int temp;
int row1;
int row2;
int main() {
// Initialize array
for(j = 0; j < 10; j++) {
printf("\n");
for(k = 0; k < 10; k++) {
array[j][k] = j * 10 + k;
printf("%d ", array[j][k]);
}
}
printf("\n \n");
// Print out selected row
printf("Type in a number for the corresponding row to be printed \n");
scanf("%d", &inputrow);
if(inputrow >= 0 && inputrow < 10) {
for(j = 0; j < 10; j++) {
printf("%d ", array[inputrow][j]);
}
}
printf("\n \n");
//Print out selected column
printf("Type in a number for the corresponding column to be printed \n");
scanf("%d", &inputcolumn);
if(inputcolumn >= 0 && inputcolumn < 10) {
for(j = 0; j < 10; j++) {
printf("%d ", array[j][inputcolumn]);
}
}
printf("\n \n");
printf("Type in a number for the row that method showArray will print \n");
scanf("%d", &scanrow);
showArray(scanrow);
printf("\n \n");
printf("Type in two numbers for the rows that method exchangeRow will switch \n");
scanf("%d %d", &row1, &row2);
exchangeRow(row1, row2);
printf("\n \n");
system("PAUSE");
}
int showArray(int row) {
for(j = 0; j < 10; j++) {
printf("%d ", array[row][j]);
}
}
int exchangeRow(int row1, int row2) {
if(row1 >= 0 && row1 < 10 && row2 >= 0 && row2 < 10) {
temp = row1;
row1 = row2;
row2 = temp;
printf("The first row now holds the values: ");
showArray(row1);
printf("\n");
printf("The second row now holds the values: ");
showArray(row2);
}
}