DPST1092 Revision 01 Questions
Revision Exercise: Change RGB Colours (Pointers)
1092 fetch-activity c_revision_change_colour
Your task is to add code to this function in change_colour.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_change_colour/activities/c_revision_change_colour/files.cp/change_colour.c"
Complete the shift_colour to change the RGB values so that:
- the new green value = old red value
- new blue = old green
- new red = old blue
Once your program is working, you should see something like:
dcc change_colour.c -o change_colour ./change_colour RGB Value: 10 20 30 Shifted RGB Value: 30 10 20 ./change_colour RGB Value: 255 0 90 Shifted RGB Value: 90 255 0
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_change_colour
Revision Exercise: Change RGB Colours (Structs)
1092 fetch-activity c_revision_change_colour_2
Currently the red, green and blue values are stored in three separate integers.
Add code to struct rgb so that it contains three integers called red, green and blue.
Rewrite main so that struct rgb is used rather than three separate variables.
Add code to the function change_colour so that given a struct rgb * (a pointer to an rgb struct), you change the RGB values so that:
Complete the shift_colour to change the RGB values so that
- the new green value = old red value
- new blue = old green
- new red = old blue
dcc change_colour_2.c -o change_colour_2 ./change_colour_2 RGB Value: 10 20 30 Shifted RGB Value: 30 10 20 ./change_colour_2 RGB Value: 255 0 90 Shifted RGB Value: 90 255 0
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_change_colour_2
Revision Exercise: Read in and Malloc an Array
1092 fetch-activity c_revision_malloc_array
For this question you'll need to complete two functions; create_array and print_double.
Part 1: Creating an Array using Malloc
Your task is to add code to this function in malloc_array.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_malloc_array/activities/c_revision_malloc_array/files.cp/malloc_array.c"
create_array function so that
it returns an integer array size elements long.
Fill the array by using scanf to read in each element.
You will have to use malloc to allocate space on
the heap for your new array.
Part 2: Print the double of each element of an array
Your task is to add code to this function in malloc_array.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_malloc_array/activities/c_revision_malloc_array/files.cp/malloc_array.c"
print_double function so that it
prints double each element in array.
The double of each element should be separated by a space. You should not change the array itself.
Once your code is working, it should look something like:
dcc malloc_array.c test_malloc_array.c -o malloc_array ./malloc_array Enter size of array: 10 Enter array elements: 1 2 3 4 5 6 7 8 9 10 Array elements doubled: 2 4 6 8 10 12 14 16 18 20
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_malloc_array
Revision Exercise: Daily Average Temperature
1092 fetch-activity c_revision_daily_avg_temp
Your task is to add code to this function in daily_avg_temp.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_daily_avg_temp/activities/c_revision_daily_avg_temp/files.cp/daily_avg_temp.c"
A weather station records the temperature 5 times each day.
The temperatures for the last three days are stored in a
2-dimensional array
called daily_temperature. Each row corresponds to a single day and has 5 columns
for the temperatures taken throughout that day.
Add code to the function daily_avg_temp
so that given the 2-dimensional integer array daily_temperature
you print out the average temperature for each day.
Once your program is working, you should see something like:
./daily_avg_temp Average temperature for day 0 = 23 Average temperature for day 1 = 28 Average temperature for day 2 = 20
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_daily_avg_temp
Revision Exercise: Clamp the length of a string
1092 fetch-activity c_revision_clamp_string
Your task is to add code to this function in clamp_string.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_clamp_string/activities/c_revision_clamp_string/files.cp/clamp_string.c"
Add code to the clamp_string function to ensure that string is at most limit characters long.
You should not change the arguments or return type of the clamp_string function. You can (and should) change the string variable directly.
You do not need to print anything in the function.
Once your program is working, you should see something like:
dcc clamp_string.c test_clamp_string.c -o clamp_string ./clamp_string Input: Hello, how are you? Limit: 5 Output: Hello ./clamp_string Input: Hello!! Limit: 30 Output: Hello!!
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_clamp_string
Revision Exercise: Remove File Suffix
1092 fetch-activity c_revision_remove_file_suffix
Write a program remove_file_suffix.c which reads
filenames from its input and outputs the filename without the suffix.
The suffix tells you the file type and is at the end of the filename
after a full stop. For example the suffix of test.txt is .txt.
You should use fgets to read in each filename. Your program should only stop at the end of input.
If the filename doesn't have a suffix you should ignore it.
Once you have remove_file_suffix.c working correctly it should behave as follows:
dcc remove_file_suffix.c -o remove_file_suffix ./remove_file_suffix hello.py hello test.txt test ./remove_file_suffix hello hello.c hello
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_remove_file_suffix
Revision Exercise: Combine two sentences into a single string
1092 fetch-activity c_revision_combine_sentences
There are two functions to complete for this task.
Your task is to add code to this function in combine_sentences.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_combine_sentences/activities/c_revision_combine_sentences/files.cp/combine_sentences.c"
Complete the strip_newline so that it removes the newline
at the end of sentence.
Your task is to add code to this function in combine_sentences.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_combine_sentences/activities/c_revision_combine_sentences/files.cp/combine_sentences.c"
combine_sentences so that it returns
a new string which is sentence_1 and sentence_2
joined together.
You will need to use malloc to allocate space for the new string.
You should use your strip_newline function to get rid of the newlines from the sentences before combining them.
When your program is working, it should look something like:
dcc combine_sentences.c test_combine_sentences.c -o combine_sentences ./combine_sentences Sentence 1: What a wonderful day Sentence 2: to write assembly Combined: What a wonderful dayto write assembly
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_combine_sentences
Revision Exercise: Blast off! (Recursion)
1092 fetch-activity c_revision_blast_off
Your task is to add code to this function in blast_off.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_blast_off/activities/c_revision_blast_off/files.cp/blast_off.c"
perform_launch function so that it counts
down to launch one second at a time. Each passing second should be printed on a new line.
You must use recursion to do this question. You are not permitted to use while or for loops.
When your code is working, it should look like:
./blast_off 10 10 9 8 7 6 5 4 3 2 1 Blast off!
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_blast_off
Revision Exercise: Movie Reviews (Structs)
1092 fetch-activity c_revision_movie_review
You're writing a program to rate movies, each movie rating is
stored in a struct movie_review. The review contains
the name of the movie, release year and a rating out of 10.
You need to complete two functions new_review and print_review.
Your task is to add code to this function in movie_review.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_movie_review/activities/c_revision_movie_review/files.cp/movie_review.c"
new_review function so that it returns
a pointer to a new struct movie_review filled out with
the title, release year and rating.
You should remove the newline at the end of the title before
storing it in the struct.
Your task is to add code to this function in movie_review.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_movie_review/activities/c_revision_movie_review/files.cp/movie_review.c"
print_review function so that given a pointer to
a movie review m you print the title, release year and rating.
When your code is working, it should look like:
./movie_review Enter title: 10 Things I Hate About You Enter release year: 1999 Enter rating from 0-10: 10 New review: Movie: 10 Things I Hate About You Released: 1999 Rating: 10/10
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_movie_review
Revision Exercise: Find Well-Rated Movies (Structs)
1092 fetch-activity c_revision_find_movies
Your task is to add code to this function in find_movies.c:
INTERNAL ERROR MISSING FILE: "./activities/c_revision_find_movies/activities/c_revision_find_movies/files.cp/find_movies.c"
find_and_print_movies function should search the struct movie_review array to find movies rated at or above a minimum rating
and print out any movie with a high enough rating.
The struct is defined in find_movies.h.
If the user has entered an invalid rating (not between 1 and 10), you should not search for movies and print a message saying the rating is invalid.
When your code is working, it should look like:
dcc find_movies_main.c find_movies.c -o find_movies ./find_movies Find movies rated at or above: 4 You could watch: - Jurassic Park (1993): 8/10 - Alien (1979): 9/10 - The Thing (1982): 10/10 - Despicable Me 4 (2024): 6/10 - Spiderman 3 (2007): 5/10 - Legally Blonde (2001): 8/10 - Mean Girls (2004): 7/10 ./find_movies Find movies rated at or above: 100 Invalid rating, must be between 0 and 10.
When you think your program is working you can use autotest to run some simple automated tests:
1092 autotest c_revision_find_movies