multiplying matricies

This is a discussion on multiplying matricies within the C Programming forums, part of the General Programming Boards category; so I think everything is good in my code, except when i print I get a few funny numbers. I ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    46

    multiplying matricies

    so I think everything is good in my code, except when i print I get a few funny numbers. I think because the numbers are too big to be integers. How do I effectively convert them to show up properly in the code?

    Code:
    #include <stdio.h>
    #include "max_sizes_b.h"
    
    int main (void) {
    	
    int A[MAX_L][MAX_N], B[MAX_M][MAX_N], C[MAX_L][MAX_N], i, j, l, m, n;
    
    scanf("%d%d%d", &l, &m, &n);
    
    for (i = 0; i < l; i++)
      for (j = 0; j < m; j++)
        scanf("%d ", &A[i][j]);
    
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        scanf("%d ", &B[i][j]);
        
    sumofarrays(A, B, C, l, m, n);
    
    for (i = 0; i < n; ++i) {
      for (j = 0; j < n; j++)
        printf("%4d ", C[i][j]);
        printf("\n");
    } 
    
    return 0;
    
    }
    Code:
    #include <stdio.h>
    #include "max_sizes_b.h"
    
    void sumofarrays(int A[][MAX_M], int B[][MAX_N], int C[][MAX_N], int l, int m, int n) {
    	
    int i, j, k;
    
    for (i = 0; i < l; ++i)
      for (j = 0; j < m; ++j)
        {
    		C[i][j] = 0.0;
    		for (k = 0; k < m; ++k)
    		C[i][j] = C[i][j] + A[i][k] * B[k][j];
    	}
    }

  2. #2
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    no, wait, I think something is wrong with my code......let me work on it some more

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    46
    ok i think i got it. this is right? right?

    Code:
    #include <stdio.h>
    #include "max_sizes_b.h"
    
    int main (void) {
    	
    int A[MAX_L][MAX_N], B[MAX_M][MAX_N], C[MAX_L][MAX_N], i, j, l, m, n;
    
    scanf("%d%d%d", &l, &m, &n);
    
    for (i = 0; i < l; i++)
      for (j = 0; j < m; j++)
        scanf("%d ", &A[i][j]);
    
    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        scanf("%d ", &B[i][j]);
        
    sumofarrays(A, B, C, l, m, n);
    
    for (i = 0; i < l; ++i) {
      for (j = 0; j < n; j++)
        printf("%4d ", C[i][j]);
        printf("\n");
    } 
    
    return 0;
    
    }
    Code:
    #include <stdio.h>
    #include "max_sizes_b.h"
    
    void sumofarrays(int A[][MAX_M], int B[][MAX_N], int C[][MAX_N], int l, int m, int n) {
    	
    int i, j, k;
    
    for (i = 0; i < l; ++i)
      for (j = 0; j < n; ++j)
        {
    		C[i][j] = 0.0;
    		for (k = 0; k < m; ++k)
    		C[i][j] = C[i][j] + A[i][k] * B[k][j];
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying chars
    By orjanb314 in forum C Programming
    Replies: 8
    Last Post: 11-20-2008, 08:58 AM
  2. Multiplying Two Polynomials
    By CaptainMorgan in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 01:34 PM
  3. Defining matricies
    By rich999 in forum C++ Programming
    Replies: 6
    Last Post: 12-02-2005, 06:59 PM
  4. More Matricies
    By Lurker in forum Tech Board
    Replies: 1
    Last Post: 12-25-2003, 02:14 AM
  5. Multiplying! PLEASE HELP!!!!!!!!!!!!!
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 10-23-2001, 09:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21