Programming II 2024/2025 (CCINF1002) - DCC/FCUP

Practical Class #02 - Conditional Execution (25/02 and 26/02)


Exercises for submission

In what concerns the "exercises during classes" component of the evaluation, the exercises that you can submit for this class are:

Deadline for submission: March 10th (submit to PII's Mooshak)

You are encouraged to talk to the professors and your colleagues if you encounter difficulties. However, any more direct help that you receive should be acknowledged in the comments of the code you submit.
After the deadline the problems will still be available on Mooshak, but the submissions will not count towards your grade.
Each practical class is worth 18% of your final component for exercices during class. Since there will be 6 classes with submissions, you can achieve maximum grade even without doing fully doing all classes.
For a problem to count you must pass all tests (that is, to have an accepted). Even if you solve all problems, the maximum on one class is 100%.
To obtain 100% it will always be enough to solve the main exercises.


With the exercises in this class you will develop the following skills:

  1. Explore the usage of logical expressions (and operators such as && and ||)
  2. Explore the usage of of conditionals (if and else)
  3. Consolidate your knowledge of coding, debugging and problem solving

If you feel stuck, go back and revise the lecture 01: C Fundamentals, 02: Expressions and mostly 03: Conditional Execution


1) My first conditional program

Save this code to a file, compile, execute it and make sure you understand everything that it is doing:

#include <stdio.h>

int main(void) {
  int n;

  printf("Insert a number: ");
  scanf("%d", &n);

  // Example of an if else instruction
  // (if only one line of instruction follows, curly braces {}
  //  are not mandatory, but could have still been used)
  if (n % 2 == 0)
    printf("%d is even\n", n);
  else
    printf("%d is odd\n", n);

  // Example of a && operator [logical AND]
  // (if more than one line of instruction follows,
  // curly braces {} are mandatory)
  if (n >= 10 && n <= 99) {
    printf("%d has 2 digits\n", n);
    printf("the 1st digit is %d\n", n%10);
    printf("the 2nd digit is %d\n", n/10);
  }

  // Example of a || operator [logical OR]
  if (n % 10 == 1 || n % 10 == 3) {
    printf("last digit of %d is 1 or 3\n", n);
  } else if (n % 10 == 5 || n % 10 == 7) {
    printf("last digit of %d is 5 or 7\n", n);
  } else {
    printf("last digit of %d is not 1, 3, 5 or 7\n", n);
  }
  
  return 0;
}

Here is an example execution when we input the number 42:

Insert a number: 42
42 is even
42 has 2 digits
the 1st digit is 2
the 2nd digit is 4
last digit of 42 is not 1, 3, 5 or 7

And here what happens when we input the number 125:

Insert a number: 125
125 is odd
last digit of 125 is 5 or 7


2) Mooshak submissions (main exercises)

Armed with what you learned from the previous exercise, you are now ready to tackle the problems from this class.


Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!

  1. [PII009] Absolutely
  2. [PII010] Best of 3
  3. [PII011] FizzBuzz
  4. [PII012] Grading Exams
  5. [PII013] Buy Me Flowers

We encourage you to speak with your colleagues and with your professors if you need help (including using Discord), but be aware that you should be trying to solve the exercises by yourself!

You get from a course what you invest on it and by actually trying to solve the problems you will learn a lot (including some of the things you will not get correct the first time, which will become lessons you will not forget).

This course is all about "putting your hands dirty" in terms of actually coding, and if you rely on other's code (or tools such as ChatGPT) you will be most of all just fooling yourself... Try to do it and enjoy the rich and dynamic experience we prepared for you! 😊

😁 suprised pikachu...



Extra exercises for consolidating your knowledge [extra]

So you enjoyed the main exercises so much, that you want more problems to submit? Don't worry we've got you covered! Here are two more exercises to consolidade your knowledge and to show you are coding for more than simply your grade 😃


Read the statements, code and try to submit Accepted solutions to all of the following problems. Don't forget to test first on your computer!

  1. [IP014] Triangle Trouble
  2. [IP015] Leap Year

Challenge exercise [challenge]

So you want one more challenge? Here you go! (it is not that hard... don't worry, we have even more interesting exercises for future classes, but for know you are just initiating your journey through the world of C coding and algorithms...)

Happy coding! 😊