Search code examples
cfor-loopmatrixmultidimensional-arraynested-loops

I wrote a program to add two matrices but it's not working. Any suggestions for what's wrong?


This program takes two arrays from the user and adds them and prints the sum. The program uses nested for loops to take the values and print the values. The compiler is returning the error array1 not declared in this scope. The program also stops working if I remove the sum printing part. Any suggestions to shorten the program appreciated.

#include<stdio.h>

int a,b;

int i,j;

main()
{

    printf("Enter the size of the array  \n Rows : ");
    scanf("%d",&a);
    printf("Columns : ");
    scanf("%d",&b);

    int array[a][b];

    printf("Enter the values of the %dx%d array : \n",a,b);

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

    printf("The values of the First Matrix are :\n");


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

        printf("\n");
    }

    int input;
    printf("If you want to do further operations on Matrices press 1\n");
    scanf("%d",&input);

    if(input==1)
    {
        printf("Enter the size of the array  \n Rows : ");
    scanf("%d",&a);
    printf("Columns : ");
    scanf("%d",&b);

    int array1[a][b];

    printf("Enter the values of the %dx%d array : \n",a,b);

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

    printf("The values of the Second Matrix are :\n");


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

        printf("\n");
    }

    }


    input = 0;
    printf("If you want to add the two matrices press 1 \n");
    scanf("%d",input);

    int array2[a][b];

    if(input==1)
    {
        for(i=0;i<a;i++)
    {
        for(j=0;j<b;j++)
        {
            array2[i][j] =  array[i][j]+array1[i][j];
        }
    }

    }

    printf("The Sum of the first and Second array is : \n ");


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

        printf("\n");
    }


}

Solution

  • You have lot of problem in the code, advise you to use curly braces {..} properly. Also use int main(void) { } instead of just main() { }.

    The compiler is returning the error array1 not declared in this scope ? Because array1 is declared inside if(input==1) block and you are accessing outside that scope.

    Also statement scanf("%d",input); is wrong, it gives the warning, compile your program with -Wall flag.

    And finally avoid using global variable for this small tasks or use #define to define row and column value.

    Here is modified code

    int main() {
            printf("Enter the size of the array  \n Rows : ");
            int a = 0,b = 0;
            scanf("%d",&a);
            printf("Columns : ");
            scanf("%d",&b);
            int array[a][b];
            printf("Enter the values of the %dx%d array : \n",a,b);
    
            for(int i=0;i<a;i++) {
                    for(int j=0;j<b;j++) {
                            scanf("%d",&array[i][j]);
                    }
            }
            printf("The values of the First Matrix are :\n");
            for(int i=0;i<a;i++) {
                    for(int j=0;j<b;j++) {
                            printf("%d\t",array[i][j]);
                    }
                    printf("\n");
            }
            int input;
            printf("If you want to do further operations on Matrices press 1\n");
            scanf("%d",&input);
            if(input==1) {
                    printf("Enter the size of the array  \n Rows : ");
                    scanf("%d",&a);
                    printf("Columns : ");
                    scanf("%d",&b);
                    int array1[a][b];
                    printf("Enter the values of the %dx%d array : \n",a,b);
                    for(int i=0;i<a;i++) {
                            for(int j=0;j<b;j++) {
                                    scanf("%d",&array1[i][j]);
                            }
                    }
                    printf("The values of the Second Matrix are :\n");
                    for(int i=0;i<a;i++) {
                            for(int j=0;j<b;j++) {
                                    printf("%d\t",array1[i][j]);
                            }
                            printf("\n");
                    }
    
                    input = 0;
                    printf("If you want to add the two matrices press 1 \n");
                    scanf("%d",&input);/* use &input */
                    int array2[a][b];
                    if(input==1) {
                            for(int i=0;i<a;i++) {
                                    for(int j=0;j<b;j++) {
                                            array2[i][j] =  array[i][j]+array1[i][j];
                                    }
                            }
                    }
                    printf("The Sum of the first and Second array is : \n ");
                    for(int i=0;i<a;i++) {
                            for(int j=0;j<b;j++) {
                                    printf("%d\t",array2[i][j]);
                            }
                            printf("\n");
                    }
            }
            return 0;
    }