MP 1: Emojis
Overview
Your first MP in CS 240 is all about getting conformable with memory allocation, pointers, and character encodings in C. You will complete several different sects of functions to build up your C programming skills.
Initial Files
You will download and turn in your code using the internal CS github server known as github-dev
.
- Follow our guide to getting your github-dev set up.
- Once you have your github setup, run the following commands for your cs240/netid/ directory:
git fetch release git merge release/mp1 -m "Merging initial files"
- If you are already know how to run C++ code on your own computer, you’re probably all set to run C code. If not, you will want to get you set up to be able to run and compile C code:
- For an editor, I personally use Visual Studio Code (free, open-source). Any plain text editor should work great.
- For a compiler, we will use
gcc
. If you are on Windows, I highly recommend you use the Windows Subsystem for Linux to run the latest Ubuntu inside of Windows. (This is how run my C code myself.) - Once you have Ubuntu installed, run
sudo apt install gcc make valgrind
in your Ubuntu terminal to get the required tools to program and debug C code. - If nothing else works, the CS department provides the server
linux.ews.illinois.edu
can can be accessed via ssh to compile/run your code. - Don’t hesitate to ask us in helping get you set up. Getting started programming should be easy and you should understand how your setup works, the challenge should be in the programming (not getting started). :)
Machine Problem
Part 1: Adding Integers
In add_int.c
, you will find four functions that require you to add two integers together. Complete each function to return the sum of the two integers provided:
-
In some functions, the integers are provided via an int pointer (
int *
). You will need to access the contents of the memory pointed to by the pointer to find the integer value. (You may need to review how to dereference a pointer from your C++ course.) -
In some functions, the function will require you to return your value in newly allocated memory. In C, you can create new memory using the
malloc
call that will return a pointer to the newly allocated heap memory (similar to thenew
keyword in C++). To allocate exactly enough memory to store anint
, the following code allocates the exact memory required to store anint
and stores that in the pointer namedresult
:
int *result = malloc( sizeof(int) );
Running Your Program
We have provided a make
file for you to run your code:
- In your terminal, type
make
to compile your program. - To run your code, run
./mp1
. You should just focus on the functions from Part 1 at this point. - You can view/edit the
part1
function inmain.c
to see what test cases we have used. (Feel free to edit this file to add more test cases on your own!)
Part 2: Capitalizing Strings
In capitalizing.c
, you will find three functions where you will capitalize one or more letters in a provided string.
-
Remember that strings in C are a pointer (
char *
) to the beginning of sequence of bytes that ends with aNULL
byte (0x00
). This means that everything you know about pointers applies here and there is nothing “special” about a string (besides the termination condition). -
When detecting a lower-case letter, examine an ASCII table to find the range of all possible lower case letters. By using the table, you should also find a way to easily translate any lower-case letter to upper-case. Please do not write 26 if-statements for every possibly letter, you can do it with just math! :)
-
In completing these functions, try to use the earlier functions in your later code. For example, can you call
capitalize
incapitalizeAll
so you do not have to rewrite or copy/paste code?
Running Part 2
Compile your program with make
, focusing on the output related to functions from Part 2.
Part 3: Emojis! 🎉
In emoji.c
, you will find your final five functions where you will combine everything you know into working with UTF-8 encoded strings.
-
In
emoji_favorite
, we just want to know your favorite emoji. You will need to specify a string of bytes that encodes your favorite emoji. In C, you can specify multiple hex-values in a string by using\x
followed by two hex digits. For example:"\x45\x67"
is the string containing0x45
(E
) and0x67
(g
). Almost all emojis will require four bytes for a single character. -
For the purpose of this MP, we will consider an emoji to be anything in the inclusive range
U+1F000
-U+1F9FF
. (There are some invalid characters in this range and a few early emojis outside of this range, but we want to keep it simple. Feel free to be more accurate, we will only test your code on real emoji within this range and your solution won’t break if you program a more correct solution.) -
In
emoji_invertChar
, you get to invert an emoji. You must invert “😊” (U+1F60A
) to some sort of sad face (your choice!). The inversion is a symantec, so you should invert the meaning of the emoji (not necessarily flipping the bits). In addition to “😊”, you need to invert five other emojis of your choice (and it’s okay to do more than just five). -
Finally, in
emoji_random_alloc
, you should generate a new random emoji! The C documentation for rand outlines how to create a random integer in C, which you’ll need to use for your emoji. Make sure that you return a different emoji each timeemoji_random_alloc
is called.
Running All Three Parts
You can test all three parts of your program together:
- To compile everything simply run
make
. - To run your code, run
./mp1
and everything should look correct! 🎉
Submit
When you have completed your program, double-check all three parts run without errors and gets the result your expect. When you are ready, submit the code via the following git commands:
git add -A
git commit -m "MP submission"
git push origin master
You can verify your code was successfully submitted by viewing your git repo via the web interface here: https://github-dev.cs.illinois.edu/cs240-fa20