我想将行和列中的行更改为该二维数组的行
我想要一个程序,它接受输入并提供如下输出。
Input: 1 2 3
4 5 6
Output: 1 4
2 5
3 6
Input: 1 2 3
4 5 6
7 8 9
Output: 1 4 7
2 5 8
3 6 9
我在硬编码数组中做了一个样本,如下所示
int main()
{
int i,j;
int grades[2][3] = { {55, 60, 65},
{85, 90, 95}
};
for( j = 0; j < 3; j++)
{
for( i = 0; i < 2;i++)
{
printf("%d\t",grades[i][j]);
}
printf("\n");
}
return 0;
}
自从我用C语言编程以来,很长一段时间,无论如何我们可以做出动态或更好的方式来做同样的事情。现在它的硬编码。
我记得我们必须使用malloc左右,就是这样。
psuedo代码也没关系。
答案 0 :(得分:2)
从Zhehao Mao用户那里拿出并修复它,看起来像这样:
#include <stdio.h>
void transpose(int *src, int *dest, int rows, int cols){
int i,j;
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
dest[j*rows + i] = src[i*cols + j];
}
}
}
int main(void)
{
int oldar[2][3] = {{1,2,3},{4,5,6}};
int newar[3][2];
transpose(&oldar[0][0], &newar[0][0], 2, 3);
int i, j;
for(i = 0; i < 2; i++)
{
for(j = 0; j < 3; j++)
printf("%d ", oldar[i][j]);
printf("\n");
}
for(i = 0; i < 3; i++)
{
for(j = 0; j < 2; j++)
printf("%d ", newar[i][j]);
printf("\n");
}
}
原始帖子无法工作的原因是int **需要指向指针的指针,如:
int **a ---------> int *int1 --> 1
int *int2 --> 2
int *int3 --> 3
当我们说int [n] [m]时,这不是我们得到的。相反,我们将数组组织成这样的
a[0][0]
\
1 2 3 4 5 6
\___/ \___/
"a[0]" / \____ "a[1]"
或类似的东西。图片可能无法解释清楚,但目前我无法做得更好。
答案 1 :(得分:1)
这是一个相当天真的实现。我很确定有更有效的方法,但这是我能想到的全部。
void transpose(int **src, int **dest, int rows, int cols){
int i,j;
for(i=0; i<rows; i++){
for(j=0; j<cols; j++){
dest[j][i] = src[i][j];
}
}
}
int main(void){
int oldar[2][3] = {{1,2,3},{4,5,6}};
int newar[3][2];
transpose(oldar, newar, 2, 3);
}
双指针可以表示双数组,因此不需要在堆上进行分配。
答案 2 :(得分:1)
这是一个半完成的程序,就像我在C中做的那样:
int main()
{
int **data;
int rows = 0,
columns = 0;
char in[256];
int *irow;
// Get user input.
for(rows = 0; 1; ++rows)
{
scanf("%255s", in);
if(strcmp(in, "exit") == 0)
break;
// Parse row here. Remove all the tabs. Set column count.
for(int icolumn = 0; 1; ++icolumn)
{
/* ... */
}
// Set columns if first time.
if(rows == 0)
columns = icolumn;
// Check to make sure user inputs correct amount of columns.
if(columns != icolumns)
{
printf("OMG! The user is a hacker!\n");
break;
}
// Push parsed row into **data.
data[rows] = irow;
}
// Display output.
for(int i = 0; i < columns; ++i)
{
for(int j = 0; j < rows; ++j)
{
printf("%d\t", data[j][i]);
}
printf("\n");
}
return 0;
}
我是一名C ++程序员,因此用户输入部分有点混乱。
答案 3 :(得分:1)
void main()
{
clrscr();
int in[10][10];
int out[10][10];
int row,column,i,j;
printf("enter row");
scanf("%d",&row);
printf("Enter column");
scanf("%d",&column);
//storing values in matrix
for(i=1;i<=row;i++)
{
for(j=1;j<=column;j++)
{
printf("Enter (%d,%d)th value",i,j);
scanf("%d",&in[i-1][j-1]);
}
}
//show stored values
printf("\ninput is\n\n");
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
printf("%d\t",in[i][j]);
}
printf("\n");
}
//show transposed value. it is also stored in out matrix
printf("\nOutput is\n\n");
for(i=0;i<column;i++)
{
for(j=0;j<row;j++)
{
printf("%d\t",in[j][i]);
out[i][j]=in[j][i];
}
printf("\n");
}
getch();
}
//////////////////////////////////////
输入矩阵存储在[] []矩阵中,输出矩阵存储在out [] []矩阵中。 如果我们增加矩阵变量值,这个程序适用于行和列低于10的任何矩阵,它也适用于更大的矩阵。
答案 4 :(得分:0)
嘿这里是一个不使用malloc的简单解决方案,当我在c级的第0级并且不知道“alloc.h”函数时,我这样做了, 您可以使用#rows = #cols = max(#rows,#cols)的正方形数组,如果我们采用您的示例,则矩阵将是3x3矩阵,然后在空白条目中添加任何特殊字符,因此矩阵将看起来像这样
matrix:1 2 3
4 5 6
@ @ @
现在您可以轻松地以您想要的方式转换矩阵... 底线:为了简化矩阵运算,尝试将它们转换为方阵... 使用MALLOC的另一件事是最好的方法,这是为了防止你对所有那些alloc.h函数defs都不方便...
答案 5 :(得分:0)
理论上,你有两个数组
数组x和y
Int grade [x] [y]
你可以交换这两个数组,你得到
int grade [y] [x]
要做到这一点有很多方法,例如通过将数组复制到另外两个1D,或一个2D数组,或简单的指针交换