Reorder problems

This commit is contained in:
Madeline Busig 2024-10-30 11:07:19 -07:00
parent 2faea7bcfb
commit 7c56902d6d
6 changed files with 57 additions and 57 deletions

View File

@ -1,7 +1,8 @@
#ifndef P1_H #ifndef P2_H
#define P1_H #define P2_H
int count_alphanumeric(const char* str); void find_avg_min_max(double* arr, int size,
double* out_avg, double* out_min, double* out_max);
#endif #endif

View File

@ -1,8 +0,0 @@
#ifndef P2_H
#define P2_H
void find_avg_min_max(double* arr, int size,
double* out_avg, double* out_min, double* out_max);
#endif

7
include/p5.h Normal file
View File

@ -0,0 +1,7 @@
#ifndef P1_H
#define P1_H
int count_alphanumeric(const char* str);
#endif

View File

@ -1,19 +1,32 @@
#include "p1.h" #include "p2.h"
#include <ctype.h> void find_avg_min_max(double* arr, int size,
double* out_avg, double* out_min, double* out_max) {
int count_alphanumeric(const char* str) { // If there are no elements in the array, attempting to access the first
int num_alphanumeric = 0; // element to initialize min and max will access memory outside the bounds
const char* current = str; // of the array. Instead, we will return early.
if (size <= 0) {
while (*current != '\0') { return;
if (isalnum(*current)) {
++num_alphanumeric;
} }
++current; double sum = 0;
double min = arr[0];
double max = arr[0];
for (int i = 0; i < size; ++i) {
sum += arr[i];
if (arr[i] < min) {
min = arr[i];
} }
return num_alphanumeric; if (arr[i] > max) {
max = arr[i];
}
}
*out_avg = sum / size;
*out_min = min;
*out_max = max;
} }

View File

@ -1,32 +0,0 @@
#include "p2.h"
void find_avg_min_max(double* arr, int size,
double* out_avg, double* out_min, double* out_max) {
// If there are no elements in the array, attempting to access the first
// element to initialize min and max will access memory outside the bounds
// of the array. Instead, we will return early.
if (size <= 0) {
return;
}
double sum = 0;
double min = arr[0];
double max = arr[0];
for (int i = 0; i < size; ++i) {
sum += arr[i];
if (arr[i] < min) {
min = arr[i];
}
if (arr[i] > max) {
max = arr[i];
}
}
*out_avg = sum / size;
*out_min = min;
*out_max = max;
}

19
src/p5.c Normal file
View File

@ -0,0 +1,19 @@
#include "p1.h"
#include <ctype.h>
int count_alphanumeric(const char* str) {
int num_alphanumeric = 0;
const char* current = str;
while (*current != '\0') {
if (isalnum(*current)) {
++num_alphanumeric;
}
++current;
}
return num_alphanumeric;
}