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

Practical Class #03 - Cycles (11/03 and 12/03)


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 24th (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 cycle instructions such as while, do...while and for (and statements such as break and continue)
  2. Explore some algorithmic patterns envolving cycles (such as using accumulators and extracting the digits of a number)
  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, 03: Conditional Execution and mostly 04: Cycles


1) My first cycle program

  1. 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 a, b;
    
      printf("Insert two numbers: ");
      scanf("%d %d", &a, &b);
    
      // Example of a for instruction that shows all numbers between a and b
      // (because the body is only one line, curly braces are optional)
      // (also note how the variable can be declared inside the for initialization)
      printf("for:");
      for (int i=a; i<=b; i++)
        printf(" %d", i);
      printf("\n");
    
      // Example of a while instruction that shows all numbers between a and b
      printf("while:");
      int i = a;
      while (i<=b) {
        printf(" %d", i);
        i++;
      }
      printf("\n");
    
      // Example of a do while instruction that shows all numbers between a and b
      printf("do...while:");
      i = a;
      do {
        printf(" %d", i);
        i++;
      } while (i<=b);
      printf("\n");
    
      
      return 0;
    }
    

    Here is an example execution when we input the numbers 2 and 6:

    Insert two numbers: 2 6
    for: 2 3 4 5 6
    while: 2 3 4 5 6
    do...while: 2 3 4 5 6
    

     
  2. What happens if you try it with the range numbers reversed (6 2)? Can you understand why for and while do not print anything? And why does do...while still print a number?
     
  3. What happens if you change all increments i++ to i+=2?
     
  4. Note how the previous change might print only odd or even numbers depending on the value of a. Can you now change the code to only print even numbers between a and b? (hint: use an if instruction combined with the remainder operator % to check if the number is even)

2) My first Mooshak cycle exercise

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


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



3) Cycles inside cycles

😁 cycles inside cycles...

  1. 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) {
    
      for (int i=0; i<3; i++)
        for (int j=0; j<5; j++)
          printf("%d %d\n", i, j);
      
      return 0;
    }
    
    
  2. Now that you know that you can have cycles inside cycles, you are ready to tackle the next problem:


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



4) Iterating through the digits of a number

  1. 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 = 5672;
    
      // print all digits of n
      while (n>0) {
        printf("%d\n", n%10);
        n /= 10;
      }
      
      return 0;
    }
    
  2. Now that you know that you can have cycles inside cycles, you are ready to tackle the next problem:


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



5) More cycle problems!

Can you now solve two problems withouth having any base code?


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



Extra exercises for consolidating your knowledge [extra]

Loved the main exercises and craving more challenges? These extra problems are here to help you sharpen your skills and reward those that are coding for the love of learning 😍


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. [IP022] Spin me right round
     
    Hints

    - While traversing the digits, if you keep the "last" digit, you can compare it to the current one, right? 😉

    - Be careful with the order you are iterating


     
  2. [IP023] Perfection
     
    Hints

    - Think first of an "easier" problem: can you calculate the reverse of a number? While iterating through the digits, how can you incrementally build the reverse number?

    - After having the original and the reversed number, the problem becomes trivial!


     

Challenge exercise [challenge]

You almost conquered the cycles set of problems, but there is still one last hurdle. Can you do the ultimate challenge of this class?

This challenge is of algorithmic nature, so you need and efficient solution and not just a correct (but more "brute force") approach if you don't want to face a Time Limit Exceeded. Can you see some patterns on the spiral and how you can take advantage of them?

This exercise is more challenging and not so trivial or immediate, so give yourself some time to think about it before looking or asking for help.

Happy coding! 😊