读写矩阵 [英] Read and write matrix

查看:124
本文介绍了读写矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这个code取R *ç矩阵的输入

研究 - >行
Ç - >列

它正常工作时为r = C
但无法生成正确的输出当r>℃。

例如:

R = 6,C = 2

输入:
1 2
3 4
5 6
7 8
9 10
11月12日

输出:

1 2
11 12
5 6
11 12
11 12
11月12日

请告诉我,我错了。
我不希望使用固定大小的数组。

 的#include<&stdio.h中GT;无效scanmatrix为(int * A [],INT R,INT C)
{
INT I,J;对于(i = 0; I< R;我++)
{
    为(J = 0; J&c为C; J ++)
    {
        scanf函数(%d个,&安培; A [I] [J]);
    }
}
}无效printmatrix为(int * A [],INT R,INT C)
{
INT I,J;对于(i = 0; I< R;我++)
{
    的printf(\\ n \\ n);
    为(J = 0; J&c为C; J ++)
    {
        的printf(%d个\\ t的,一个[I] [J]);
    }
}
}诠释的main()
{
INT *一个;INT R,C,I,J;scanf函数(%D,&安培; R和C);A =(INT *)malloc的(R * C *的sizeof(INT));scanmatrix(安培; A,R,C);
printmatrix(安培; A,R,C);返回(0);
}


解决方案

使用单一指针分配二维数组时二维意味着分配内存continously.so你不能使用标像[I] [J],对于您需要点正确指针

下面code说明如何ü可以使用单一指针来存储二维数组U可以在code实现同样的事情

 #包括LT&;&stdio.h中GT;
#定义COL 2
#定义ROW 6
诠释的main()
{
        INT * P,I,J;
        P =(INT *)malloc的(ROW * COL *的sizeof(INT));
         对于(i = 0; I<行;我++)
        {
                为(J = 0; J< COL; J ++)
                {
                        scanf函数(%d个\\ t的,&安培; P [我* COL + J]);
                }
                的printf(\\ n);
        }        对于(i = 0; I<行;我++)
        {
                为(J = 0; J< COL; J ++)
                {
                        的printf(%d个\\ t的页[我* COL + J]);
                }
                的printf(\\ n);
        }
返回0;
}

i have written this code to take input of r*c matrix

r -> rows c -> columns

it works fine when r<=c but fails to produce correct output when r>c.

Example:

r=6, c=2

input: 1 2 3 4 5 6 7 8 9 10 11 12

output:

1 2 11 12 5 6 11 12 11 12 11 12

please tell me where i am going wrong. i don't want to use constant size array.

#include <stdio.h>

void scanmatrix(int* a[],int r,int c)
{
int i,j;

for(i=0;i<r;i++)
{
    for(j=0;j<c;j++)
    {
        scanf("%d",&a[i][j]);
    }
}
}

void printmatrix(int* a[],int r,int c)
{
int i,j;

for(i=0;i<r;i++)
{
    printf("\n\n");
    for(j=0;j<c;j++)
    {
        printf("%d\t",a[i][j]);
    }
}
}

int main()
{
int* a;

int r,c,i,j;

scanf("%d%d",&r,&c);

a=(int *)malloc(r*c*sizeof(int));

scanmatrix(&a,r,c);
printmatrix(&a,r,c);

return(0);
}

解决方案

Using single pointer to allocate 2d dimesional array means allocating memory continously.so you cant use subscript like a[i][j], for that you need to point pointer correctly

Below code shows how u can use single pointer to store 2D array the same thing u can implement in your code

#include<stdio.h>
#define COL 2
#define ROW 6
int main()
{
        int *p,i,j;
        p=(int *)malloc(ROW*COL*sizeof(int ));
         for(i=0;i<ROW;i++)
        {
                for(j=0;j<COL;j++)
                {
                        scanf("%d\t",&p[i*COL+j]);
                }
                printf("\n");
        }

        for(i=0;i<ROW;i++)
        {
                for(j=0;j<COL;j++)
                {
                        printf("%d\t",p[i*COL+j]);
                }
                printf("\n");
        }
return 0;
}

这篇关于读写矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