Skip to content

In-Class Exercise

3. Practice

The Axerwálians have launched the flying saucers. They arrived on Earth under the cover of night and immediately started intense cow kidnapping. The local farmers helplessly watch their herds shrinking.

motto


Preliminary task!

Before solving the tasks, download our starting code, which we will build upon!

Starting code

starting.cpp


  1. Read an integer that specifies how many cows the aliens have identified in total (N). Then, read the data for the cows in the next N lines! Each line contains a name and a weight (e.g., "Riska 125"). Create an object for each kidnapped cow and store them in a vector!
  2. Count how many cows in the vector weigh between 60 and 70 kg!
  3. Modify the code from the first task! Before storing, check if a cow with the given name already exists in the vector. If yes, do not add a new element, but update the weight of the existing cow!
  4. Create a set that stores cows! Insert the new cow into this as well, not just the vector. What do you observe? (Solution in the next class)
  5. Modify the set to store unsigned values! Store the weights of the cows stored in the vector, then print them after reading!
    • Try different inputs: include duplicates, smaller and larger values!
    • Observe which elements are stored!
    • Observe the order of the elements!
  6. Check if the set contains the value 69!
  7. If yes, remove this element from the set!
  8. Implement task 3 using a map as well! The map keys should be strings, and the values numbers. So the cow names will be the map keys, and the values will be the weights!
    • Print the contents of the map after storing!
    • Observe the order of the elements!
  9. Check if there is a cow named Riska in the map! If yes, update its weight to 125!
  10. Count how many cows in the map weigh less than 60 kg!
  11. Find the largest weight / the heaviest cow in all three data structures! Collect only the maximum weight in all three cases!

Got stuck or couldn’t follow the class?! Or just want to review?
We show a possible solution for the tasks!

Class exercise solution

solution.cpp



Last update: 2025-09-04
Created: 2025-09-04