无法在C中访问Bidimensional数组的位置

时间:2015-08-10 19:09:51

标签: c arrays multidimensional-array element

大家好我一直在尝试解决C中的乘法问题。我在Integer数组数组中得到一个元素有问题。现在,我只能访问数组A的第一行[ ] [],但其他值不好。以下是代码段,问题出在 matrixProduct 方法中:

int fa,ca,fb,cb=0;

int main(){
    int A[20][20], B[20][20]. C[20][20];
    printf("Rows for A: ");
    scanf("%d",&fa);
    printf("\nColumns for A: ");
    scanf("%d",&ca);
    printf("\nRows for B: ");
    scanf("%d", &fb);
    printf("\nColumns for B: ");
    scanf("%d",&cb);
    if (ca!=fb){
        printf("\nOperation not available");
        return 0;
    }else{//Filling matrix A & B
        int i,j;
        for (i=0; i<fa; i++){
            for(j=0;j<ca;j++){
                printf("Element at position A[%d][%d]: ",i,j);
                scanf("%d",&A[i][j]);
            }
        }
        for (i=0; i<fb; i++){
            for(j=0;j<cb;j++){
                printf("Element at position B[%d][%d]: ",i,j);
                scanf("%d",&B[i][j]);
            }
        }
    }
    int k=productoMatriz(A,B);

    return 0;
}

//Parameters two matrixes A,B 
int matrixProduct(int A[fa][ca],int B[fb][cb]){
    int i,j,k=0;
    int C[20][20];
    for (i=0; i<fa; i++){
            for(j=0;j<cb;j++){
                C[i][j]=0;
            }
    }
    for(i=0;i<fa;i++){
        for(j=0;j<cb;j++){
                printf(" A [%d] [%d] = %d\n",i,j,A[i][j]);//Problem is in here works
        //ok for the first row but then it doesnt!
            for(k=0;k<ca;k++){
        //C[i][j]=A[i][k]*B[k][j];
            }
        }
    }
    printf("\n\nC Matrix:\n");
    for(i=0; i<fa; i++)
    for(j=0; j<cb; j++)
    {
    printf("%d  ",C[i][j]);
    if(j==cb-1)
        printf("\n\n");
    }
    return 0;
}

修改

现在我已经将20的值更改为宏并更改了matrixProduct中的参数,但现在我试图在main方法中返回C值,然后在其他方法中打印出来void printMatrix(int C [ MAXSIZE] [MAXSIZE]):

C[MAXSIZE][MAXSIZE]=matrixProduct(A,B);
printMatrix(C);

我再次得到错误的价值......

3 个答案:

答案 0 :(得分:3)

问题在于:

int matrixProduct(int A[fa][ca],int B[fb][cb]){

您的函数原型需要int [fa][ca]int [fb][cb],但您传递的是int [20][20]int [20][20]。这些是不同大小的对象,因此当您尝试访问数组元素时,它会从错误的地址拉出。

布置二维数组,每行跟随下一行。所以如果你有int A[2][2],它看起来像这样:

|  0  |  1  |  2  |  3  |
 [0,0] [0,1] [1,0] [1,1]

同样,如果你有int A[3][3],它看起来像这样:

|  0  |  1  |  2  |  3  |  4  |  5  |  6  |  7  |  8  |
 [0,0] [0,1] [0,2] [1,0] [1,1] [1,2] [2,0] [2,1] [2,2]

在第一种情况下,[1,0]位于第三个字节,而在第二种情况下,它位于第四个字节。

编辑:

解决您的编辑问题,这不起作用:

C[MAXSIZE][MAXSIZE]=matrixProduct(A,B);

因为您无法从函数返回数组。您需要做的是将2D数组C作为参数传递。因为数组作为指向第一个元素的指针传递给函数,所以对数组所做的更改将反映在函数之外。

答案 1 :(得分:2)

问题在于,您通过faca fb数组传递固定大小的20 x 20阵列,用于可变大小的cb。这是不允许的:接受数组的方式应该与声明数组的方式相同:

int matrixProduct(int A[20][20], int B[20][20])

否则,编译器将从内存中的错误位置访问数据,从而导致未定义的行为。

或者,您可以将AB内的main声明移到facafb之后的某个点,读取cb,并以与matrixProduct函数标题中声明它们相同的方式声明它们。

此外,看起来matrixProduct函数缺少原型。你应该收到警告。

答案 2 :(得分:1)

其他答案可能指出你的错误。但据我说,这里有点

  • 对于打印,您不需要返回功能。
  • 因此,应使用void函数。
  • 您应该使用宏来指定数组的大小。因为当你想增加或减少数组的大小时,它会很方便。
  • 第一维的尺寸可能无法写入原型和参数中。但第二必须。
  • 只要有可能和有用的情况,尽量使功能。
  • 您希望return0一起使用,但它可以用于错误处理 您应该选择其他数字,例如-1
  • 尽可能避免全局变量! Why?
  • 我还建议在定义函数之前声明函数原型,因为如果你写错了类型的变量或者miss,编译器就会发出警告。
  • 您可以使用代码但查看您的错误

我已尝试改进您的代码

#include <stdio.h>

#define SIZE 20

void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb);
void display(int mult[][SIZE], int fa, int cb);

