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

Practical Class #04 - Functions and Arrays (18/03 and 19/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 31st (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 function definition and calling, argument passing and return statements
  2. Explore how functions help in reusing code and making it more organized
  3. Explore arrays, how to create and how to use them
  4. Have a basic understanding of what algorithmic efficiency is
  5. 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, 04: Cycles and mostly 05: Functions, 06: Arrays and 07: About Algorithms


This week's theme for the exercises is Stranger Things.


1) My first function

On this class you will be using functions. Here is a very simple example to remind you of them:

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

    #include <stdio.h>
    
    // Simple function to return the sum of two integers
    int sum(int a, int b) {
      return a+b;
    }
    
    // Simple function to return the maximum of two integers
    int max(int a, int b) {
      if (a>b) return a;
      else return b;
    }
    
    int main(void) {
    
      // You can call functions passing variable arguments
      int a = 2;
      int b = 3;  
      int c = sum(a,b);
      printf("sum(%d, %d) = %d\n", a, b, c);
    
      // You can also call them directly
      printf("sum(4, 5) = %d\n", sum(4,5));
      printf("max(42, 1) = %d\n", sum(42,1));
      printf("max(6, 10) = %d\n", sum(6,10));
      
      return 0;
    }
    
    

    Here is its output:

    sum(2, 3) = 5
    sum(4, 5) = 9
    max(42, 1) = 43
    max(6, 10) = 16
    

     
  2. Can you create and test a new function to return the minimum of two numbers?

2) My first Mooshak function 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!

Note that for the exercises of the class you need to submit a .c file with only the function definition (and no main function).
For instance, the following code could be the full content of a pii025.c file to submit on Mooshak (it would give Wrong Answer as result because obviously it does not produce the correct answer).

int triangle(int a, int b, int c) {
  return 0;
}

If you want to test on your computer you can compile with the test code that is given on the problem statement:



3) My first array code

Test the code examples from the lectures on arrays. Make sure to compile, run and understand what's happening:


4) Array problems

Can you now solve four problems with arrays?


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. [IP030] Shifting Messages
     
    Hints

    - Can you do one without any help?


     
  2. [IP031] The Perfect Ice Cream
     
    Hints

    - Binary search is your friend :)


     

Challenge exercise [challenge]

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! 😊