Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <time.h>
- using namespace std;
- int main() {
- srand(time(NULL));
- const int X = 20;
- const int Y = 30;
- const int Z = 20;
- int matrixA[X][Y];
- int matrixB[Z][X];
- //Generowanie macierzy rzadkich
- for(int i=0; i<X; ++i) {
- for(int j=0; j<Y; ++j) {
- matrixA[i][j] = (rand() % 10 == 0) ? rand() % 10 : 0;
- }
- for(int k=0; k<Z; ++k) {
- matrixB[k][i] = (rand() % 10 == 0) ? rand() % 10 : 0;
- }
- }
- //Wyświetlanie macierzy
- cout << "MacierzA" << endl;
- for(int i=0; i<X; ++i) {
- for(int j=0; j<Y; ++j) {
- cout << matrixA[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- //Liczenie ilości wartości w macierzy
- int noValuesA = 0;
- for(int i=0; i<X; ++i) {
- for(int j=0; j<Y; ++j) {
- if(matrixA[i][j]!=0) noValuesA++;
- }
- }
- //Zamiana macierzy na COO
- int indexA = 0;
- int valuesA[noValuesA], rowA[noValuesA], colA[noValuesA];
- for(int i=0; i<X; ++i) {
- for(int j=0; j<Y; ++j) {
- if(matrixA[i][j]!=0) {
- valuesA[indexA] = matrixA[i][j];
- rowA[indexA] = i;
- colA[indexA] = j;
- indexA++;
- }
- }
- }
- //Wyświetlanie macierzy COO
- cout << "Wartości A" << endl;
- for(int i=0; i<noValuesA; ++i) {
- cout << valuesA[i] << " ";
- }
- cout << endl;
- cout << "Wiersze A" << endl;
- for(int i=0; i<noValuesA; ++i) {
- cout << rowA[i] << " ";
- }
- cout << endl;
- cout << "Kolumny A" << endl;
- for(int i=0; i<noValuesA; ++i) {
- cout << colA[i] << " ";
- }
- cout << endl << endl;
- //Wyświetlanie macierzy
- cout << "Macierz B" << endl;
- for(int i=0; i<Z; ++i) {
- for(int j=0; j<X; ++j) {
- cout << matrixB[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl;
- //Liczenie ilości wartości w macierzy
- int noValuesB = 0;
- for(int i=0; i<Z; ++i) {
- for(int j=0; j<X; ++j) {
- if(matrixB[i][j]!=0) noValuesB++;
- }
- }
- //Zamiana macierzy na COO
- int indexB = 0;
- int valuesB[noValuesB], rowB[noValuesB], colB[noValuesB];
- for(int i=0; i<Z; ++i) {
- for(int j=0; j<X; ++j) {
- if(matrixB[i][j]!=0) {
- valuesB[indexB] = matrixB[i][j];
- rowB[indexB] = i;
- colB[indexB] = j;
- indexB++;
- }
- }
- }
- //Wyświetlanie macierzy COO
- cout << "Wartości B" << endl;
- for(int i=0; i<noValuesB; ++i) {
- cout << valuesB[i] << " ";
- }
- cout << endl;
- cout << "Wiersze B" << endl;
- for(int i=0; i<noValuesB; ++i) {
- cout << rowB[i] << " ";
- }
- cout << endl;
- cout << "Kolumny B" << endl;
- for(int i=0; i<noValuesB; ++i) {
- cout << colB[i] << " ";
- }
- cout << endl;
- //Wymnażanie
- int matrixC[Y][Z];
- for(int i=0; i<Y; ++i) {
- for(int j=0; j<Z; ++j) {
- matrixC[i][j] = 0;
- }
- }
- //Wymnażanie COO
- clock_t beginCOO = clock();
- for(int i=0; i<noValuesA; ++i) {
- for(int j=0; j<noValuesB; ++j) {
- if(rowA[i] == colB[j])
- matrixC[colA[i]][rowB[j]] += valuesA[i]*valuesB[j];
- }
- }
- clock_t endCOO = clock();
- cout << endl;
- //Wyświetlanie wyniku COO
- cout << "Macierz wynikowa" << endl;
- for(int i=0; i<Y; ++i) {
- for(int j=0; j<Z; ++j) {
- cout << setw(3) << matrixC[i][j] << " ";
- }
- cout << endl;
- }
- //Wymnażanie Regularne
- int matrixD[Y][Z];
- for(int i=0; i<Y; ++i) {
- for(int j=0; j<Z; ++j) {
- matrixD[i][j] = 0;
- }
- }
- clock_t beginRegular = clock();
- for(int i=0; i<Y; ++i) {
- for(int j=0; j<Z; ++j) {
- int sum = 0;
- for(int k=0; k<X; ++k) sum += matrixA[k][i]*matrixB[j][k];
- matrixD[i][j] = sum;
- }
- }
- clock_t endRegular = clock();
- //Wyświetlanie wyniku regularnego
- cout << endl;
- for(int i=0; i<Y; ++i) {
- for(int j=0; j<Z; ++j) {
- cout << setw(3) << matrixD[i][j] << " ";
- }
- cout << endl;
- }
- cout << endl << "Czas COO: " << (double) (endCOO - beginCOO) / CLOCKS_PER_SEC << endl << "Czas reularny: " << (double) (endRegular - beginRegular) / CLOCKS_PER_SEC << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement