COMP1911 23T2 Introduction to Programming
  1. Write a function, that mirrors the behaviour of the library function strncat. Your code should use the following prototype.

    void mystrncat(char str1[], char str2[], int n);
    

    Reminder: strncat appends str2 to str1, over writing the terminating null char ('\0') at the end of str1, and then adds a terminating null byte char. strncat will use at most n bytes from str2.

  2. What does this program print?
    #include <stdio.h>
    
    int main(int argc, char *argv[]) {
        printf("%s", argv[argc - 1]);
        return 0;
    }
    
    

  3. Write a C program last_argument_reversed.c which writes out its last command line argument with the characters in reverse order.

    Your program should print nothing if there are no command line arguments.

    For example:

    ./last_argument_reversed The quick brown fox
    xof
    

  4. What are the differences between f1 and f2?
    #define SIZE 5
    int * f1(void){
        int nums[SIZE] = {1,2,3,4,5};
        return nums;
    }
    
    int * f2(void){
        int * nums = malloc(sizeof(int) * SIZE);
        int i = 0;
        while(i < SIZE){
            nums[i] = i+1;
            i = i + 1;
        }
        return nums;
    }
    

  5. Write a function with the following prototype to reverse a string
    char * reverseString(char * s);
    

    Then write a program that uses the function to read in a string from standard input and print it in reverse

  6. Consider the following code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define MAXITEMS 4
    
    int z;  
    
    void fun(int x);
    
    int main(int argc, char * argv[]) {
        
        int var = 5;
        int array[MAXITEMS] = {1,9,1,7};
        int * q = malloc(sizeof(int));
        char s0[] = "Hello";
    
        //DO SOMETHING WITH THE VARIABLES
        fun(var);
        
    
        return EXIT_SUCCESS;
    }
    
    void fun(int x){
        int myFun = x/2.0;
        printf("I am having %d times the fun you are!\n",myFun);
    }
    
    What section of memory (text,data,stack,heap) would the following be stored in
    • var

    • array

    • q

    • *q

    • s0

    • The code for the main and the function

    • z

    • x

    • myFun

    Extra Home Study Questions

    .
    1. What would be the output of the following code?
      int x = -9;
      int *p1 = &x;
      int *p2;
      
      p2 = p1;
      printf("%d\n", *p2);
      *p2 = 10;
      printf("%d\n",x);
      
      -9
      10
      
    2. Given the following code fragment:
      char goals[] = "All your goals belong to us.";
      char *a, *b, *c;
      
      a = goals + 5;
      b = &goals[10];
      c = goals + (b - goals) + (b - a);
      
      The fragment is valid C. It executes without error. Indicate clearly and exactly what the following expressions evaluate to:

      1. a == goals

      2. a > goals

      3. goals > c

      4. c - b

      5. goals - a

      6. a[0] != b[0]

      7. *c

      8. goals[a - goals] == *a

      9. c[a - b]

    3. Given the following code fragment:
      int i = 0;
      int j = 0;
      char *s = "ceded";
      
      while (s[i] != '\0') {
        j = j + s[i] - 'a';
        i = i + 1;
      }
      printf("%d %d\n", i, j);
      
      The fragment is valid C. It executes without error. Indicate clearly and exactly what output will be printed.

    4. Write a function with the prototype below that calculates the sum and the product of all the integers in the given array.
      void sumProd(int nums[], int len, int *sum, int *product);