CS 2213/1, Spring 2005      
Quiz 2 Questions
Last Name:                            First Name:                  

Answers are in RED.

  1. Separate Compilation, etc.: Consider the following C program in three separate files:

    C source file: quadratic.c
    #include <math.h>
    #include "quadratic.h"
    
    static double disc(double a, double b, double c);
    
    int roots(double a, double b, double c, double *r1p, double *r2p) {
       double d = disc(a, b, c);
       if (a == 0) return -1;
       else if (d >= 0) {
          *r1p = (-b + sqrt(d))/(2.0*a);
          *r2p = (-b - sqrt(d))/(2.0*a);
          return 1;
       }
       else
          return 0;
    }
    
    static double disc(double a, double b, double c) {
       return b*b - 4.0*a*c;
    }
    C source file: quadratic_main.c
    #include <stdio.h>
    #include "quadratic.h"
    
    int main() {
       double a, b, c, r1, r2;
       int realroots;
       printf("Enter coefficients of quadratic equation: ");
       scanf( /* PART a: REST OF SCANF BELOW */
          "%lf %lf %lf", &a, &b, &c
       
       );
       realroots = roots(a, b, c, &r1, &r2);
       if (realroots == -1)
          printf("Not a quadratic equation\n");
       else if (realroots == 0)
          printf("Complex roots\n");
       else
          printf("Real roots: %7.4f, %7.4f\n", r1, r2);
    }
    C source file: quadratic.h  (fill in a name)
    /* Third C source file here */
    /* quadratic.h: prototype for roots function */
    int roots(double a, double b, double c, double *r1p, double *r2p);
    
    
    
    

    This program works correctly, except that I have omitted a few details that you are to provide.

    1. Fill in the call to the scanf function, where it says PART a: REST OF SCANF BELOW. This call should be reading values for the coefficients a, b, and c.

    2. The program won't compile as it is. It needs a third file and a couple of additions to the other two files above. In the listings above give the contents of the third file and the additions that are needed. (One of the additions is not strictly required, but ought to be there.)

    3. What does the word static on the function disc mean? Why is it there? This means that the function is private, not accessible from outside the file.

    4. What kind of variables are r1p and r2p in the file quadratic.c? These are pointers to double type.


  2. Copy Programs:

    Consider the standard copy program in C:

    C source file: quadratic.c
    #include <stdio.h>
    #include <ctype.h>
    
    
    int main(void) {
       int ch;
       int lett = 0, digit = 0, lines = 0;
       
    
    
       while ((ch = getchar()) != EOF) {                       
          /* putchar(ch); */
          if (isalpha(ch)) lett++;
          if (isdigit(ch)) digit++;   
          if (ch == '\n') lines++;
       }
       printf("Letters: %i, digits: %i, lines: %i\n",
          lett, digit, lines);
       
    }

    1. Add code to this function (in place of the putchar(ch);) so that instead of copying, it will count the number of letters, the number of digits, and the number of lines in the input file it is reading. (Don't forget to declare variables that you use.)

    2. Add code to print these three counts.
      [Hint: the function isalpha checks if a character is a letter, and isdigit checks if a character is a digit.]
      For full credit your code must be complete, so that it would compile and execute.


Copyright © 2011, Neal R. Wagner. Permission is granted to access, download, share, and distribute, as long as this notice remains.