added weekly exercises of isp-ws25
This commit is contained in:
92
c/isp-ws25/weeklies/week04/w04ex01.c
Executable file
92
c/isp-ws25/weeklies/week04/w04ex01.c
Executable file
@@ -0,0 +1,92 @@
|
||||
/*
|
||||
* In this task, you should write a program that records daily temperature data for a week and perform a basic analysis. The program should do the following:
|
||||
*
|
||||
Store the temperatures of a full* week (i.e., 7 days) in an array
|
||||
Let the user define a temperature threshold for further analysis
|
||||
Calculate the average temperature of the week
|
||||
Print the number of days with a temperature above the threshold
|
||||
|
||||
Functions are already declared for these functionalities in the given code, which still need to be implemented:
|
||||
|
||||
readTemperatures(float temperatures[]): For each day of the week, this function should print Temperature for day n: (where n represents the number of the day) and the user can enter the temperature for the respective day. The input for each day should be stored in an array that is passed when called in the main() function.
|
||||
readTemperatureThreshold(): The user can enter a temperature threshold after printing Temperature threshold: . The value should be returned to the main() function.
|
||||
calculateAverageTemperature(float temperatures[]): In this function, the average temperature of all values in the array is calculated and returned.
|
||||
countDaysWithTemperatureAboveThreshold(float temperatures[], float threshold): This function should return the number of days on which the recorded temperature is above (i.e., greater than) the user-defined threshold.
|
||||
|
||||
|
||||
These functions are called in the main() function. Both the average temperature (with two decimal places) and the threshold (with two decimal places) along the days with temperature above the threshold are printed in the main() (see the example below).
|
||||
|
||||
This is an example:
|
||||
|
||||
Temperature for day 1: 24.7
|
||||
Temperature for day 2: 22.8
|
||||
Temperature for day 3: 28.3
|
||||
Temperature for day 4: 28.9
|
||||
Temperature for day 5: 30
|
||||
Temperature for day 6: 24.8
|
||||
Temperature for day 7: 23.2
|
||||
Temperature threshold: 25.5
|
||||
Average temperature: 26.10
|
||||
Days with temperature above 25.50: 3
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#define DAYS_PER_WEEK 7
|
||||
|
||||
void readTemperatures(float temperatures[]);
|
||||
float readTemperatureThreshold();
|
||||
float calculateAverageTemperature(float temperatures[]);
|
||||
int countDaysWithTemperatureAboveThreshold(float temperatures[], float threshold);
|
||||
|
||||
int main()
|
||||
{
|
||||
float temperatures[DAYS_PER_WEEK] = {0};
|
||||
float temperature_threshold = 0.0;
|
||||
float average_temperature = 0.0;
|
||||
int days_above_threshold = 0;
|
||||
|
||||
// Add your code here
|
||||
readTemperatures(temperatures);
|
||||
temperature_threshold = readTemperatureThreshold();
|
||||
average_temperature = calculateAverageTemperature(temperatures);
|
||||
printf("Average temperature: %.2f\n", average_temperature);
|
||||
days_above_threshold = countDaysWithTemperatureAboveThreshold(temperatures, temperature_threshold);
|
||||
printf("Days with temperature above %.2f: %d", temperature_threshold, days_above_threshold);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Add your code here
|
||||
void readTemperatures(float temperatures[])
|
||||
{
|
||||
for(int day = 0 ; day < DAYS_PER_WEEK ; day++)
|
||||
{
|
||||
printf("Temperature for day %d: ", (day + 1));
|
||||
scanf("%f", &temperatures[day]);
|
||||
}
|
||||
}
|
||||
float readTemperatureThreshold()
|
||||
{
|
||||
float threshold = 0;
|
||||
printf("Temperature threshold: ");
|
||||
scanf("%f", &threshold);
|
||||
return threshold;
|
||||
}
|
||||
float calculateAverageTemperature(float temperatures[])
|
||||
{
|
||||
float average_temperature = 0;
|
||||
for(int day = 0 ; day < DAYS_PER_WEEK ; day++)
|
||||
{
|
||||
average_temperature += temperatures[day];
|
||||
}
|
||||
return (average_temperature / DAYS_PER_WEEK);
|
||||
}
|
||||
int countDaysWithTemperatureAboveThreshold(float temperatures[], float threshold)
|
||||
{
|
||||
int days_above_threshold = 0;
|
||||
for(int day = 0 ; day < DAYS_PER_WEEK ; day++)
|
||||
{
|
||||
if (temperatures[day] > threshold){days_above_threshold++;}
|
||||
}
|
||||
return days_above_threshold;
|
||||
}
|
||||
91
c/isp-ws25/weeklies/week04/w04ex02.c
Executable file
91
c/isp-ws25/weeklies/week04/w04ex02.c
Executable file
@@ -0,0 +1,91 @@
|
||||
/*
|
||||
In this task, you should write a program that checks if an array contains duplicate integer values.
|
||||
|
||||
First, the program prints Enter the number of elements to store in the array (max MAX_ARRAY_SIZE): . Then, the user can specify the size of the array (limited to the given constant MAX_ARRAY_SIZE). If the input of the user is less than 1 or greater than MAX_ARRAY_SIZE, the program should print Invalid size\n and ask the user again for the size of the array.
|
||||
|
||||
Once the size has been specified by the user, the user can enter n (i.e., n = size of the array) elements as integer values. This is done one after another by asking the user Element i: to enter the ith element.
|
||||
|
||||
After all elements have been entered by the user, the duplication check is performed. For this, implement the following function:
|
||||
|
||||
containsDuplicates(int values[], int size): This function takes the array containing the user-defined values and the size of the array as parameters. The function should return whether the array contains any duplicate values or not. The function should return the boolean value true if the array contains duplicate values, and false otherwise.
|
||||
|
||||
In the main() function, call containsDuplicates(int values[], int size) and print The array contains duplicates\n if the return value is true, and The array does not contain duplicates\n if the return value is false.
|
||||
|
||||
This is an example:
|
||||
|
||||
Enter the number of elements to store in the array (max 10): 4
|
||||
Element 1: 2
|
||||
Element 2: 9
|
||||
Element 3: -1
|
||||
Element 4: 9
|
||||
The array contains duplicates
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_ARRAY_SIZE 10
|
||||
|
||||
void getArraySize(int* array_size);
|
||||
void getArrayValues(int values[], int array_size);
|
||||
bool containsDuplicates(int values[], int array_size);
|
||||
|
||||
int main(void)
|
||||
{
|
||||
bool has_duplicates = 0;
|
||||
int array_size = 0;
|
||||
getArraySize(&array_size);
|
||||
int values_to_check[array_size];
|
||||
memset(values_to_check, 0, array_size*sizeof(int));
|
||||
getArrayValues(values_to_check, array_size);
|
||||
has_duplicates = containsDuplicates(values_to_check, array_size);
|
||||
|
||||
if (has_duplicates)
|
||||
{
|
||||
printf("The array contains duplicates\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("The array does not contain duplicates\n");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void getArraySize(int* array_size)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
printf("Enter the number of elements to store in the array (max %d): ", MAX_ARRAY_SIZE);
|
||||
scanf("%d", array_size);
|
||||
if ( 1 > *array_size || *array_size > MAX_ARRAY_SIZE )
|
||||
{
|
||||
printf("Invalid size\n");
|
||||
continue;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void getArrayValues(int values[], int array_size)
|
||||
{
|
||||
for ( int counter = 0 ; counter < array_size ; counter++ )
|
||||
{
|
||||
printf("Element %d: ", (counter + 1));
|
||||
scanf("%d", &values[counter]);
|
||||
}
|
||||
}
|
||||
|
||||
bool containsDuplicates(int values[], int array_size)
|
||||
{
|
||||
for (int counter_a = 0 ; counter_a < array_size ; counter_a++)
|
||||
{
|
||||
printf("Loop A, Position %d: %d\n", counter_a, values[counter_a]);
|
||||
for (int counter_b = counter_a + 1 ; counter_b < array_size ; counter_b++)
|
||||
{
|
||||
printf("Loop B, Position %d: %d\n", counter_b, values[counter_b]);
|
||||
if (values[counter_a] == values[counter_b]){ return true; }
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user