added weekly exercises of isp-ws25
This commit is contained in:
52
c/isp-ws25/weeklies/week03/w03ex01.c
Executable file
52
c/isp-ws25/weeklies/week03/w03ex01.c
Executable file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
- find mistakes in the given source code
|
||||
- lines with incorrect code are commented, with the correct input below
|
||||
|
||||
[] take seconds as user input
|
||||
[] return seconds, minutes, hours as struct
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
// struct _Time_
|
||||
typedef struct _Time_
|
||||
{
|
||||
int seconds_;
|
||||
int minutes_;
|
||||
int hours_;
|
||||
//};
|
||||
} Time;
|
||||
|
||||
Time convertTime(int total_seconds);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int total_seconds = 0;
|
||||
//Time time;
|
||||
Time time = {0, 0, 0};
|
||||
|
||||
printf("Total number of seconds: ");
|
||||
//scanf("%c", &total_seconds);
|
||||
scanf("%d", &total_seconds);
|
||||
|
||||
//time = convertTime(total_seconds)
|
||||
time = convertTime(total_seconds);
|
||||
|
||||
printf("%-8s %8d\n%-8s %8d\n%-8s %8d\n", "Hours:", time.hours_, "Minutes:", time.minutes_, "Seconds:", time.seconds_);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
//float convertTime(total_seconds)
|
||||
Time convertTime(int total_seconds)
|
||||
{
|
||||
Time result;
|
||||
|
||||
result.hours_ = total_seconds / 3600;
|
||||
total_seconds = total_seconds % 3600;
|
||||
result.minutes_ = total_seconds / 60;
|
||||
//result.seconds_ = total_seconds * 60;
|
||||
result.seconds_ = total_seconds % 60;
|
||||
|
||||
return result;
|
||||
}
|
||||
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;
|
||||
}
|
||||
36
c/isp-ws25/weeklies/week03/w03ex03.c
Executable file
36
c/isp-ws25/weeklies/week03/w03ex03.c
Executable file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int lucas(int number);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int index_position = 0;
|
||||
int lucas_number = 0;
|
||||
|
||||
printf("Enter index position: ");
|
||||
scanf("%d", &index_position);
|
||||
|
||||
if(index_position < 0)
|
||||
{
|
||||
printf("Entered position must be a non-negative integer\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
lucas_number = lucas(index_position);
|
||||
|
||||
printf("lucas(%d) = %d\n", index_position, lucas_number);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int lucas(int number)
|
||||
{
|
||||
int result = 0;
|
||||
|
||||
if(number == 0){return 2;}
|
||||
if(number == 1){return 1;}
|
||||
|
||||
result = lucas(number - 1) + lucas(number - 2);
|
||||
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user