int main()
{
    int a[SIZE][SIZE], b[SIZE][SIZE], mult[SIZE][SIZE], fa, ca, fb, cb;
    printf("Enter rows and column for first matrix: ");
    scanf("%d%d", &fa, &ca);
    printf("Enter rows and column for second matrix: ");
    scanf("%d%d",&fb, &cb);

    /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */

    while (ca!=fb)
    {
        printf("Error! column of first matrix not equal to row of second.\n");
        printf("Enter rows and column for first matrix: ");
        scanf("%d%d", &fa, &ca);
        printf("Enter rows and column for second matrix: ");
        scanf("%d%d",&fb, &cb);
    }
    take_data(a,b,fa,ca,fb,cb);  /* Function to take matices data */
    multiplication(a,b,mult,fa,ca,fb,cb); /* Function to multiply two matrices. */
    display(mult,fa,cb); /* Function to display resultant matrix after multiplication. */
    return 0;
}

void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
    int i,j;
    printf("\nEnter elements of matrix 1:\n");
    for(i=0; i<fa; ++i)
        for(j=0; j<ca; ++j)
        {
            printf("Enter elements a%d%d: ",i+1,j+1);
            scanf("%d",&a[i][j]);
        }

    printf("\nEnter elements of matrix 2:\n");
    for(i=0; i<fb; ++i)
        for(j=0; j<cb; ++j)
        {
            printf("Enter elements b%d%d: ",i+1,j+1);
            scanf("%d",&b[i][j]);
        }
}

void multiplication(int a[][SIZE],int b[][SIZE],int mult[][SIZE],int fa,int ca,int fb,int cb)
{
    int i,j,k;
    /* Initializing elements of matrix mult to 0.*/
    for(i=0; i<fa; ++i)
        for(j=0; j<cb; ++j)
        {
            mult[i][j]=0;
        }
    /* Multiplying matrix a and b and storing in array mult. */
    for(i=0; i<fa; ++i)
        for(j=0; j<cb; ++j)
            for(k=0; k<ca; ++k)
            {
                mult[i][j]+=a[i][k]*b[k][j];
            }
}

void display(int mult[][SIZE], int fa, int cb)
{
    int i, j;
    printf("\nOutput Matrix:\n");
    for(i=0; i<fa; ++i)
        for(j=0; j<cb; ++j)
        {
            printf("%d  ",mult[i][j]);
            if(j==cb-1)
                printf("\n\n");
        }
}

我们需要使用malloc()来返回数组并且不要忘记它free。使用返回数组

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#define SIZE 20

void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb);
int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb);
void display(int fa, int cb, int** mult);
void free_mult(int **mult, int Rows);


int main()
{
    int a[SIZE][SIZE], b[SIZE][SIZE], fa, ca, fb, cb;
    int** mult;
    printf("Enter rows and column for first matrix: ");
    scanf("%d%d", &fa, &ca);
    printf("Enter rows and column for second matrix: ");
    scanf("%d%d",&fb, &cb);

    /* If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again. */

    while (ca!=fb)
    {
        printf("Error! column of first matrix not equal to row of second.\n");
        printf("Enter rows and column for first matrix: ");
        scanf("%d%d", &fa, &ca);
        printf("Enter rows and column for second matrix: ");
        scanf("%d%d",&fb, &cb);
    }
    take_data(a,b,fa,ca,fb,cb);  /* Function to take matices data */
    mult = multiplication(a,b,fa,ca,fb,cb); /* Function to multiply two matrices. */
    display(fa,cb,mult); /* Function to display resultant matrix after multiplication. */

    free_mult(mult, fa);

    return 0;
}

void take_data(int a[][SIZE], int b[][SIZE], int fa,int ca, int fb, int cb)
{
    int i,j;
    printf("\nEnter elements of matrix 1:\n");
    for(i=0; i<fa; ++i)
        for(j=0; j<ca; ++j)
        {
            printf("Enter elements a%d%d: ",i+1,j+1);
            scanf("%d",&a[i][j]);
        }

    printf("\nEnter elements of matrix 2:\n");
    for(i=0; i<fb; ++i)
        for(j=0; j<cb; ++j)
        {
            printf("Enter elements b%d%d: ",i+1,j+1);
            scanf("%d",&b[i][j]);
        }
}

int** multiplication(int a[][SIZE],int b[][SIZE],int fa,int ca,int fb,int cb)
{
    int i,j,k;


    int **mult = (int **)malloc(fa * sizeof(int *));
    int row;
    // for each row allocate Cols ints
    for (row = 0; row < fa; row++) {
        mult[row] = (int *)malloc(fa * sizeof(int));
    }


    /* Initializing elements of matrix mult to 0.*/
    for(i=0; i<fa; ++i)
        for(j=0; j<cb; ++j)
        {
            mult[i][j]=0;
        }
    /* Multiplying matrix a and b and storing in array mult. */
    for(i=0; i<fa; ++i)
        for(j=0; j<cb; ++j)
            for(k=0; k<ca; ++k)
            {
                mult[i][j]+=a[i][k]*b[k][j];
            }
    return mult;
}

void display(int fa, int cb, int** mult)
{
    int i, j;
    printf("\nOutput Matrix:\n");
    for(i=0; i<fa; ++i)
        for(j=0; j<cb; ++j)
        {
            printf("%d  ",mult[i][j]);
            if(j==cb-1)
                printf("\n\n");
        }
}

void free_mult(int **mult, int Rows)
{
    int row;

    // first free each row
    for (row = 0; row < Rows; row++) {
        free(mult[row]);
    }

    // Eventually free the memory of the pointers to the rows
    free(mult);
}