Practical Class #05 - Mooshak, Strings and Pointers

Versão em Português


Mooshak: Exercises for Submission

For the purpose of the grade assigned to the resolution of exercises throughout the semester, the exercises you can submit from this class are the following exercises, which are part of the set of exercises from the last two practical classes:

Submission deadline: March 31 (submit on the Imperative Programming Mooshak)

You are encouraged to talk with the instructors and other colleagues if you encounter difficulties. However, any direct help you receive from colleagues should be acknowledged in the comments of the program you submit.
After the deadline, the problems will remain available on Mooshak, but submissions will no longer count toward your grade.
Each class accounts for 10% of the grade for this component.
For a problem to count, you must pass all the tests (i.e., get accepted). Even if you solve all the problems, the maximum score per class is 100%.
To achieve 100%, it will always be sufficient to solve the main exercises.


Exercise 1) Understanding Pointers

  1. Let i be an integer variable and let p and q be pointers to integers. Indicate which of the following assignments are legal.
    1. p = i;
    2. *p = i;
    3. p = &q;
    4. p = &i;
    5. &i = p;
    6. p = q;
    7. p = *q;
    8. *p = q;
    9. *p = *q;

  2. Let i be an int variable and p a pointer to i. Indicate which of the following read and write instructions are correct.
    1. printf("%d",i);
    2. scanf("%d",i);
    3. scanf("%d",&i);
    4. printf("%d",p);
    5. printf("%d",*p);
    6. scanf("%d",p);
    7. printf("%p",p);
    8. printf("%p",i);
    9. printf("%p",*p);

  3. Suppose that the following declarations are in effect:
    int a[] = {5, 15, 34, 54, 14, 2, 52, 72}; 
    int *p = &a[1], *q = &a[5];
    1. What is the value of *(p+3)?
    2. What is the value of *(q-3)?
    3. What is the value of q - p?
    4. Is the condition p < q true or false?
    5. Is the condition *p < *q true or false?

  4. Suppose that high, low and middle are all pointer variables of the same type, and that low and high point to elements of an array. Why is the following statement illegal, and how could it be fixed?
    middle = (low + high) / 2;

  5. Rewrite the following function to use pointer arithmetic instead of array subscripting. In other words, eliminate the variable i and all uses of the [] operator. Make as few changes as possible.
    void storezeros(int a[], int n){
      int i ;
    
      for (i= 0; i < n; i++)
        a[i]= 0 ;
    }
    

  6. In the previous practical class you had to write a program to produce an Inverted Identity Matrix, where you used two nested for loops to accomplish it. Rewrite that code, to print the Identity Matrix (instead of the inverted) using a single pointer to step through the array one element at a time.

    Hint: Since we won't be using row and col index variables, it won't be easy to tell where to store the one's (1). Instead, we can use the fact that the first element of the array should be 1; the next N elements should be 0; the next element should be 1; and so forth. Use a variable to keep track of how many consecutive 0's have been stored: when the count reaches N, it’s time to store 1.

  7. Rewrite the following function to use pointer arithmetic instead of array subscripting. Use a single loop instead of nested loops.
    int sumTwoDimensionalArrays(const int a[][LEN], int n) {
      int i, j, sum = 0;
    
      for (i= 0 ; i < n; i++) 
        for (j= 0; j < LEN; j++) 
          sum += a[i][j] ;
    
      return sum;
    }
    

  8. Implement a function with the following declaration:
    void decompose(int totalSecs, int *hours, int *mins, int *secs);
    Given the total of seconds totalSecs, an integer, the function must decompose it into hours, minutes (0-59) and seconds (0-59); the result values must be assigned to the contents of the pointers hours, mins and secs. You can assume that the total number of seconds is greater than zero.

  9. Implement a function with the following declaration:
    void twoLargest(int vec[], int size, int *pmax1, int *pmax2);
    Given a sequence of integers represented by array vec[] with size size, the function must determine the two largest values in the sequence; the results must be assigned to the positions pointed to by pmax1 and pmax2. You can assume that size is always greater than one.

  10. Consider the function presented in the lecture to reverse the order of characters in a string. You should rewrite an alternative version of that function using pointers instead of indexes. The function should have the following declaration:
    void reverse(char *str);

  11. Write a function to search for a character in a string using pointers. The function should have the following declaration:
    char *occursAt(char *str, char ch);
    The result should be a pointer to the first occurrence of the ch character (if it occurs) or NULL otherwise.


Exercise 2) More Problems with Pointers and Strings

  1. Implement a function similar to the predefined function in C:
    char *strcat(char s1[], char s2[]);
    which concatenates the string s2 to s1 and returns the address of the first string.

  2. Implement a function similar to the predefined function in C:
    char *strcpy (char *dest, char source[]);
    which copies the string source to the string dest and returns the value of the latter.

  3. Implement a function similar to the predefined function in C:
    int strcmp(char s1[], char s2[]);
    which compares (lexicographically) two strings. The result of the function should be as follows:

  4. Implement a function similar to the predefined function in C:
    char *strstr(char s1[], char s2[]);
    which determines the position where the string s2 occurs in the string s1. The function should return NULL if s2 does not occur in s1.

  5. Implement a function with the following declaration:
    void truncW(char text[], int n);
    Given a string text composed by several words (words are separated by one or more spaces) and an integer n, the function truncates all words so that each word has at most n characters.

    Example:
    char text[] = "university, faculty and fraternity"
    truncW(text, 4);  // text becomes "univ facu and frat"
    
    Note that the word "and" has only 3 characters (3 < 4) and so it remains unaltered (i.e. it isn't truncated).

  6. Implement a function with the following declaration:
    int remConsecutives(char s[]);
    in which given a string s as input, it outputs the length of a substring with all repeated characters removed.

    Example:
    int result = remConsecutive("aabcccaac"); // outputs 3, which corresponds to substring "abc"
    

  7. Implement a function with the following declaration:
    int triSup(int N, float m[N][N]);
    that verifies if a given square matrix is ​​upper triangular, that is, all elements below the diagonal are zeros.

  8. One way of representing index sets is to use an array of integers containing 1's or 0's depending on whether or not that index belongs to the set. So the set {1, 4, 7} would be represented by an array in which the first 8 positions would contain the following sequence of 1's and 0's: {0,1,0,0,1,0,0,1}.

    1. Implement a function int unionSet(int N, int v1[N], int v2[N], int res[N]) which places in the array res the result of the union of the sets v1 and v2.

    2. Implement a function int intersectSet(int N, int v1[N], int v2[N], int res[N]) which places in the array res the result of the intersection of the sets v1 and v2.