102 lines
3.1 KiB
C
102 lines
3.1 KiB
C
/*
|
|
* The saved moisture levels from Moisture Detection 2, now accessed with pointers.
|
|
* Put two readings back in order, handle a reading that never arrived, and
|
|
* report the range of the saved data.
|
|
*/
|
|
#include <stdio.h>
|
|
|
|
#define MAX_READINGS 4
|
|
|
|
void swap_readings(int a, int b);
|
|
int *level_at(int minutes[], int levels[], int count, int minute);
|
|
int level_range(const int levels[], int count, int *lowest, int *highest);
|
|
|
|
int main(void)
|
|
{
|
|
/* Slot i of minutes and slot i of levels are one reading. Minute 3 never arrived. */
|
|
int minutes[MAX_READINGS] = {1, 2, 4, 5};
|
|
int levels[MAX_READINGS] = {40, 12, 3, 7};
|
|
|
|
int lowest = -1;
|
|
int highest = -1;
|
|
int status;
|
|
|
|
/* Part A: the levels for minutes 1 and 2 were saved the wrong way round. */
|
|
swap_readings(levels[0], levels[1]);
|
|
printf("after swap: minute %d = %d, minute %d = %d\n",
|
|
minutes[0], levels[0], minutes[1], levels[1]);
|
|
if (levels[0] == 12 && levels[1] == 40) {
|
|
printf("check A: PASS\n");
|
|
} else {
|
|
printf("check A: FAIL, minutes 1 and 2 are still the wrong way round\n");
|
|
}
|
|
|
|
/*
|
|
* Part B: minutes 3 and 4 were swapped too. Once check A passes, remove
|
|
* the // from the two lines below.
|
|
*/
|
|
// swap_readings(level_at(minutes, levels, MAX_READINGS, 3),
|
|
// level_at(minutes, levels, MAX_READINGS, 4));
|
|
printf("check B: the program reached Part C\n");
|
|
|
|
/* Part C */
|
|
status = level_range(levels, MAX_READINGS, &lowest, &highest);
|
|
printf("range of %d readings: status %d, lowest %d, highest %d\n",
|
|
MAX_READINGS, status, lowest, highest);
|
|
if (status == 0 && lowest == 3 && highest == 40) {
|
|
printf("check C1: PASS\n");
|
|
} else {
|
|
printf("check C1: FAIL, expected status 0, lowest 3, highest 40\n");
|
|
}
|
|
|
|
lowest = -1;
|
|
highest = -1;
|
|
status = level_range(levels, 0, &lowest, &highest);
|
|
printf("range of 0 readings: status %d, lowest %d, highest %d\n",
|
|
status, lowest, highest);
|
|
if (status != 0 && lowest == -1 && highest == -1) {
|
|
printf("check C2: PASS\n");
|
|
} else {
|
|
printf("check C2: FAIL, an empty log has no range: return nonzero, write nothing\n");
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Given: returns the address of the level saved at minute, or NULL when
|
|
* there is no reading for that minute.
|
|
*/
|
|
int *level_at(int minutes[], int levels[], int count, int minute)
|
|
{
|
|
int i;
|
|
|
|
for (i = 0; i < count; i = i + 1) {
|
|
if (minutes[i] == minute) {
|
|
return &levels[i];
|
|
}
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* YOUR TASK: swap the caller's two readings. As written, a and b are copies,
|
|
* so the swap never reaches main().
|
|
*/
|
|
void swap_readings(int a, int b)
|
|
{
|
|
int held = a;
|
|
|
|
a = b;
|
|
b = held;
|
|
}
|
|
|
|
/*
|
|
* YOUR TASK: with at least one reading, write the lowest level through lowest
|
|
* and the highest through highest, and return 0. An empty log has no lowest
|
|
* or highest: return 1 and write nothing.
|
|
*/
|
|
int level_range(const int levels[], int count, int *lowest, int *highest)
|
|
{
|
|
(void)levels; (void)count; (void)lowest; (void)highest; /* delete once you use them */
|
|
return 1;
|
|
}
|