mirror of
https://github.com/RGBCube/CSAssignments
synced 2025-07-25 13:07:46 +00:00
Start working on new interactive runner
This commit is contained in:
parent
1080941194
commit
38ba3af643
23 changed files with 202 additions and 15 deletions
9
sources/c/example/assignment.toml
Normal file
9
sources/c/example/assignment.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
name = "example"
|
||||
given-date = "3/11/2022"
|
||||
description = "This is an example entry for C"
|
||||
directions = """
|
||||
Print "Example" 10 times.
|
||||
"""
|
||||
|
||||
[compile]
|
||||
main-file = "example.c"
|
8
sources/c/example/example.c
Normal file
8
sources/c/example/example.c
Normal file
|
@ -0,0 +1,8 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
printf("Example");
|
||||
}
|
||||
return 0;
|
||||
}
|
16
sources/c/language.toml
Normal file
16
sources/c/language.toml
Normal file
|
@ -0,0 +1,16 @@
|
|||
name = "C"
|
||||
description = "C (pronounced like the letter c) is a general-purpose computer programming language"
|
||||
|
||||
[colors]
|
||||
foreground = 0x000000
|
||||
background = 0x00599d
|
||||
|
||||
[build]
|
||||
common = "gcc -o {out-file} {main-file}"
|
||||
|
||||
[run]
|
||||
linux = "./{out-file}"
|
||||
windows = "{out-file}"
|
||||
|
||||
[dependencies]
|
||||
common = ["gcc"]
|
31
sources/c/star-rating/assignment.toml
Normal file
31
sources/c/star-rating/assignment.toml
Normal file
|
@ -0,0 +1,31 @@
|
|||
name = "star-rating"
|
||||
given-date = "30/10/2022"
|
||||
description = "A program that takes 21 reviews and calculates the average rating and prints some stars"
|
||||
directions = """
|
||||
1. Read in 21 user ratings for a movie.
|
||||
Each rating is a number from 1 to 5 stars.
|
||||
Store the ratings in an array.
|
||||
|
||||
2. Calculate the average rating. Print this
|
||||
to the screen rounded to 1 decimal point, e.g.
|
||||
4.1 stars.
|
||||
|
||||
3. Create a new array called ‘rating_frequency’
|
||||
that stores the number of ratings for
|
||||
each star, e.g. 3 one-star ratings, 5
|
||||
two-star ratings, etc.
|
||||
|
||||
4. Display the ratings' frequency to the screen,
|
||||
where an asterisk (* symbol) is printed out for
|
||||
the number of movie ratings of that star
|
||||
number. See screenshot.
|
||||
|
||||
5. Create a new array ‘rating_percent’ that
|
||||
contains the percentage of ratings for each
|
||||
star, e.g. 14.3% one star, 23.8% two star, etc.
|
||||
Display this to the screen in a similar manner
|
||||
to task 4.
|
||||
"""
|
||||
|
||||
[compile]
|
||||
main-file = "star_rating.c"
|
47
sources/c/star-rating/star_rating.c
Normal file
47
sources/c/star-rating/star_rating.c
Normal file
|
@ -0,0 +1,47 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int ratings[21];
|
||||
int rating_frequency[] = {0, 0, 0, 0, 0};
|
||||
int rating_percent[] = {0, 0, 0, 0, 0};
|
||||
int total_stars = 0;
|
||||
|
||||
for (int i = 0; i < 21; i++) {
|
||||
printf("Review number %i, enter rating (1 to 5): ", i+1);
|
||||
// read the input as int
|
||||
scanf("%i", &ratings[i]);
|
||||
|
||||
int rating = ratings[i];
|
||||
|
||||
if (rating < 1 || rating > 5) {
|
||||
fprintf(stderr, "Invalid rating! Ratings must be between 1 and 5.\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
// increment the star count for the stars
|
||||
// (-1 is because indexes start from 0)
|
||||
++rating_frequency[rating-1];
|
||||
total_stars += rating;
|
||||
}
|
||||
|
||||
float average = (float) total_stars / 21;
|
||||
printf("\nAverage rating: %.1f\n\n", average);
|
||||
|
||||
for (int star_count_i = 0; star_count_i < 5; star_count_i++) {
|
||||
printf("%i star: ", star_count_i+1);
|
||||
|
||||
for (int i = 0; i < rating_frequency[star_count_i]; i++)
|
||||
printf("*");
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
|
||||
for (int star_count_i = 0; star_count_i < 5; star_count_i++) {
|
||||
float star_count_percent = ((float) rating_frequency[star_count_i] / 21) * 100;
|
||||
printf("%i star percentage: %.1f%\n", star_count_i+1, star_count_percent);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue