Thread: Fractions are just = to 0

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    1

    Fractions are just = to 0

    I have made a program to multiply matrixs together. However when I try to put a fraction as one of the values of a matrix it equals to zero. Here is my program any help will be much appreciated.

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    
    {
    
    #define k 3
    #define m 5
    #define n 4
    int i,j,l;
    
    float A[k][m] = { {2,-1/3,0,2/3,4},
    				  {1/2,3/2,4,-2,1},
    				  {0,3,-9/7,6/7,4/3},
    				 };
    
    float B[m][n] = { {6/5,0,-2,1/3},
    				  {5,7/2,3/4,-3/2},
    				  {0,-1,1,0},
    				  {9/2,3/7,-3,3},
    				  {4,-1/2,0,3/4},
    				 };
    
    float C[k][n] = { {0,0,0,0},
    				  {0,0,0,0},
    				  {0,0,0,0},
    				 };
    
    for(i = 0 ; i < k ; i++)
    	{
    		for(j = 0 ; j < n ; j++)
    			{
    				for(l = 0 ; l < m ; l++)
    					{
    						C[i][j] += A[i][l]*B[l][j];
    					}
    			}
    	}
    	
    printf("      A \n------------\n");
    	
    for(i = 0 ; i < k ; i++)
    	{
    		for(j = 0 ; j < m ; j++)
    			{
    				printf("%.0f  ", A[i][j]);
    			}
    		printf("\n");
    	}
    
    printf("\n");
    printf("      B \n------------\n");
    
    for(i = 0 ; i < k ; i++)
    	{
    		for(j = 0 ; j < m ; j++)
    			{
    				printf("%.0f   ", B[i][j]);
    			}
    		printf("\n");
    	}
    	
    printf("\n");
    printf("      C \n------------\n");
    
    for(i = 0 ; i < k ; i++)
    	{
    		for(j = 0 ; j < m ; j++)
    			{
    				printf("%f  ", C[i][j]);
    			}
    		printf("\n");
    	}
    
    printf("\n");	 	    
    
    }
    this is what comes out
    A
    ------------
    2 0 0 0 4
    0 1 4 -2 1
    0 3 -1 0 1

    B
    ------------
    1 0 -2 0 5
    5 3 0 -1 0
    0 -1 1 0 4

    C
    ------------
    18 0 -4 0 1
    1 -1 10 -7 19
    19 10 -1 -3 1

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Your fractions turn out to be zero because you're dealing with integers and integer division. You have to use float literals or double literals for this to work.
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Fractions
    By madmax2006 in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 11:58 PM
  2. fractions
    By zdream8 in forum C Programming
    Replies: 2
    Last Post: 05-21-2008, 09:54 PM
  3. fractions
    By joeshmoe1337 in forum C Programming
    Replies: 21
    Last Post: 09-11-2004, 01:34 AM
  4. Fractions
    By Aakash Datt in forum C++ Programming
    Replies: 8
    Last Post: 04-30-2003, 07:36 PM
  5. Fractions
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2002, 07:51 AM