UC3M

Telematic/Audiovisual Syst./Communication Syst. Engineering

Systems Architecture

September 2017 - January 2018

2.6.  Exercises

To solve the following exercises we recommend that you create a text file in your development environment and write in it the solutions. Compile and (if appropriate) execute the program to verify that it performs the expected operations. You may use the following skeleton:

#include <stdio.h>
int main() 
{

    /* Write here your code */

    return 0;
}
  1. Write the definition of a data structure to store a table of 10 integers, a string of 20 characters, a real number with single precision, and another with double precision. Next to the definition but in separated line, declare two variables with this type. Compile the program to verify that the syntax is correct.

  2. A programmer has written the following definitions:

    #define SIZE 100
    
    struct map_annotation 
    {
        char note[SIZE];
        struct coordinates point;
    };
    
    struct coordinates 
    {
       float longitude;
       float latitude;
    };
    
    struct map_annotation annotations[SIZE];

    What is the problem? (you may always copy and paste them in your program and use the compiler to check)

  3. Include in your program the variable chain to store a string of 10 characters defined as char chain[SIZE], where SIZE is 11. The string needs to be processed such that each character is modified with the character four positions ahead in the alphabet. Write the code to execute this operation (do not worry about letters w, x, y and z. At the end of the program include the following line to print the string on the screen:

    printf("%s\n", chain);
  4. Download the program enum.c in your development environment in Linux. Open a terminal with a command interpreter, compile it with the command gcc -Wall -o enum enum.c and execute it with ./enum.

    #include <stdio.h>
    
    int main(int argc, char *argv[]) 
    {
        enum status { ON = 0, OFF = 1 };
        enum status a, b;
    
        a = 0;
        a++;
        a = 20;
        b = a++;
        
        /* Imprime a y luego b en la misma linea */
        /* Print a and then b in the same line */
        printf("%d %d\n", a, b);
        return 0;
    }
    

    Which values are printed on the screen? Looking at the result, which conclusion can be drawn about the enumerated types in C?

  5. Define a data structure to store a draw of the Lotería Primitiva. That is, a group of six numbers, a complementary number and a reimbursement number all of them different between the values 1 and 49 (both included). Calculate the size of your data structure with the sizes shown in Table 2.1 (see the Rules of the game, in spanish).

  6. Declare an array of 100 integers and fill its content with any value. Declare a second array also with 100 integers. Write the code to store in each element of the second table the sum of the elements up to that position in the first table.

  7. A data structure is required to store the inbox of a mobile phone. The messages in the inbox are of two types, either SMS or MMS. The SMS have a sender (a phone number), a date/time (an integer number), a text of no more than 140 characters and a message type that can be either SMC or normal. The messages of type MMS have the same fields except that instead of the message type, they have a string of up to 200 characters with the path to the attached image. Write the necessary data structure definitions to have an array of 100 messages as inbox.

  8. Download the following program equal.c in your Linux development environment.

    #include <stdio.h>
    int main(int argc, char *argv[]) 
    {
        struct equal { int x; };
        struct equal e1, e2;
        e1.x = 1;
        e2 = e1; /* SPECIAL LINE */
        e1.x = 2;
        printf("%d\n", e2.x);
        return 0;
    }
    

    Open a terminal with a command interpreter, compile the program with the command gcc -Wall -o equal equal.c and execute it with the command ./equal. Can you guess which number is printed on the screen? After looking at the result, explain what exactly is happening in the line e2 = e1 and in general with the = operand applied to structures in C.

    If you are curious, the program Equal.java contains the Java version of this code, and the result that appears on the screen is no the same than in the case of C. Why do you think is this?

    public class Equal {
        int x;
        public Equal() {
    	x = 1;
        }
        public static void main(String args[]) {
    	Equal e1 = new Equal();
    	Equal e2 = e1; /* SPECIAL LINE! */
    	e1.x = 2;
    	System.out.println(e2.x);
        }
    }