-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
132 lines (113 loc) · 3.59 KB
/
main.c
File metadata and controls
132 lines (113 loc) · 3.59 KB
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
/*
https://github.com/daltonbr/MatrixAssembly
Authors:
Bruno Vedovetto @bleandro
Dalton Lima @daltonbr
Lucas Pinheiro @lucaspin
Wesley Otto @ottozinho
Description:
Consider three matrices of integers A, B, C and L x L size, defined in C
language (these matrices could be randomly generated). Implement three
equivalent functions (all functions must run in C language application):
The matrices must be static instantiates in C, at compiling-time.
These matrix must be ramdonly POPULATED, not randonly sized. In relation to L,
its good that the size could vary in fixed intervals (5x5, 10x10, 50x50,
100x100, etc).
Group 1: (a * 2b) - returning the greater value in the main diagonal.
*/
#include <stdio.h>
#include <stdlib.h> //malloc
#include <time.h> // srand
#define L 3
void printMatrix(int matrix[L][L]);
void printDiagonal(int matrix[L][L]);
void randomPopulateMatrix(int matrix[L][L]);
int greaterValueInDiagonal(int matrix[L][L]);
void scalarTimesMatrix (int _scalar, int matrix[L][L], int outputMatrix[L][L]);
void matrixTimesMatrix (int firstMatrix[L][L], int secondMatrix[L][L], int outputMatrix[L][L]);
int main(void) {
int a[L][L];
int b[L][L];
int c[L][L];
int i, j, r;
srand(time(NULL));
printf("--== Matrix Operations in Assembly! ==--\n");
printf("* All matrices bellow has a fixed [%dx%d] size\n",L, L);
printf("* They are randomly populate with integers between 0 and 99\n");
printf("* We calculate (a*2b)\n");
printf("* We return the greater element from the main diagonal");
printf("\nPrinting 'a' matrix\n");
randomPopulateMatrix(a);
printMatrix(a);
randomPopulateMatrix(b);
printf("\nPrinting 'b' matrix: \n");
printMatrix(b);
/* (2b) */
printf("\nScalar (2) times 'b' matrix\n");
int scalar = 2;
int bPlus2[L][L], outputMatrix[L][L];
scalarTimesMatrix(scalar, b, bPlus2);
printMatrix(bPlus2);
/* (a * 2b) */
matrixTimesMatrix(a, bPlus2, outputMatrix);
printf("\n(a * 2b) = \n");
printMatrix(outputMatrix);
int greaterInDiagonal = greaterValueInDiagonal(outputMatrix);
printf("Greater Element in Diagonal: %d\n", greaterInDiagonal);
}
void printMatrix(int matrix[L][L]) {
int i, j, tempElement;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
tempElement = matrix[i][j];
printf(" [%d%d]: (%d) |", i, j, tempElement);
}
printf("\n");
}
}
void printDiagonal(int matrix[L][L]) {
int i, tempElement;
for (i = 0; i < L; i++) {
tempElement = matrix[i][i];
printf(" [%d%d]: (%d) |", i, i, tempElement);
}
}
void randomPopulateMatrix(int matrix[L][L]) {
int i, j, tempElement;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
matrix[i][j] = rand() % 100; //returns a pseudo-random integer between 0 and 99
}
}
}
int greaterValueInDiagonal(int matrix[L][L]) {
int i = 0, greaterElement;
greaterElement = matrix[i][i];
for (i = 1; i < L; i++) {
if (greaterElement < matrix[i][i]) {
greaterElement = matrix[i][i];
}
}
return greaterElement;
}
/* void or return an outputMatrix */
void scalarTimesMatrix (int _scalar, int matrix[L][L], int outputMatrix[L][L]) {
int i, j;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
outputMatrix[i][j] = _scalar * matrix[i][j];
}
}
}
void matrixTimesMatrix (int firstMatrix[L][L], int secondMatrix[L][L], int outputMatrix[L][L]) {
int i, j, k, accumulator;
for (i = 0; i < L ; i++) {
for (j = 0; j < L; j++) {
accumulator = 0;
for (k = 0; k < L ; k++) {
accumulator += firstMatrix[i][k] * secondMatrix[k][j];
}
outputMatrix[i][j] = accumulator;
}
}
}