PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, August 14, 2022

[FIXED] why the output is blank?

 August 14, 2022     c, output     No comments   

Issue

I have to write a program for matrix multiplication.there may be an easier algorithm,but i want to know what is the problem here and if there is anything wrong with my algorithm or .... if there is any need for additional info,please tell me.the program should multiply two matrixes with variable sized columns and rows.I`m a begginner so my mistake could be realy obvious.

#include <stdio.h>

int main()
{
    int m,n,l,a,b;
    scanf("%d %d %d",&m,&n,&l);
    int A[m][n],B[n][l],AB[m][l];
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++) scanf("%d",&A[i][j]);
    }
     for(int i=0;i<n;i++){
        for(int j=0;j<l;j++) scanf("%d",&B[i][j]);
    }
      for(int i=0;i<m;i++){
        for(int j=0;j<l;j++) AB[i][j]=0;
    }
    
     for(int i=0;i<m;i++){
        for(int j=0;j<l;j++){
            a=n;
            b=0;
            while(a!=0){
                AB[i][j]=AB[i][j]+A[i][b]*B[b][j];
                a=a--;
                b=b++;
            }
        }
    }
   for(int i=0;i<m;i++){
        for(int j=0;j<l;j++){
            printf("%d ",AB[i][j]);
        }
        printf("\n");
   }
}

so I changer it according to the comments,did i do it right?because the new problem is that it is not running.I click enter but it just goes to the next line instead of showing the output.


Solution

Changed increment/decrement instructions and added output statements:

#include <stdio.h>

int main()
{
    int m,n,l,a,b;
    printf("Enter m, n, l: ");
    scanf("%d %d %d",&m,&n,&l);
    int A[m][n],B[n][l],AB[m][l];
    for(int i=0;i<m;i++){
        for(int j=0;j<n;j++) {
            printf("Enter A[%d][%d]: ", i, j);
            scanf("%d",&A[i][j]);
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<l;j++) {
            printf("Enter B[%d][%d]: ", i, j);
            scanf("%d",&B[i][j]);
        }
    }
    for(int i=0;i<m;i++){
        for(int j=0;j<l;j++) AB[i][j]=0;
    }
    
    for(int i=0;i<m;i++){
        for(int j=0;j<l;j++){
            a=n;
            b=0;
            while(a!=0){
                AB[i][j]=AB[i][j]+A[i][b]*B[b][j];
                a=a-1;
                b=b+1;
            }
        }
    }
    for(int i=0;i<m;i++){
            for(int j=0;j<l;j++){
                printf("%d ",AB[i][j]);
            }
            printf("\n");
    }
}
$ gcc -Wall matrix.c
$ ./a.out           
Enter m, n, l: 2 2 2
Enter A[0][0]: 1
Enter A[0][1]: 1
Enter A[1][0]: 1
Enter A[1][1]: 1
Enter B[0][0]: 2
Enter B[0][1]: 2
Enter B[1][0]: 2
Enter B[1][1]: 2
4 4 
4 4 
$

The output does not look blank to me. :-)



Answered By - Franck
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing