我真的看不出这段代码有什么问题?
我正在尝试获取:
w 3 l l
d o n 3
g a m 3
o v 3 r
来自:
l 3 3 r
l n m 3
3 0 4 v
w d g 0
这是我的代码:
int main() {
printf("Size of array : ");
int n;
scanf("%d", &n);
printf("Cases : \n");
char **array = (char**)malloc(n * sizeof(char));
for (i = 0; i < n; i++)
array[i] = (char*)malloc(n * sizeof(char));
int j;
// Words input
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
scanf("%c", &array[i][j]);
}
}
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
printf("%c", array[i][j]);
}
}
printf("\n\n");
// Transpose of the array
for (i = 0; i <= n; i++) {
for (j = i + 1; j < n; j++) {
char tmp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = tmp;
}
}
// Display the transposed array
printf("Transposed array \n");
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
printf("%c", array[i][j]);
}
}
printf("\n");
printf("\n");
// Swap the columns
for (i = 0; i <= n; i++) {
for (j = 0; j < n / 2; j++) {
char tmp = array[i][j];
array[i][j] = array[i][n - 1 - j];
array[i][n - 1 - j] = tmp;
}
}
// Display after rotation
printf("After rotation\n");
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
printf("%c", array[i][j]);
}
}
printf("\n");
return 0;
}
是语法问题吗? 它似乎可以很好地处理整数...
int main() {
printf("Size of array : ");
int n;
scanf("%d", &n);
printf("Cases : \n");
char **array = (char**)malloc(n * sizeof(char));
for (i = 0; i < n; i++)
array[i] = (char*)malloc(n * sizeof(char));
int j;
// Words input
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
scanf("%c", &array[i][j]);
}
}
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
printf("%c", array[i][j]);
}
}
printf("\n\n");
// Transpose of the array
for (i = 0; i <= n; i++ ) {
for (j = i + 1; j < n; j++ ) {
char tmp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = tmp;
}
}
// Display the transposed array
printf("Transposed array \n");
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
printf("%c", array[i][j]);
}
}
printf("\n");
printf("\n");
// Swap the columns
for (i = 0; i <= n; i++) {
for (j = 0; j < n / 2; j++) {
char tmp = array[i][j];
array[i][j] = array[i][n - 1 - j];
array[i][n - 1 - j] = tmp;
}
}
// Display after rotation
printf("After rotation\n");
for (i = 0; i <= n; i++) {
for (j = 0; j < n; j++) {
printf("%c", array[i][j]);
}
}
printf("\n");
return 0;
}
最佳答案
正如@Some程序员兄弟所指出的,有两个主要问题:
首先,您没有分配“n x n 字符矩阵”意义上的“二维数组”,但您似乎分配了一个包含 n 个指针的数组,每个指针都指向一个包含 n 个字符的序列。所以你应该首先分配n个指针,即char **array = malloc(n * sizeof(char*))
.
其次,您使用 i <= n
反复超出数组边界自始至终;使用i < n
反而。顺便说一句:你没有声明 i
和j
...
解决这个问题后,您的程序就可以运行了:
int main()
{
printf("Size of array : ");
int n;
scanf("%d", &n);
printf("Cases : \n");
char **array = malloc(n * sizeof(char*));
for (int i = 0; i < n; i++)
array[i] = malloc(n * sizeof(char));
int j;
// Words input
for (int i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
scanf("%c", &array[i][j]);
}
}
for (int i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
printf("%c", array[i][j]);
}
printf("\n");
}
printf("\n\n");
// Transpose of the array
for (int i = 0; i < n; i++ )
{
for (int j = i + 1; j < n; j++ )
{
char tmp = array[i][j];
array[i][j] = array[j][i];
array[j][i] = tmp;
}
}
// Display the transposed array
printf("Transposed array \n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%c", array[i][j]);
}
printf("\n");
}
printf("\n");
printf("\n");
// Swap the columns
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n / 2; j++)
{
char tmp = array[i][j];
array[i][j] = array[i][n - 1 - j];
array[i][n - 1 - j] = tmp;
}
}
// Display after rotation
printf("After rotation\n");
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
printf("%c", array[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
关于c - 旋转 90 度二维字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46257999/