-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.cpp
More file actions
120 lines (78 loc) · 2.38 KB
/
Copy pathmain.cpp
File metadata and controls
120 lines (78 loc) · 2.38 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
//
//
// Compilation:
//
// g++ main.cpp -lpthread -o serial -lm
//
//
//
/* Includes */
#include <cmath>
#include <iostream> // std::cin, std::cout
/* global vars */
#define N 1000000 // length of measurement array
// Matrix of measurements
double Measurements[N];
// Populate the measurement matrix with some numbers
void generateMeasurements(void)
{
double period = N/4; // Signal period
double maxminTemperature = 200; // Range of values
for(long int i = 0; i < N; i++ ){
// Standard deviation of noise distribution
double std = 10.0;
// Generate random number in [0,1]
double noise = std * ((double) rand() / (RAND_MAX));
// Calculate noiseless measurement from a cosine curve in [-1,1]
double temperatureClean = maxminTemperature * cos( 2.0 * M_PI * (1.0/period) * (double)i);
// Generate final (noisy) measurement
double temperature = temperatureClean + noise;
//printf("Temperature: %f\n", temperature );
//fflush(stdout);
// Store measurement into global array
Measurements[ i ] = temperature;
}
}
void *calculateAverage (void *)
{
// TODO
double avgValue = 0;
for(long int i = 0; i < N; i++ )
avgValue = avgValue + Measurements[ i ] / N;
printf("Average value: %f\n", avgValue );
fflush(stdout);
pthread_exit(0); /* exit thread */
}
int main()
{
// Generate measurements. All values stored in the global array Measurements.
generateMeasurements();
pthread_t worker1;
// Menu selection
int input;
do{
/*Displays the menu to user*/
printf("Main menu\n");
printf("1. Calculate Average\n");
printf("2. Display Maximum\n");
printf("3. Display Minimum:\n");
printf("0. Exit:\n");
scanf("%d",&input);
switch(input){
case 1:
pthread_create (&worker1, NULL, calculateAverage, (void *)NULL );
break;
case 2 :
printf("TBD\n");
break;
case 3:
printf("TBD\n");
break;
default:
printf("invalid\n");
break;
}
} while(input != 0);
/* exit */
exit(0);
} /* main() */