added weekly exercises of isp-ws25
This commit is contained in:
84
c/isp-ws25/weeklies/week03/w03ex02.c
Executable file
84
c/isp-ws25/weeklies/week03/w03ex02.c
Executable file
@@ -0,0 +1,84 @@
|
||||
/*
|
||||
[] implement logTemperature()
|
||||
[x] take current loop iteration as argument
|
||||
[x] print "Enter temperature log #X:"
|
||||
[x] take temperature as int
|
||||
[x] if higher / lower than constants, print "Temperature X is impossible\n"
|
||||
[x] if higher / lower than constants, ask user again
|
||||
[x] if valid, print "Logged temperature: X degree Celsius\n"
|
||||
[x] implement calculateAbsoluteDifference()
|
||||
[x] take two int as argument
|
||||
[x] return absolute difference
|
||||
|
||||
Example:
|
||||
Welcome to the temperature logger!
|
||||
Enter temperature log #1:-300
|
||||
Temperature -300 is impossible
|
||||
Enter temperature log #1:30
|
||||
Logged temperature: 30 degree Celsius
|
||||
Enter temperature log #2:20
|
||||
Logged temperature: 20 degree Celsius
|
||||
Enter temperature log #3:50000
|
||||
Temperature 50000 is impossible
|
||||
Enter temperature log #3:25
|
||||
Logged temperature: 25 degree Celsius
|
||||
The difference of min/max values is 10
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define LOWEST_POSSIBLE_TEMPERATURE (-273) // absolute zero
|
||||
#define HIGHEST_POSSIBLE_TEMPERATURE (6000) // approx. temperature of sun's photosphere (of course, temperatures can get higher)
|
||||
#define MAX_ITERATION 3
|
||||
|
||||
int logTemperature(int current_iteration);
|
||||
int calculateAbsoluteDifference(int minimum, int maximum);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Welcome to the temperature logger!\n");
|
||||
int cur_min = HIGHEST_POSSIBLE_TEMPERATURE; // initialize with highest value to find lower values
|
||||
int cur_max = LOWEST_POSSIBLE_TEMPERATURE; // initialize with lowest value to find high values
|
||||
|
||||
for (int log_index = 0; log_index < MAX_ITERATION; ++log_index)
|
||||
{
|
||||
int temp = logTemperature(log_index + 1); // +1 to start with 1 instead of 0
|
||||
if(temp < cur_min)
|
||||
{
|
||||
cur_min = temp;
|
||||
}
|
||||
if(temp > cur_max)
|
||||
{
|
||||
cur_max = temp;
|
||||
}
|
||||
}
|
||||
|
||||
int diff = calculateAbsoluteDifference(cur_min, cur_max);
|
||||
printf("The difference of min/max values is %d\n", diff);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int calculateAbsoluteDifference(int minimum, int maximum)
|
||||
{
|
||||
return maximum - minimum;
|
||||
}
|
||||
|
||||
int logTemperature(int current_iteration)
|
||||
{
|
||||
int temperature = 0;
|
||||
|
||||
printf("Enter temperature log #%d:", current_iteration);
|
||||
scanf("%d", &temperature);
|
||||
|
||||
if(temperature > HIGHEST_POSSIBLE_TEMPERATURE || temperature < LOWEST_POSSIBLE_TEMPERATURE)
|
||||
{
|
||||
printf("Temperature %d is impossible\n", temperature);
|
||||
temperature = logTemperature(current_iteration);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Logged temperature: %d degree Celsius\n", temperature);
|
||||
}
|
||||
|
||||
return temperature;
|
||||
}
|
||||
Reference in New Issue
Block a user