-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
92 lines (81 loc) · 1.77 KB
/
main.cpp
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
double * Random(int n) {
srand(time(NULL));
double *p;
p =(double*) malloc(n * sizeof(double));
while (n--) {
*(p+n)= (double)rand()/RAND_MAX*1.0;
}
return p;
}
void printMatrix(double * matrix, int n){
int i=0;
while(i<n){
printf("%0.2f ",matrix[i]);
i++;
}
printf("\n");
}
double ** Matrix(double *A, int n, int r) {
double **matrix = (double**)malloc(sizeof(double *)*r);
int cols= n/r + (n % r != 0);
int totalAdded=0;
int rowCounter=0, colCounter=0;
while (rowCounter < r) {
matrix[rowCounter] = (double *)malloc(cols*(sizeof (double)));
colCounter = 0;
while (colCounter < cols) {
if (totalAdded < n) {
matrix[rowCounter][colCounter] = A[(rowCounter*cols) + colCounter];
}
else {
matrix[rowCounter][colCounter] = 0;
}
colCounter++;
totalAdded++;
}
rowCounter++;
}
return matrix;
}
double * Max(double *A, int n){
int i=0;
double max=0, *p=NULL;
while(i<n){
if(A[i]>max){
max=A[i];
p=&A[i];
}
i++;
}
return p;
}
int main(){
int n = 8, r=8;
printf("Vnesi n: ");
scanf("%d", &n);
printf("Vnesi r: ");
scanf("%0d", &r);
clock_t start,stop;
double time;
start=clock();
double * numbers= Random(n);
stop=clock();
time=(double)(stop-start)/CLOCKS_PER_SEC;
printf("1D: \n");
printMatrix(numbers,n);
printf("2D: \n");
double **p = Matrix(numbers,n,r);
int i=0;
while(i<r){
printMatrix(p[i],n/r + (n % r != 0));
i++;
}
double * max= Max(numbers,n);
printf("Najvecja vrednost: %0.2f na naslovu %08X\n",*max,max);
printf("Cas generiranja naključnih stevil: %0.2f s\n",time);
return 0;
}