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

Practical Class #01 - First Programs (18/02 and 19/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 3rd (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. Be able to write, compile and execute C programs
  2. Manipulate simple data and algebraic expressions
  3. Handle simple Input and Output
  4. Debug simple programs

If you feel stuck, go back and revise the lecture 01: C Fundamentals


1) My first C program

  1. Your first task is to create an environment that you can use to develop your C code:
     
  2. Now that you have a working environment, test it. Compile and execute the following code:
     
    #include <stdio.h>
    
    int main(void) {
    
      printf("Hello cruel world, can I go back to Python?\n");
      
      return 0;
    }
    

    If you have a command line with gcc and assuming you created a file called hello.c with the code above, you can simply do the following:

    If you did everything correctly, you should be able to see the following output:

    Hello cruel world, can I go back to Python?


2) My first Mooshak submissions


On this course we will use Mooshak to automatically evaluate your Python code (see Mooshak's help).

In this problem the goal is to make your first submissions in Mooshak, all for the [PII001] Hello there. The problem statement will also be available inside Mooshak. Always start by carefully reading the problem statement given!

This problem is not hard at all using a a basis the code from the last exercise, right?

  1. Start by submitting an Accepted in Mooshak. Create a file called for instance pii001.c, write the code and test it. Is it doing what you expected?
  2. After a correct submission you should now experiment with some common errors to see the behavior of Mooshak in these cases:

3) Reading my first input

  1. Implement, compile and test the following code. Do you understand what is doing?
     
    #include <stdio.h>
    
    int main(void) {
      
      int n;                           // create variable to store an integer
      printf("Number? ");              // print message asking for a number             
      scanf("%d", &n);                 // read an integer to variable n 
      printf("The number is %d\n", n); // print the value of variable n
      
      return 0;
    }
    

  2. What if you want to read more than one value? Implement, compile and test the following code. Do you understand what is doing?
    (you should simply input two numbers - there is no message asking for them and the C code goes straight to the scanf function)
     
    #include <stdio.h>
    
    int main(void) {
      
      int x, y;                  // two integer variables
      scanf("%d %d", &x, &y);    // read two integers
      printf("(%d,%d)\n", x, y); // print integers as a pair
      
      return 0;
      }

     
  3. For this exercise you will be solving the problem [PII002] Bond, James Bond!.

    1. Read the problem, code and submit an Accepted solution. Don't forget to test first on your computer!
    2. After a correct submission experiment to make, for example, by changing the ! to ? in the printf call.
      Submit to Mooshak to see what happens and to discover obtained a Wrong Answer.
      If you click on the underlined result itself you can see a detailed table containing the result of each test case and also giving you the first input where your program failed, and the respective expected output (should be something like this picture - note on how the observations report what error was obtained).

    Here are a couple more notes about Mooshak on this course:


4) My first expressions

  1. Implement, compile and test the following code. Try to carefully understand what is doing.
     
    #include <stdio.h>
    
    int main(void) {
      
      int a = 42;
      int b = 9;
      
      int sum = a + b;
      printf("%d + %d = %d\n", a, b, sum);
        
      int sub = a - b;
      printf("%d - %d = %d\n", a, b, sub);
        
      int mul = a * b;
      printf("%d * %d = %d\n", a, b, mul);
      
      int div = a / b; // a and b are integers, so the division is "truncated"
      printf("%d / %d = %d\n", a, b, div);
    
      int mod = a % b; // division remainder
      printf("%d %% %d = %d\n", a, b, mod);
    
      float fa = a;
      float fb = b;
      float fdiv = fa / fb; // a and b are floats, division is not "truncated"
      printf("%f / %f = %f\n", fa, fb, fdiv); // by default with 6 decimal places
    
      // we can print with any number of (rounded) decimal places
      float n = 1.9876;
      printf("n = %.0f (0)\n", n);
      printf("n = %.1f (1)\n", n);
      printf("n = %.2f (2)\n", n);
      printf("n = %.3f (3)\n", n);
      printf("n = %.4f (4)\n", n); 
          
      return 0;
    }

    With the knowledge you obtained from the previous code, you know everything needed the next 3 main problems

    Use the hints if you need extra help (they are hidden in case you do not want spoilers)


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

  2. [PII003] Time Goes By
    Hints

    - Create an integer variable (e.g. int d) and read to it using scanf

    - the number of hours is just... d * 60; and the number of minutes and seconds?

    - store the results in three different values and print them


  3. [PII004] Check Please
    Hints

    - Create a float variable t and an int variable n

    - Read the input and store it on the variables

    - Compute the result as t / n and print it with printf("%.2f", result_variable)


  4. [PII005] Digit Separator
    Hints

    - If the number is n then the units (rightmost digit) is simply... n % 10

    - The second rightmost digit (tens) is (n / 10) % 10. Can you see why?

    - Can you do the other 2 digits by yourself?



Extra exercises for consolidating your knowledge [extra]

PII's Mooshak classes will usually include at least one extra exercise that you can use of you want to keep testing your knowledge of the material. For this class we propose two other problems that complement the main ones.

  1. Solve and submit the problem [PII006] Fast and Furious
    Hints

    - In this problem you need to take into account the minutes, but if m is an integer value, then m/60 will truncate the result (and will always be zero)

    - If you want this m/60 to be a float, then m needs to be a float or you should use m/60.0

    - to round to the integer part you can print using %.0f


  2. Solve and submit the problem [PII007] The case of the Cylinder
    Hints

    - Can you do one problem by yourself? 😁


Challenge exercise [challenge]

IP Mooshak classes will usually include one challenge exercise, which can be much harder the previous ones and might even need knowledge that was not given explicitly on the course.

Since this is the first challenge, it is almost of the same difficulty of the previous exercises (but one small aspect was not directly give in the clases and you will need to find it by your own).

Happy coding! 😊