Gladson
Gladson

Reputation: 97

How to use functions with arrays correctly

This is a small program that I wrote to add 2 square matrices.

When i input the second matrix the values of the first are altered and the result is subsequently false. It works quite well for 2x2 matrices,But not for any bigger matrix. The algorithm works well without the use of functions. Please help me solve this problem.

Here is the code:

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

int n;
void PrintMatrix(int P[n][n])
{
    int i,j;
    for(i=0;i<n;i++)
    {
        printf("\n");
        for(j=0;j<n;j++)
        {
            printf("%d\t", P[i][j]);
        }
    }printf("\n");
}
void ReadMatrix(int P[n][n])
{
    int i,j;
    for(i=0;i<n;i++)
    {
    printf("input row %d\n", i+1);
        for(j=0;j<n;j++)
        {
        scanf("%d", &P[i][j]);
        }
    }
}
void AddMatrix(int P[n][n], int Q[n][n], int Result[n][n])
{
   int i,j;
    for(i=0;i<n;i++)
    {
    for(j=0;j<n;j++)
      {
        Result[i][j]=P[i][j]+Q[i][j];
      }
    }
}

int main()
{
    int i,j;
    int A[n][n];
    int B[n][n];
    int Sum[n][n];

    printf("input matrix size:");
    scanf("%d", &n);

    printf("input first matrix\n");
    ReadMatrix(A);
    PrintMatrix(A);

    printf("input second matrix\n");
    ReadMatrix(B);
    PrintMatrix(B);

    AddMatrix(A,B,Sum);
    PrintMatrix(Sum);
    return 0;
}

Upvotes: 0

Views: 79

Answers (2)

Nish
Nish

Reputation: 379

if you run this under gdb here is something you'll find (gdb) p A $14 = 0x7fffffffdfb0 (gdb) p B $15 = 0x7fffffffdfa0

irrespective of whatever "n" you choose the array base address always differ by 16 that is integer 2 x 2 matrix works

but if i Input length as 3 the program crashes. There is memory corruption. before doing input (gdb) x/12 A 0x7fffffffdfb0: 0 0 -255260739 52 0x7fffffffdfc0: -1 -1 -1 -1 0x7fffffffdfd0: -8272 32767 -1 -1

after input: x/12 0x7fffffffdfb0 0x7fffffffdfb0: 1 2 3 4 0x7fffffffdfc0: 5 6 7 8 0x7fffffffdfd0: 9 32767 -1 -1 is fine as i gave values 1 2 3 4 5 6 7 8 9

but p A $16 = 0x7fff00000009 is this accessing it causes segmentation fault in PrintMatrix.

now if you change your program to this

int main()
{
    int i,j;

    printf("input matrix size:");
    scanf("%d", &n);
    int A[n][n];
    int B[n][n];
    int Sum[n][n];

your problems are solved and you are good to go

Upvotes: 0

Lundin
Lundin

Reputation: 215360

You can define your own matrix type with a struct, such as for example:

typedef struct
{
  int*    data;
  size_t  width;
  size_t  height
} matrix_t;

Or if you have a modern C compiler, you can write the functions like

void func (size_t width, size_t height, int matrix[width][height])

But the struct version is probably to prefer.


EDIT

As for why your program is buggy, you must initialize n to a value. If you declare the arrays as variable-length arrays of size [n][n] after the point where you read n from the user, it should work fine.

Upvotes: 1

Related Questions