UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

The C Programming Language

Exercises

1.  Testing your programs with the compiler

Work Plan

Repeat in your virtual machine the steps shown in the video mentioned in the previous section.

How long did this activity take you? mins.

2.  The elements of a C program

Resources

A resource that is worth keeping it handy to check (not to read right now because it has 300 pages) is The GNU C Programming Tutorial written by Mark Burgess (Online version). It contains detail information about everything related to C.

Work Plan

After reading the suggested document, answer to the questions in the following section.

Assessment

Remember that when the compiler processes source files, it does so independently and in a single pass. Consider the following source files.

file_1.c file_2.c file_3.c
1
2
3
4
5
6
7
8
9
10
int b = 10;

void fill_squares(int *tbl, int b) 
{
    int i;
    for (i = 0; i < b; i++) 
    {
        tbl[i] = square(i + 1);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#define SIZE 10

int cube(int);

int main(int argc, char *argv[]) 
{
    int a;
    int table[SIZE];

    a = square(b);
    b = cube(a);

    fill_squares(table, SIZE);
    return 0;
}

int square(int number) 
{
    return number * number;
}
1
2
3
4
5
6
int b = 10;

int cube(int number) 
{
  return square(number) * number;
}

Answer the following questions.

  1. In file_1.c the parameter b (line 3) and the global variable b (line 1) are different variables.

    • True

    • False

  2. In line 8 of file_1.c the compiler does not know function square. A prototype needs to be included before the function to avoid a warning by the compiler.

    • True

    • False

  3. In line 10 of file_2.c the compiler knows nothing about function square.

    • True

    • False

  4. In line 11 of file_2.c the compiler knows nothing about function cube.

    • True

    • False

  5. The assignment to b in line 11 of file_2.c is correct because it is a global variable defined in another file.

    • True

    • False

  6. Function square in line 17 of file_2.c needs to be moved to the beginning of the file (or at least its prototype) for the compiler not to emit a warning.

    • True

    • False

  7. The declaration of b in line 1 of file_3.c is incorrect because it collides with the declaration in line 1 of file_1.c.

    • True

    • False

  8. The compiler has no information about function square in line 5 of file_3.c.

    • True

    • False

Post in the course forum or check with the professors those questions that you are not sure about the answer.

How long did this activity take you? mins.

3.  Solving exercises about data definitions in C

Work Plan

  1. Solve the first four exercises. Check the solutions with a classmate, posting in the course forum or going to office hours before the next class.

How long did this activity take you? mins.

4.  Solve exercises about declaring and scope of validity of variables

Resources

The following documents complement the information included in this chapter.

Work Plan

  1. Solve the first three exercises. Check your results with a classmate, posting in the forum or during office hours.

How long did this activity take you? mins.

5.  Solve exercises about functions

Resources

The following documents complement the information included in this chapter.

Work Plan

  1. Solve the first three exercises. Check your answers with your a classmate, posting in the course forum or during office hours.

How long did this activity take you? mins.

6.  Call rates

Work Plan

In an application to execute in a mobile phone, the following data structures need to be designed:

  • Structure to store a call rate. It must contain the name of the carrier company, the hour of start and end of the rate (both values between 0 and 23), the rate for establishing the call, and the minute rate (both in euros). Declare a synonym for this type. Declare a table to store 100 elements of this type.

  • Structure to store an outgoing call. It must contain the name of destination carrier, the starting hour (only the hour, between 0 and 23, no minutes nor seconds), the duration in minutes and the number called. Declare a synonym for this type. Declare a table to store 100 elements of this type.

Answer the following questions.

  • What is the size of these tables?

  • How did you decide the length of the field to store the carriers name?

  • Can you propose a more compact version of your data structures?

  • Which functions can you think that can be implemented with the information in these tables? (either processing them separately or both at the same time).

  • Let us suppose that each data structure with their functions are in separated files. A third file is added in which only the main function is defined, but it uses all the functions and the data structures previously defined. Which information must be included before the main definition?

How long did this activity take you? mins.