Skip to content

In-Class Exercise

1. Practice

There exists an artificial celestial body in the Solar System that no one has ever detected before, even though it is really close to us: on the far side of the Moon. This celestial body was created by an alien civilization, the Axerwálians, whose goal is to plunder Earth. And they don’t just want anything—they want our dear, beloved cows. Their plan is to abduct all of them. We know one crucial fact about this civilization: the members of the Axerwálians respect and serve their leader, Queen Olga, in an unprecedented way, and they are also extremely superstitious. As the first step of their mission, they are mapping Earth’s cow population.

motto

  1. Write a Hello world! program without using an IDE!

    • compile it from the terminal (with the -Wall -Werror flags)!
    • also generate it using CLion (set the -Wall -Werror flags manually)!
  2. Get familiar with the preprocessor and macros!

    • Try out what the -E flag does. Why do we get the result we see?
    • Learn about macros. Create a MY_FAV_NUM macro representing your favorite number! Use it in output and check the preprocessor’s output!
    • What happens if we put code between #if SECRET_NUM == 1 and #endif, and why?
    • Use command-line compilation to make the program run again!
  3. Create a header file containing your macro definitions!

    • Copy your macros into it.
    • Recompile the program while using your header file!
    • Make the header file include itself!
    • What’s the solution to the problem that appears?
  4. Extend the main function! Read an integer that specifies how many cows the aliens managed to identify in total (N).

  5. From the next N lines, read the data for each cow! Each line contains a name and a weight value (e.g., "Riska 125").

    • use an unsigned type for the loop variable. What do you notice? How can you fix it?
  6. The aliens are not satisfied with every cow. The abduction fails if:

    • the cow’s weight is less than 50
    • the cow’s name is shorter than 3 characters
    • the cow’s name is "Olga"
  7. Concatenate the names of the successfully abducted cows into a single string, separated by spaces! (e.g., "Mari Pali Riska")

  8. Count how many abductions failed! Print the number in the following format:

    • "Failed abductions: 10"
  9. Implement the previous task in a way that you first create the output text inside a string and then print it!

  10. Try what happens if we pass the resulting string to the std::stoi (string to integer) function!

    • Handle the error using try / catch! (We will learn about exceptions later, for now just get used to them using the reference.)
    • Try with different inputs!
      • "10 Failed abductions:"
      • "Failed abductions:"
      • "10"
    • After the try / catch block, print the value you got back as an integer!
  11. Before printing, insert an assert that checks whether the returned value equals the number of failed abductions! Use the debugger to step through the program!


Got stuck, couldn’t follow the class?! Or just want to review? We’ll show you one possible solution to the tasks!

Solution to the in-class exercise

solution.cpp



Last update: 2025-10-01
Created: 2025-10-01