Bonglord420
Bonglord420

Reputation: 13

Adding Matrices with while loops

I need to add two 3x3 matrices together using while loops. I am able to read and print both matrices using while loops but cannot work out how to add the matrices with while loops.

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

int main()
{

    int i=0,j=0,k=0,l=0,m=0,n=0;
    int a[3][3],b[3][3],c[3][3];

    printf("Enter the first matrix \n \n");
    while(i<3)
    {
        j=0;
        while(j<3)
        {
            scanf("%d",&a[i][j]);
            j++;
        }
        i++;
    }
    printf("\n");
    printf("The first matrix is \n\n");
    i=0;
    while(i<3)
    {
        j=0;
        while(j<3)
        {
            printf("%d ",a[i][j]);
            j++;
        }
        printf("\n");
        i++;
    }
    printf("\n");

    //////////////////////////////////////////////////////////////////////////

 printf("Enter the second matrix \n \n");

    while(k<3)
    {
        l=0;
        while(l<3)
        {
            scanf("%d",&b[k][l]);
            l++;
        }
        k++;
    }
    printf("\n");
    printf("The second matrix is \n\n");
    k=0;
    while(k<3)
    {
        l=0;
        while(l<3)
        {
            printf("%d ",b[k][l]);
            l++;
        }
        printf("\n");
        k++;
    }
    printf("\n");

    ///////////////////////////////////////////////////////////////////////////////

    printf("The sum of the matrix's is \n \n");

    return 0;
}

Upvotes: 0

Views: 2892

Answers (3)

user6428287
user6428287

Reputation:

int matrix_a [3][3] = {{1,2,3},{3,4,5},{4,5,6}};
int matrix_b [3][3] = {{5,6,7},{6,7,8},{7,8,9}};

int i = 0;
while (i < 3) {
   j = 0;
   while (j < 3) {
      matrix_a[i][j] += matrix[i][j];
      ++ j;
   }
   ++ i;
}

for loops are a much better choice:

for (int i = 0; i < 3; ++ i) {
   for (int j = 0; j < 3; ++ j) {
      matrix_a[i][j] += matrix_b[i][j];
   }
}

Superoptimal pointer-arithmetic alternative:

int* ptr_a = matrix_a;
int* ptr_b = matrix_b;

int size = 3 * 3;
while (size --) {
   * ptr_a++ += * ptr_b++;
}

Upvotes: 2

rishap
rishap

Reputation: 1

You can add the below code to do the addition:

             k=0;
                while(k<3)
                {
                    l=0;
                    while(l<3)
                    {
                        c[k][l] = a[k][l]+b[k][l];
                        l++;
                    }
                    k++;
                }

And, to display your matrix, you can use for loop instead of while.

Example:

    for(int p =0;p<3;p++){
        for(int q=0;q<3;q++){
            c[p][q] = a[p][q] + b[p][q] ;
        }
    }

    for(int p =0;p<3;p++){
        for(int q=0;q<3;q++){
            printf("%d \t",c[p][q]) ;
        }
        printf("\n");
    }

Upvotes: 0

Marco Orlando
Marco Orlando

Reputation: 70

Break your problem down into smaller sub problems to help solve it. EXAMPLE:

  1. How to loop through a 2-D array?
  2. How to get values and assign them to a variable from looping through the array?
  3. How to manipulate the values to are obtaining (In your case adding them to something else)
  4. How to insert these values into the correct position in a new array.

Upvotes: 0

Related Questions