added weekly exercises of isp-ws25

This commit is contained in:
2026-04-13 19:02:19 +02:00
parent c51e836ed9
commit 00fdd31a18
28 changed files with 1835 additions and 0 deletions

View File

@@ -0,0 +1,136 @@
/*
In this task, you should get familiar with enum.
The program allows the user to enter ratings for a movie
using integers that should correspond to an enum.
Only ratings between 1 and 5 are considered.
The program processes the ratings,
prints the average rating using stars (i.e., *) and
prints a recommendation based on the average ratings.
Your task is:
- Declare a typedefd enum for the 5 categories of the rating system
- 1 == worst rating
- 5 == best rating
- all ratings are entered by the user
- calculate average rating using total_ratings and weighted_ratings
- total_ratings == total number of ratings entered by the user
- weighted_ratings == ratings weighted by their category
- ratings in 1st category: weighted with 1
- ratings in 2nd category: weighted with 3
This is necessary to calculate the average rating by dividing weighted_ratings by total_ratings
Use your typedefd enum as data type of the variable in which you store the average ratings
Use an integer division to calculate the average rating
(e.g., an average rating of 3.84 corresponds to the 3rd category).
Print the average rating using stars.
For instance, if the average rating of the movie is in the
3rd category of the rating system, then print Average rating: ***.
Print a recommendation based on the rating category of the movies average rating:
- 1st category: The movie is terrible, do not watch it
- 2nd category: The movie is bad and not worth watching
- 3rd category: The movie is average, neither especially good nor bad
- 4th category: The movie is good and worth watching
- 5th category: The movie is excellent and a must-see
- Print "Invalid rating for the movie" if none of the above cases apply.
Examples:
Enter movie ratings (1 = TERRIBLE, 2 = BAD, 3 = AVERAGE, 4 = GOOD, 5 = EXCELLENT). End with -1:
3 4 3 3 2 3 5 4 4 3 3 -1
Average rating: ***
Recommendation: The movie is average, neither especially good nor bad
Enter movie ratings (1 = TERRIBLE, 2 = BAD, 3 = AVERAGE, 4 = GOOD, 5 = EXCELLENT). End with -1:
5 4 3 4 5 4 3 4 5 4 3 5 5 5 5 5 3 3 3 -1
Average rating: ****
Recommendation: The movie is good and worth watching
*/
#include <stdio.h>
#define MAX_RATING 5
// TODO: Declare a (typedef'd) enum for a movie rating scale that consists of 5 categories
typedef enum _Ratings_ {
TERRIBLE = 1,
BAD,
AVERAGE,
GOOD,
EXCELLENT
} Ratings;
void giveRecommendation(Ratings rating)
{
printf("Recommendation: ");
switch (rating) {
case TERRIBLE:
printf("The movie is terrible, do not watch it\n");
break;
case BAD:
printf("The movie is bad and not worth watching\n");
break;
case AVERAGE:
printf("The movie is average, neither especially good nor bad\n");
break;
case GOOD:
printf("The movie is good and worth watching\n");
break;
case EXCELLENT:
printf("The movie is excellent and a must-see\n");
break;
default:
printf("Invalid rating for the movie\n");
break;
}
}
int main(void)
{
printf("Enter movie ratings (1 = TERRIBLE, 2 = BAD, 3 = AVERAGE, 4 = GOOD, 5 = EXCELLENT). End with -1:\n");
int input = 0;
int ratings[MAX_RATING] = {0};
while (1)
{
scanf("%d", &input);
if (input == -1) break;
if (input > 0 && input <= MAX_RATING)
{
ratings[--input]++;
}
}
int total_ratings = 0;
int weighted_ratings = 0;
for (int i = 0; i < sizeof(ratings)/sizeof(ratings[0]); i++)
{
total_ratings += ratings[i];
weighted_ratings += (ratings[i] * (i+1));
}
if (total_ratings == 0)
{
printf("No ratings entered\n");
return 1;
}
// TODO: Store the average rating in a variable. The data type of the variable should be your typedef'd enum
Ratings average_rating = weighted_ratings / total_ratings;
// TODO: Print the average rating by displaying stars
printf("Average rating: ");
for(int i = 0; i < average_rating; i++) printf("%c", '*');
printf("\n");
// TODO: Print the recommendation
giveRecommendation(average_rating);
return 0;
}