-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram3.c
More file actions
266 lines (236 loc) · 11.3 KB
/
program3.c
File metadata and controls
266 lines (236 loc) · 11.3 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/**********************************************************************/
/* */
/* Program Name: program3 - Sum of Experimental Scientific Data */
/* Author: Ethan Pinto */
/* Installation: Pensacola Christian College, Pensacola, Florida */
/* Course: CS227, Data Structures and Algorithms */
/* Date Written: October 14, 2016 */
/* */
/**********************************************************************/
/**********************************************************************/
/* */
/* I pledge all of the lines in this C program are my own original */
/* work and that none of the lines in this C program have been copied */
/* from anyone else, unless I was specifically authorized to do so by */
/* my CS227 instructor. */
/* */
/* */
/* Signed: _____________________________________ */
/* (signature) */
/* */
/* */
/**********************************************************************/
/**********************************************************************/
/* */
/* This program assigns a list of experimental scientific data to an */
/* array, sorts the arrary into descending order, and then calculates */
/* the sum of the data. */
/* */
/**********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**********************************************************************/
/* Symbolic Constants */
/**********************************************************************/
#define PROGRAM_NUMBER 3 /* Teacher assigned course number */
#define LAST_NAME "Pinto" /* The programmers last name */
#define COURSE_NUMBER "CS227" /* PCC Assigned Course Number */
#define MIN_QUANTITY 2 /* Minimum quantity of data */
#define MAX_QUANTITY 100 /* Maximum quantity of data */
#define QUIT 0 /* Program exit value */
#define DATA_ALLOC_ERR 1 /* Can't allocate memory for scientific*/
/* data */
#define SORT_ALLOC_ERR 2 /* Can't allocate memory for sort */
/* function */
/**********************************************************************/
/* Function Prototypes */
/**********************************************************************/
void print_heading();
/* Prints the program heading */
void print_instructions();
/* Prints the program instructions */
int get_data_quantity();
/* Gets the number of data values */
void get_scientific_data(int quantity, float *p_data);
/* Gets the scientific data values */
void sort_data(int quantity, float *p_data);
/* Sorts the experimental scientific data into descending order */
void print_data(int quantity, float *p_data);
/* Prints the scientific data values */
float calculate_sum(int quantity, float *p_data);
/* Calculates the sum of all the scientific data values */
void print_sum(float data_sum);
/* Prints the sum of all the scientific data values */
/**********************************************************************/
/* Main Function */
/**********************************************************************/
int main()
{
float *p_data; /* Array of experimental scientific data values */
int quantity; /* Number of experimental scientific data values */
/* Prints the program heading */
printf("\n\n\n\n\n\n\n");
print_heading();
/* Loop processing experimental scientific data until user quits */
while(print_instructions(), (quantity = get_data_quantity()) !=
QUIT)
{
/* Allocates memory for experimental scientific data values and */
/* aborts if an error occurs */
if ((p_data = (float *) malloc(sizeof(p_data) * quantity)) ==
NULL)
{
printf("\nError # %s occured in main().", DATA_ALLOC_ERR);
printf("\nCan't allocate memory for scientific data");
printf("\nThe program is aborting");
exit(DATA_ALLOC_ERR);
}
/* Gets the experimental scientific data */
get_scientific_data(quantity, p_data);
/* Sorts the experimental scientific data */
sort_data(quantity, p_data);
/* Prints the experimental scientific data */
print_data(quantity, p_data);
/* Prints the sum of the experimental scientific data */
print_sum(calculate_sum(quantity, p_data));
/* Frees allocated memory for experimental scientific data */
free(p_data);
}
/* Prints a goodbye message and terminates the program */
printf("\n\nThanks for using this program. Have a nice day! ;-)");
printf("\n\n\n\n\n\n");
return 0;
}
/**********************************************************************/
/* Prints the Program Heading */
/**********************************************************************/
void print_heading()
{
printf("\n======================================================");
printf("\n Program Number: %d", PROGRAM_NUMBER);
printf("\n Programmer: %s", LAST_NAME);
printf("\n PCC Course Number: %s", COURSE_NUMBER);
printf("\n======================================================");
return;
}
/**********************************************************************/
/* Prints the Instructions */
/**********************************************************************/
void print_instructions()
{
printf("\n\n\n");
printf( "This program processes experimental scientific data");
printf("\n-----------------------------------------------------");
return;
}
/**********************************************************************/
/* Gets the Number of Data Values */
/**********************************************************************/
int get_data_quantity()
{
int quantity; /* Number of experimental scientific data values */
do
{
printf("\nHow many data values are there (%d to ", MIN_QUANTITY);
printf("%d, %d = quit): ", MAX_QUANTITY, QUIT);
scanf ("%d", &quantity);
}
while(quantity != QUIT && (quantity < MIN_QUANTITY || quantity >
MAX_QUANTITY));
return quantity;
}
/**********************************************************************/
/* Gets the Experimental Scientific Data */
/**********************************************************************/
void get_scientific_data(int quantity, float *p_data)
{
int counter; /* */
/* put this into the for loop*/
for(counter = 0.0f; counter < quantity; counter++)
{
printf(" Enter data value %d: ", counter + 1);
scanf ("%f", p_data);
*p_data++;
}
return;
}
/**********************************************************************/
/* Sorts the Experimental Scientific Data */
/**********************************************************************/
void sort_data(int quantity, float *p_data)
{
float *p_biggest, /* Largest value in the array of data */
*p_data_counter, /* Loop control variable for the data */
*p_sort, /* Array for the sorted data values */
*p_sort_counter; /* Loop control variable for the sorted data*/
/* Allocates memory for experimental scientific data values and */
/* aborts if an error occurs */
if((p_sort_counter = (float *) malloc(sizeof(*p_sort_counter) *
quantity)) == NULL)
{
printf("\nError %d occurred in sort function ", SORT_ALLOC_ERR);
printf("\nCan't allocate memory for scientific data");
printf("\nThe program is aborting");
exit(SORT_ALLOC_ERR);
}
/* Sort the experimental scientific data in descending order */
for(p_sort = p_sort_counter; (p_sort - p_sort_counter) < quantity;
p_sort++)
{
*p_sort = 0.0f;
for(p_data_counter = p_data; (p_data_counter - p_data) < quantity;
p_data_counter++)
{
if(*p_data_counter > *p_sort)
{
*p_sort = *p_data_counter;
p_biggest = p_data_counter;
}
}
*p_biggest = 0.0f;
}
/* Copies the sorted experimental scientific data to memory */
memcpy(p_data, p_sort_counter, sizeof(*p_sort_counter) * quantity);
/* Frees allocated memory for experimental scientific data */
free(p_sort_counter);
return;
}
/**********************************************************************/
/* Calculates the Sum of the Experimental Scientific Data */
/**********************************************************************/
float calculate_sum(int quantity, float *p_data)
{
float *p_data_counter, /* Loop control variable for the data */
data_sum = 0.0f; /* Sum of experimental scientific data */
for(p_data_counter = p_data; (p_data_counter - p_data) < quantity;
p_data_counter++)
data_sum += *p_data_counter;
return data_sum;
}
/**********************************************************************/
/* Prints the Experimental Scientific Data */
/**********************************************************************/
void print_data(int quantity, float *p_data)
{
float *p_counter; /* Loop control variable */
printf("\nThe data in descending order (with duplicates noted):");
printf("\n-----------------------------------------------------");
for(p_counter = p_data; (p_counter - p_data) < quantity; p_counter++)
{
if(*p_counter == *p_counter + 1)
printf("\n %9.2f (Duplicate)", *p_counter);
else
printf("\n %9.2f", *p_counter);
}
return;
}
/**********************************************************************/
/* Prints the Sum of Experimental Scientific Data Values */
/**********************************************************************/
void print_sum(float data_sum)
{
printf("\n ---------");
printf("\n %9.2f total", data_sum);
return;
}