Search code examples
arrayscmultidimensional-arrayaddition

how to do addition of two 2d arrays and store and display them in 3rd array using function?


I've just started learning C language.

I have started the topic of arrays and now I'm confused as to how to add two 2D arrays and store and display them in a third 2D array using a function...

PS. I am attaching the code which I mentioned above.

#include <stdio.h>

void displayA1(int a[3][3])
{
    printf("\n The 1st Array is: \n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++) {
            printf(" %d ", a[i][j]);
        }
    }
}

void displayB1(int b[3][3])
{
    printf("\n The 2nt Array is: \n");
    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++) {
            printf(" %d ", b[i][j]);
        }
    }
}

int doAdd(int a[3][3], int b[3][3], int c[3][3])
{
    int sum = 0;

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            sum = sum + a[i][j] + b[i][j];
        }
    }
    return sum;
}

int main()
{
    int a[3][3];
    int b[3][3];
    int c[3][3];

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("Enter the Elements: ");
            scanf("%d", &a[i][j]);
        }
    }

    printf("\n****************************\n");

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("Enter the Elements: ");
            scanf("%d", &b[i][j]);
        }
    }

    displayA1(a);
    displayB1(b);
    int sum = doAdd(a, b, c);

    printf("\n\n The Addition of two Arrays is: %d ", sum);

    return 0;
}

I wrote a program but it turns out to show the sum of individual elements of both the arrays.

Would be glad to receive help.


Solution

  • Here are some hints to improve your program and solve your problem:

    • You should factorize the code in displayA1 and displayB1, which only differ in the message printed.
    • Similarly, you could use a function to read the elements of a matrix and call it for a and b, avoiding error prone code replication.
    • The function doAdd should store the sums of individual matrix elements into the third matrix.
    • The main function should display the third matrix with the results.

    Here is a modified version:

    #include <stdio.h>
    #include <stdlib.h>
    
    void input_matrix(const char *name, int a[3][3])
    {
        printf("\nEnter the elements of matrix %s:\n", name);
    
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                printf("Enter the element %s[%d][%d]: ", name, i, j);
                if (scanf("%d", &a[i][j]) != 1) {
                    printf("invalid input\n");
                    exit(1);
                }
            }
        }
    }
    
    void display_matrix(const char *name, const int a[3][3])
    {
        printf("\nThe matrix %s is:\n", name);
    
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                printf(" %4d", a[i][j]);
            }
            printf("\n");
        }
    }
    
    int doAdd(const int a[3][3], const int b[3][3], int c[3][3])
    {
        int sum = 0;
    
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                c[i][j] = a[i][j] + b[i][j];
                sum = sum + c[i][j]
            }
        }
        return sum;
    }
    
    int main(void)
    {
        int a[3][3];
        int b[3][3];
        int c[3][3];
    
        input_matrix("a", a);
        input_matrix("b", b);
    
        display_matrix("a", a);
        display_matrix("b", b);
    
        int sum = doAdd(a, b, c);
    
        display_matrix("c", c);
        printf("\n\nThe sum of elements is: %d\n", sum);
    
        return 0;
    }