Thread: C programming Derive Matrix C from Matrix A and B

  1. #1
    Registered User
    Join Date
    Dec 2019
    Posts
    1

    C programming Derive Matrix C from Matrix A and B

    Need help with a C programming.
    I have two matrixes Matrix A , 10X2 and Matrix B 10X2.<br/>I would like to achieve matrix C in the following format.
    A=
    1 1.24
    2 1.49
    0 0
    0 0
    3 5.69
    4 3.78
    5 2.8
    6 9.2
    0 0
    0 0


    B=
    0 0
    0 0
    8 3.5
    9 4.1
    0 0
    0 0
    0 0
    0 0
    5 1.9
    9 2.5


    We are trying to operate these two matrices and achieve c, where c matrix looks like following, only mentioning the first colomn here
    C=
    9-8
    9-8
    0
    0
    9-5
    9-5
    9-5
    9-5
    0
    0

    Column 1 of C matrix is constructed like this. First, it will search the first column of B Matrix for a non-zero value, in the given example, it has found 8 and then 9 from the B Matrix. Then it will store 8 as low value, and 9 as high. It can be more values, but in this case, it was these two values. So after finding the low and high value it will put ‘High – Low’ in this case 9 – 8 and then it will search 1st column of Matrix A and replace with the ‘High – Low’ in this case 9 – 8, in the non zero places just before the group, as shown in Matrix C.


    tried with the following code

    Code:
    #include <stdio.h><br/>
    
    
    int main()<br/>
    {<br/>
        int B[]={0,0,8,9,0,0,0,0,5,9};<br/>
        int A[]={1,2,0,0,3,4,5,6,0,0};<br/>
        
        int n=0, x=0, y=0;
        int high=0, low=0, result=0;
        int i=0, j=0;    
        int C[n];
    
    
        n=sizeof(B)/sizeof(B[0]);
    
    
        printf("Length of B: %d \n",n);
    
    
        //Print b
        printf("Print B:");
        for(i=0;i<n;i++)
        {
            printf("%d \t",B[i]);
        
    }
        printf("\n");
    
    
        for(i=0;i<n;i++)
        {
            if(B[i]==0)
            {
                C[i]=B[i];
            }
    
    
            if((B[i]!=0)&&(i==0))
            {
                x=i;
                low=B[0];
                printf("Print low1:%d\n",low);
            }
            else if((B[i]==0)&&(B[i+1]!=0))
            {
                x=i+1;
                low=B[i+1];
                printf("Print low2:%d\n",low);
            }<br/>
     if((B[i]!=0)&&(B[i+1]==0))<br/>
            {<br/>
                y=i;<br/>
                high=B[i];<br/>
                printf("Print high1:%d\n",high);<br/>
    
    
                result=high-low;
    
    
                for(j=x;j<=y;j++)
                {
                    C[j]=result;
                }
            }
            else if((B[i]!=0)&&(i==n-1))
            {
                y=i;
                high=B[i];
                printf("Print high2:%d\n",high);
    
    
                result=high-low;
        
                for(j=x;j<y;j++)
                {
                    C[j]=result;
                }
            }<br/>
      if((B[i-1]==0)&&(B[i]!=0)&&(i==n-1))<br/>
            {<br/>
                C[i]=B[i];<br/>
            }​
    
    
        }
    
    
        //Print C
        printf("Print C:");
        for(i=0;i<n;i++)
        {
            printf("%d \t",C[i]);
        }
        printf("\n");
    }
    I am stuck a big time, any help will be appreciated
    Last edited by learner_sweden; 12-16-2019 at 04:44 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If would help if your coded example was 10x2.

    Code:
        int C[n];
    
        n=sizeof(B)/sizeof(B[0]);
    No, C doesn't resize your arrays just by reassigning a variable.

    Your indentation needs work.
    Indentation style - Wikipedia

    The random bits of HTML make it look like you found the code.
    Or just cross-posted it.
    using C programming Derive Matrix C from Matrix A and B - Stack Overflow
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-21-2016, 12:20 AM
  2. Replies: 5
    Last Post: 01-06-2016, 01:15 AM
  3. Replies: 2
    Last Post: 05-19-2014, 07:32 PM
  4. Need help in Matrix Addition & finding Inverse of a Matrix
    By ssatyan.129 in forum C Programming
    Replies: 6
    Last Post: 05-15-2009, 02:48 PM
  5. Matrix: Reloaded + Enter The Matrix (the game)
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-18-2003, 12:35 AM

Tags for this Thread