-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstats.c
139 lines (110 loc) · 3.03 KB
/
stats.c
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
/******************************************************************************
* 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 Performs statistical analysis on an array of unsigned char data items
*
* Finds the maximum, minimum, mean, and median of a data set of unsigned char.
* Sorts the dataset from largest to smallest
*
* @author Ayesha Naikodi
* @date 05/16/2020
*
*/
#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 */
sort_array(test, SIZE);
print_statistics(test, SIZE);
print_array(test, SIZE);
}
/* Add other Implementation File Code Here */
void print_statistics(unsigned char *array, unsigned int length)
{
printf("Mean is: %d \n",find_mean( array, length));
printf("Median is: %d \n",find_median( array, length));
printf("Maximum is: %d \n",find_maximum( array, length));
printf("Minimum is: %d \n",find_minimum( array, length));
}
void print_array(unsigned char *array, unsigned int length)
{
int count = 0;
printf("Array is: \n");
for(int i=0; i < length; i++)
{
printf("%d\t", *(array+i));
count++;
if(count == 10)
{
printf("\n");
count = 0;
}
}
printf("\n");
}
unsigned char find_median(unsigned char *array, unsigned int length)
{
if(length%2 == 0)
{
return (*(array+(length/2)) + *(array+(length/2)-1))/2;
}
else
{
return *(array + ((length-1)/2));
}
}
unsigned char find_mean(unsigned char *array, unsigned int length)
{
unsigned int average = 0;
if(length <=0)
{
length = 1;
}
for(int i=0; i<length; i++)
{
average += *(array+i);
}
average /= length;
return (unsigned char)average;
}
unsigned char find_maximum(unsigned char *array, unsigned int length)
{
return *(array);
}
unsigned char find_minimum(unsigned char *array, unsigned int length)
{
return *(array + length -1);
}
void sort_array(unsigned char *array, unsigned int length)
{
unsigned char temp = 0;
for(int j=0; j<length; j++)
{
for(int i=0; i<(length-1); i++)
{
if(*(array+i) < *(array+i+1))
{
temp = *(array+i);
*(array+i) = *(array+i+1);
*(array+i+1) = temp;
}
}
}
}