-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstats.c
More file actions
153 lines (107 loc) · 3.36 KB
/
stats.c
File metadata and controls
153 lines (107 loc) · 3.36 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/******************************************************************************
* Copyright (C) 2017 by Alex Fosdick - University of Colorado
*
* Redistribution, modification or use of this software in source or binary
* forms is permitted as long as the files maintain this copyright. Users are
* permitted to modify this and use it to learn about the field of embedded
* software. Alex Fosdick and the University of Colorado are not liable for any
* misuse of this material.
*
*****************************************************************************/
/**
* @file <stats.c>
* @brief <The main code>
*
* <This code can analyze an array of unsigned char data items and report analytics on the maximum, minimum, mean, and median of the data set. This code allows also to reorder this data set from large to small. All statistics are rounded down to the nearest integer. >
*
* @author <Mouna Baklouti>
* @date <11/05/2021>
*
*/
#include <stdio.h>
#include "stats.h"
/* Size of the Data Set */
#define SIZE (40)
void main() {
unsigned char test[SIZE] = { 34, 201, 190, 154, 8, 194, 2, 6,
114, 88, 45, 76, 123, 87, 25, 23,
200, 122, 150, 90, 92, 87, 177, 244,
201, 6, 12, 60, 8, 2, 5, 67,
7, 87, 250, 230, 99, 3, 100, 90};
/* Other Variable Declarations Go Here */
/* Statistics and Printing Functions Go Here */
print_statistics(test,SIZE);
sort_array(test, SIZE);
print_array(test, SIZE);
}
/* Add other Implementation File Code Here */
void print_statistics(unsigned char *test,int size) {
printf("Median is %d\n", find_median(test, size));
printf("Mean is %d\n", find_mean(test, size));
printf("Maximum is %d\n", find_maximum(test, size));
printf("Minimum is %d\n", find_minimum(test, size));
}
void swap(unsigned char* xp, unsigned char* yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
void sort_array(unsigned char *array, int length)
{
int min_idx =0;
/*SORT*/
for (int i = 0; i <length - 1; i++) {
// Find the minimum element in unsorted array
min_idx = i;
for (int j = i + 1; j < length; j++)
if (array[j] > array[min_idx])
min_idx = j;
// Swap the found minimum element
// with the first element
swap(&array[min_idx], &array[i]);
}
}
double find_median(unsigned char *array, unsigned int length)
{
double median =0;
if (length % 2 != 0)
median = array[length / 2];
else median = (double)(array[(length - 1) / 2] + array[length / 2]) / 2.0;
return median;
}
int find_mean(unsigned char *array, unsigned int length)
{
int mean =0;
for (int i=0;i<length;i++)
{
mean = mean + array[i] ;
}
mean = mean / length;
return mean;
}
unsigned char find_maximum(unsigned char *array, int length)
{
unsigned char max = array[0];
for (int i=1;i<length;i++)
{
if (array[i] > max) max = array[i];
}
return max;
}
unsigned char find_minimum(unsigned char *array, int length)
{
unsigned char min = array[0];
for (int i=1;i<length;i++)
{
if (array[i] < min) min = array[i];
}
return min;
}
void print_array(unsigned char *array, unsigned int length)
{
for (int i=0;i<length;i++)
{
printf ("Array[%d]=%d \n",i,array[i]);
}
}