1.定义一个整型二维数组,要求通过键盘输入的方式为数组元素赋值,并对数组的每一行进 行排序。

2025-01-02 15:00:20
推荐回答(2个)
回答1:

#include

const int RowNum = 3;
const int ColumnNum = 3;
void BubbleSort(int Array[RowNum][ColumnNum]);
int main()
{
int i = 0,j = 0;
int Array[RowNum][ColumnNum];
for (i=0;i {
for (j=0;j {
scanf("%d",&Array[i][j]);
}
}
printf("输入的数组如下:\n");
for (i=0;i {
for (j=0;j {
printf("%d ",Array[i][j]);
}
printf("\n");
}
BubbleSort(Array);
printf("行排序后的数组如下:\n");
for (i=0;i {
for (j=0;j {
printf("%d ",Array[i][j]);
}
printf("\n");
}

return 0;
}

void BubbleSort(int Array[RowNum][ColumnNum])
{
int Temp = 0;
for (int i=0;i {
for (int j=0;j {
for(int k=j+1;k {
if (Array[i][j] > Array[i][k])
{
Temp = Array[i][j];
Array[i][j] = Array[i][k];
Array[i][k] = Temp;
}
}
}
}

}

回答2:

#include
const int row=10;
const int col=10;
void main()
{
int i=0,j=0,k=0,temp=0;
int a[row][col];
for(i=0;ifor(j=0;jscanf("%d",&a[i][j]);

for(i=0;i{
for(j=0;j {
for(k=j+1;k {
if(a[i][j]>a[i][k])
{
temp=a[i][j];
a[i][j]=a[i][k];

a[i][k]=temp;

}

}
}
}
}