Matrix Add
/* Program MAT_ADD.C
**
** Illustrates how to add two 3X3 matrices.
**
** Peter H. Anderson, Feb 21, '97
*/
#include <stdio.h>
void add_matrices(int a[][3], int b[][3], int result[][3]);
void print_matrix(int a[][3]);
void main(void)
{
int p[3][3] = { {1, 3, -4}, {1, 1, -2}, {-1, -2, 5} };
int q[3][3] = { {8, 3, 0}, {3, 10, 2}, {0, 2, 6} };
int r[3][3];
add_matrices(p, q, r);
printf("\nMatrix 1:\n");
print_matrix(p);
printf("\nMatrix 2:\n");
print_matrix(q);
printf("\nResult:\n");
print_matrix(r);
}
void add_matrices(int a[][3], int b[][3], int result[][3])
{
int i, j;
for(i=0; i<3; i++)
{
for(j=0; j<3; j++)
{
result[i][j] = a[i][j] + b[i][j];
}
}
}
void print_matrix(int a[][3])
{
int i, j;
for (i=0; i<3; i++)
{
for (j=0; j<3; j++)
{
printf("%d\t", a[i][j]);
}
printf("\n");
}
}
class Add { public static void main(String args[]) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(system.in)); System.out.println("enter no of rows m") int m=Integer.parseInt(br.readLine); System.out.println("enter no of columns n"); int n=Integer.parseInt(br.readLine); int a[][] = new a[m][n]; int b[][] = new b[m][n]; int c[][] = new c[m][n]; System.out.println("enter the elements of matrix a); for(int i=0; i<m; i++) { for(j=0;j<n;j++) { int a[][]=Integer.parseint(br.readLine); } } System.out.println("enter the elements of matrix b); for(int i=0; i<m; i++) { for(j=0;j<n;j++) { int b[][]=Integer.parseint(br.readLine); } } system.out.println("he sum of 2 matrices is") for(i=0;i<m;i++) { for(j=0;j<n;j++) { c[][]=a[][]+b[][]; System.out.print(c[][]); } System.out.println(); } } }
class Ex
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(system.in));
System.out.println("ENTER ROWS :") ;
int m;
m =Integer.parseInt(br.readLine);
System.out.println("ENTER COLUMNS :");
int n ;
n=Integer.parseInt(br.readLine);
int a[][] = new a[m][n];
int b[][] = new b[m][n];
int c[][] = new c[m][n];
System.out.println("enter the elements of matrix a");
for(int i=0; i<m; i++)
{
for(j=0;j<n;j++)
{
int a[][]=Integer.parseInt(br.readLine);
}
}
System.out.println("enter the elements of matrix b");
for(int i=0; i<m; i++)
{
for(j=0;j<n;j++)
{
int b[][]=Integer.parseInt(br.readLine);
}
}
system.out.println("HEY ! WATCH iT ");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
c[][]=a[][]+b[][];
System.out.print(c[][]);
}
System.out.println();
}
}
}
//By SANJIV MALVIYA
If this is a homework assignment, you really should consider doing it yourself.
/* sum of two matrixes */
matrixsum (int *a, int *b, int*r, int xmax, int ymax) {
int elements = xmax * ymax;
while (elements-- != 0) *r++ = *a++ + *b++;
}
int firstarray[10][20] = {/* values for first array */}
int secondarray[10][20] = {/* values for second array */}
int resultarray[10][20]; /* result array */
matrixsum(firstarray, secondarray, resultarray, 10, 20);
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],c[10][10];
int m,n,i,j;
clrscr(); // for clear screen
printf("Enter the order of matrix");
scanf("%d%d" ,&m,&n);
printf("\n Enter the elements of Ist matrix");
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d" ,&a[i][j]);
}
}
printf("\n Enter the elements of IInd matrix");
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d" ,&b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j]; // for addition operation
printf("The matrix is ...");
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d" ,c[i][j]);
}
}
printf("\n")
getch();
}
#include<iostream>
#include<ostream>
#include<iomanip>
#include<time.h>
class matrix
{
friend std::ostream& operator<< (std::ostream& lhs, const matrix& rhs);
public:
matrix(size_t rows, size_t columns);
matrix(const matrix& rhs);
~matrix();
matrix& randomise();
matrix operator+(const matrix& rhs);
matrix& operator+=(const matrix& rhs);
private:
int** m_data;
size_t m_rows;
size_t m_columns;
};
matrix::matrix(size_t rows, size_t columns): m_data( NULL ), m_rows(rows), m_columns( columns)
{
m_data = new int*[m_rows];
for( size_t row=0; row<m_rows; ++row )
{
m_data[row] = new int[m_columns];
for( size_t column=0; column<m_columns; ++column)
{
m_data[row][column]=0;
}
}
}
matrix::matrix(const matrix& rhs): m_data( NULL ), m_rows(rhs.m_rows), m_columns(rhs.m_columns)
{
m_data = new int*[m_rows];
for( size_t row=0; row<m_rows; ++row )
{
m_data[row] = new int[m_columns];
for( size_t column=0; column<m_columns; ++column)
{
m_data[row][column]=rhs.m_data[row][column];
}
}
}
matrix::~matrix()
{
for( size_t row=0; row<m_rows; ++row )
delete[]m_data[row];
delete[]m_data;
}
matrix& matrix::randomise()
{
for( size_t row=0; row<m_rows; ++row )
{
for( size_t column=0; column<m_columns; ++column)
{
m_data[row][column]=rand();
}
}
return(*this);
}
matrix matrix::operator+(const matrix& rhs)
{
size_t max_row=m_rows<rhs.m_rows?m_rows:rhs.m_rows;
size_t max_column=m_columns<rhs.m_columns?m_columns:rhs.m_columns;
matrix temp( max_row, max_column );
temp+=*this;
temp+=rhs;
return(temp);
}
matrix& matrix::operator+=(const matrix& rhs)
{
size_t max_row=m_rows<rhs.m_rows?m_rows:rhs.m_rows;
size_t max_column=m_columns<rhs.m_columns?m_columns:rhs.m_columns;
for(size_t row=0; row<max_row; ++row)
for(size_t column=0; column<max_column; ++column)
m_data[row][column]+=rhs.m_data[row][column];
return(*this);
}
std::ostream& operator<< (std::ostream& lhs, const matrix& rhs)
{
for(size_t row=0; row<rhs.m_rows; ++row)
{
for(size_t column=0; column<rhs.m_columns; ++column)
{
lhs<<std::setw(6)<<rhs.m_data[row][column];
}
std::cout<<std::endl;
}
return( lhs );
}
int main()
{
srand((unsigned)time(NULL));
matrix m1(5,6);
m1.randomise();
matrix m2(6,5);
m2.randomise();
matrix m3 = m1 + m2;
std::cout<<"Matrix 1:\n\n"<<m1<<std::endl;
std::cout<<"Matrix 2:\n\n"<<m2<<std::endl;
std::cout<<"Matrix 3:\n\n"<<m3<<std::endl;
}
Oops is not a programming language. OOP is an acronym for object-oriented programming, a programming paradigm that represents concepts as objects with attributes and methods. Languages that make use of OOP include SmallTalk, C++, Objective-C and Java.
For the purpose of this answer, we will look at one possible implementation in C++ for adding two matrices using objects.
#include<iostream>
#include<iomanip>
#include<time.h>
class matrix
{
friend std::ostream& operator<< (std::ostream& lhs, const matrix& rhs);
public:
matrix(size_t rows, size_t columns);
matrix(const matrix& rhs);
~matrix();
matrix& randomise();
matrix operator+(const matrix& rhs);
matrix& operator+=(const matrix& rhs);
private:
int** m_data;
size_t m_rows;
size_t m_columns;
};
matrix::matrix(size_t rows, size_t columns): m_data( NULL ), m_rows(rows), m_columns( columns)
{
m_data = new int*[m_rows];
for( size_t row=0; row<m_rows; ++row )
{
m_data[row] = new int[m_columns];
for( size_t column=0; column<m_columns; ++column)
{
m_data[row][column]=0;
}
}
}
matrix::matrix(const matrix& rhs): m_data( NULL ), m_rows(rhs.m_rows), m_columns(rhs.m_columns)
{
m_data = new int*[m_rows];
for( size_t row=0; row<m_rows; ++row )
{
m_data[row] = new int[m_columns];
for( size_t column=0; column<m_columns; ++column)
{
m_data[row][column]=rhs.m_data[row][column];
}
}
}
matrix::~matrix()
{
for( size_t row=0; row<m_rows; ++row )
delete[]m_data[row];
delete[]m_data;
}
matrix& matrix::randomise()
{
for( size_t row=0; row<m_rows; ++row )
{
for( size_t column=0; column<m_columns; ++column)
{
m_data[row][column]=rand();
}
}
return(*this);
}
matrix matrix::operator+(const matrix& rhs)
{
size_t max_row=m_rows<rhs.m_rows?m_rows:rhs.m_rows;
size_t max_column=m_columns<rhs.m_columns?m_columns:rhs.m_columns;
matrix temp( max_row, max_column );
temp+=*this;
temp+=rhs;
return(temp);
}
matrix& matrix::operator+=(const matrix& rhs)
{
size_t max_row=m_rows<rhs.m_rows?m_rows:rhs.m_rows;
size_t max_column=m_columns<rhs.m_columns?m_columns:rhs.m_columns;
for(size_t row=0; row<max_row; ++row)
for(size_t column=0; column<max_column; ++column)
m_data[row][column]+=rhs.m_data[row][column];
return(*this);
}
std::ostream& operator<< (std::ostream& lhs, const matrix& rhs)
{
for(size_t row=0; row<rhs.m_rows; ++row)
{
for(size_t column=0; column<rhs.m_columns; ++column)
{
lhs<<std::setw(6)<<rhs.m_data[row][column];
}
std::cout<<std::endl;
}
return( lhs );
}
int main()
{
srand((unsigned)time(NULL));
matrix m1(5,6);
m1.randomise();
matrix m2(6,5);
m2.randomise();
matrix m3 = m1 + m2;
std::cout<<"Matrix 1:\n\n"<<m1<<std::endl;
std::cout<<"Matrix 2:\n\n"<<m2<<std::endl;
std::cout<<"Matrix 3:\n\n"<<m3<<std::endl;
}
If you want to add two matrices you need to make sure that they are both of the same size and type. For instance, if you want to add two 1D matrices it will look like this:
...
int main()
{
const int size = 10;
double array1[size] = {0.0};
double array2[size] = {0.0};
double sum[size] = {0.0};
...
//Enter values for your matrices
...
for(int i = 0; i < size; i++)
{
sum[i] = array1[i] + array2[i];
}
...
return 0;
}
You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....
That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.
write a vb program to find the magic square
huh?
Write and run a client and a server program in C-language using UDP
http://www.assignmentsclub.com/
abdulrahman
#include<
i cant write
the transpose of null space of A is equal to orthogonal complement of A
Did you know that memory allocation is not needed to display the matrix? However, the C program is to find the sum of all the elements.
Program below?!
You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....
Poor boy
That depends on where you define them. Arrays defined inside functions are declared on the stack (like other variables defined in functions). Arrays defined outside of any function, or using the static keyword inside a function are allocated in the static data area of the program. Other arrays may be allocated using malloc() (or "new" in C++); these are allocated on the heap.
write a sample program using asp.net explaining all the syntax and semantics of the program
write a vb program to find the magic square