TRENDING NEWS

POPULAR NEWS

C / Write A Function To Add Two Matrices A And B And Save The Result In C

C++/ Write a function to add two matrices a and b and save the result in c?

Write a function to add two matrices a and b and save the result in c. The header for the function is
const int N = 3;
void addMatrix(const double a[][N], const double b[][N], double c[][N]);
Hint: Each element cij is aij + bij

How do I write a C program to find the sum of two matrices?

/* Program to add two matrices entered by the user * /# include
# include
void main ( )
{
int i, j, a[3][3], b[3][3], sum[3][3];
clrscr ( ) ;
printf ("Enter the elements of 1st array .\n") ;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
scanf ("%d", &a[i][j]) ;
}
}
printf ("Elements of 1st array are: \n") ;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
printf ("%d\t", a[i][j]) ;
}
printf ("\n") ;
}
printf ("Enter the elements of 2nd array .\n") ;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
scanf ("%d", &b[i][j]) ;
}
}
printf ("Elements of 2nd array are: \n") ;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
printf ("%d\t", b[i][j]) ;
}
printf ("\n") ;
}
printf ("The sum of both the matrices are: \n") ;
for (i=1;i<=3;i++)
{
for (j=1;j<=3;j++)
{
sum[i][j] = a[i][j] + b[i][j];
printf ("%d\t", sum[i][j]) ;
}
printf ("\n") ;
}
getch ( ) ;
}
P.S.- If you want to learn more basic programs like this, then you can check out this app available on Google Play Store. It has basic programs of C, C++ and Java.BCA - Course Programming - Android Apps on Google Play

How do I write a program for 2*3 to 3*2 matrix multiplication in C?

Since it satisfies matrix multiplication condition i.e if two matrices m*n and p*q is to be multiplied, n must be equal to p.If the matrices 2*3 and 3*2 are already taken as input in first[2][3] and second [3][2] respectively, then,#include#includevoid main(){int i,j,k,sum=0, product[2][2], first[2][3], second [3][2];clrscr();for(i=0;i<2;i++){for(j=0;j<3;j++)scanf(“%d”,& first [i][j]);}for(i=0;i<3;i++){for(j=0;j<2;j++)scanf(“%d”,& second[i][j]);}for(i=0;i<2;i++){for(j=0;j<2;j++){for(k=0;k<3;k++){sum=sum+ first[i][k] * second[k][j];}product [i][j]=sum;sum=0;}}getch();}

I have 2 matrices (2X3) and (3X2). i need conditions so that when i multiply them, i dont get the identity.?

the conditions need to be set on the matrices so that when i multiply them, i dont get the identity matrix. i dont need like one exception. i need something that works for every case.
the matrices are:
A is 2x3
A= [a b c
d e f ]

B is 3x2
B= [g h
i j
k l ]

How do I write a C program to calculate the multiplication of two numbers?

Program to multiply two numbers in C:#include
#include
void main()
{
int one, two, multiply;
printf("Enter first number - ");
scanf("%d",&one);
printf("Enter second number - ");
scanf("%d",&two);
multiply = one * two;
printf("The multiplication of numbers %d and %d is %d",one,two,multiply);
getch();
}
Output:Enter first number – 20Enter second number – 4The multiplication of numbers 20 and 4 is 80So this was, the program to multiply two numbers in C.

How can I write a C program to find the sum of two one-dimensional array and store it in another variable?

You can store that into a variable#include
using namespace std;

int main()
{
int sum;
int a[2]={1,2};
int b[2]={3,4};
for (int i=0; i<2 ;i++)
{
sum+=a[i]+b[i];
}
cout<<"sum of two 1D array is ="< return 0;
}

Can someone help me with a Java problem? matrixAdd...?

This is the question:

Write a method named matrixAdd that accepts a pair of two-dimensional arrays of integers as parameters, treats the arrays as 2D matrices and adds them, returning the result. The sum of two matrices A and B is a matrix C where for every row i and column j, Cij = Aij + Bij. You may assume that the arrays passed as parameters have the same dimensions.

This is what I have so far:

public static int[][] matrixAdd(int[][] a, int[][] b){
int[][] result = new int[a.length][b[0].length];
for (int i = 0; i < result.length; i++)
for (int j = 0; j < result[0].length; j++)
result[i][j] = a[i][j] + b[i][j];
return result;
}

It works for the most part, but I need to find a way to return "{}" when the input is matrixAdd({}, {}).
I just get an ArrayIndexOutOfBoundsException.

Please help me. I've tried everything I can think of.

Summation and graphing in MATLAB?

Don't use a for loop for this. Matlab is built for matrix operations, so it will be faster if you use matrix operations instead of loops when possible. When you want to evaluate a 2D function at many points simultaneously, use meshgrid. On a matrix, sum actually sums the rows of a column. That is, A=[a,b;c,d] then sum(A) returns [a+c,b+d]. So for yours you would use:

n=0:1:5;
x=-2*pi:.1:2*pi;
% This next line makes it so you can evaluate it as a 2-variable function instead of using loops
[X N]=meshgrid(x,n);
tmp=sin((2*N-1)*pi.*X/2)./(2*N-1);
total=sum(tmp);
figure;plot(x,total)

The error it's giving you is because you have to declare/set x before the loop (order matters in Matlab). Also, in a for loop, the correct syntax is
for n=0:5
The semicolon isn't necessary. If you wanted to use a loop for some reason (like the arrays would be so gigantic that Matlab would run out of memory storing them), the code would be:

totalequation=0;
x=-2*pi:.1:2*pi;
for n=0:5
equation=sin((2*n-1)*pi*x/2)/(2*n-1);
totalequation=totalequation+equation;
end
figure
plot(x,totalequation)

Same results.

TRENDING NEWS