1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
#include<iostream>
#include <vector>
#include <ctime>
#include <fstream>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <numeric>
#include <iterator>
#include <iomanip>
class matrix
{
int **A; // pointer to store 2 dimensional array
int x, y;
public:
matrix();
matrix(int, int);
void read();
void input();
void display();
matrix operator* (int);
};
//
matrix :: matrix()
{
x = 0;
y = 0;
}
matrix :: matrix(int a, int b)
{
x = a;
y = b;
}
//read matrix
void matrix :: read()
{
std::cout << " enter number of rows " << std::endl;
std::cin >> x;
std::cout << " enter number of columns " << std::endl;
std::cin >> y;
}
//matrix input
void matrix :: input()
{
int num1, num2, k;
int i, j;
A = new int *[x];
for (k = 0; k < x; k++)
A[k] = new int [y];
for (num1=0; num1<x; num1++)
{
for (num2=0; num2<y; num2++)
{
A[num1][num2] = 0;
}
}
for ( i = 0; i < x; i++)
{
for ( j = 0; j < y; j++)
{
std::cin >> A[i][j];
}
}
}
//matrix display
void matrix :: display()
{
int i, j;
for (i=0; i<x; i++)
{
for (j=0; j<y; j++)
{
std::cout << " " << A[i][j];
}
std::cout << "\n";
}
}
//matrix mult
matrix matrix :: operator* (int a)
{
matrix temp(x, y);
int num,num1, num2;
for (num1=0; num1<temp.x; num1++)
{
for (num2=0; num2<temp.y; num2++)
{
temp.A[num1][num2] = 0;
}
}
int i, j;
for ( i = 0; i < x; i++)
{
for ( j = 0; j < y; j++)
{
temp.A[i][j] = a * A[i][j];
}
}
return (temp);
}
// MAIN
int main ()
{
matrix s;
matrix t = s;
matrix M1, M2;
M1.read ();
M1.input ();
M1.display ();
M2 = M1*2;
M2.display();
return 0;
}
|