CSc 352: Lecture-26

Exercises

- C Functions
  - What gets printed out?
  a) 
  #include <stdio.h>
  void fun(double d)
  {
    double d;
    printf("%f\n", d);
  }
  main()
  {
    float f = 4.2;
    fun(f);
  }



  b)
  #include <stdio.h>
  main()
  {
    extern int a, b; int c;
    printf("%d %d %d\n", a, b, c);
  }
  int a=2, b=3;
  static int c=4;




- Arrays, Pointers
  a)
  #include <stdio.h>
  main()
  {
    int a=5, *p = &a;
    printf("%d %d %d %d\n", p, *p-5, **&p, p-(p+5));
  } 
  


  b)
  #include <stdio.h>
  main()
  {
    int a[] = {10, 20, 30, 40, 50, 60}, *p = a + 3, b, c;
    b = *p+1;
    c = *++p;
    printf("%d %d %d\n", *p, b, c);
  }



  c)
  #include <stdio.h>
  void fun(int a[], int *p)
  {
    printf("%d %d\n", sizeof(a), sizeof(p));
  }
  main()
  {
    int a[] = {1, 2, 3, 4, 5, 6}, *p=a;
    printf("%d %d\n", sizeof(a), sizeof(p));
    fun(a, p);
  }

  
  d) 
  Given the declaration:
  int a[5][10];
  a[i][j] is equivalent to *(&a[0][0] + 10 *i +j);

  Similarly, given the declaration
  int a[5][10][15];
  write the equivalent expression to a[i][j][k]


  e)
  #include <stdio.h>
  void fun(int [], int);
  main()
  {
    int a[] = {1, 3, 5, 7, 9, 11};
    fun(a, sizeof(a)/sizeof(int));
  }
  void fun(int b[], int size)
  {
    if (size > 0){
      fun(&b[1], size-1);
      printf("%d\n", b[0]);
    }
  }



  f) We want to return the length of the string in each case?
  Which one(s) give a correct result?

  #include <stdio.h>
  int fun(const char *);
  main()
  {
    char *str="abc";
    int i;
    while (fun(str))
      i = fun(str);
    printf("%d\n", i);
  }
  int fun(const char *s)
  {
    static int a = 0;
    if (*s != '\0'){
      s++;
      a++;
    }
    else
      return 0;
    return a;
  }   
  

  OR

  #include <stdio.h>
  int fun(char *);
  main()
  {
    char *str="abc";
    int i;
    while (fun(str))
      i = fun(str);
    printf("%d\n", i);
  }
  int fun(char *s)
  {
    static int a = 0;
    if (*s != '\0'){
      s++;
      a++;
    }
    else
      return 0;
    return a;
  }


  OR

  #include <stdio.h>
  int fun(char **);
  main()
  {
    char *str="abcde";
    int i;
    while (fun(&str))
      i = fun(&str);
    printf("%d\n", i);
  }
  int fun(char **s)
  {
    static int a = 0;
    if (**s != '\0'){
      (*s)++;
      a++;
    }
    else
      return 0;
    return a;
  }
   
     

  OR


  #include <stdio.h>
  int fun(char **);
  main()
  {
    char *str="abcd";
    int i;
    while (fun(&str))
      i = fun(&str);
    printf("%d\n", i);
  }
  int fun(char **s)
  {
    static int a = 0;
    if (**s != '\0'){
      (*s)++;
      a++;
    }
    else
      return 0;
    return a;
  }


- Structures, Lists
  a) Assuming the following definitions to implement 
  a stack are given:
  struct stack{
    char d;
    struct stack next;
  };
  typedef struct stack st_elem;
  
  - How would you complete the prototype of pop, and push?
  (Assume no globals are used.)
  void pop(...)
  void push(...)

  - Define the function reverse that reverses a string using
  push and pop. It has the following prototype:
  void reverse(char s[]);

  b) Give the necessary typedefinitions to build a doubly
  linked list(each element has two pointers: next and prev)
  - Write a function delete_duplicate that deletes duplicate
  elemnts from the doubly linked list.


  c)Given the following definitions for binary trees:
  struct node{
    char d;
    struct node *left;
    struct node *right;
  };
  typedef struct node node_type;
  typedef node_type *btree;

  Give definitions for a general tree representation 
  and a function that converts a given binary tree into
  a general tree representation.


- Input/Output
  a) 
  #include <stdio.h>
  main()
  {
    fprintf(stdout, "String1\n");
    fprintf(stderr, "String2\n");
  }

  Think of the following commands applied to the executable
  of the above program:
  a.out>out1
  a.out>&out2
  
  What will be inside out1 and out2

  b)
  #include <stdio.h>
  #include <stdlib.h>
  main()
  {
  char c, s[]="abc", *p = s;
  int i;
  FILE *ofp1, *ofp2;
  
  ofp1 = fopen("temp1", "w");
  ofp2 = fopen("temp2", "w");
  for (i=0; i<3; i++){
    sscanf(s, "%c", &c);
    fprintf(ofp1, "%c", c);
  }
  for (i=0; i<3; i++){
    sscanf(p++, "%c", &c);
    fprintf(ofp2, "%c", c);
  }
  } 

  What is inside temp1 and temp2?