SEE BODY TAG
Name:
Answer Key

SEE BODY TAG

SEE BODY TAG

 

SEE BODY TAG


Take-home test: open book, open notes.

    Answer the following questions in a text file or word-processor document. For questions that ask for programs, write a separate source-code file for each question. Submit all files to the course website as "Exam 1".

  • Compiler commands:
    1. [3 pts] Write out a command that will compile a source code file named test1.c into an object file only.
      gcc -Wall -c test1.c  
    2. [3 pts] Write out a command that will compile an object file named test1.o into an executable named test1.
      gcc -o test1 test1.o  
    3. [3 pts] Write out a single command that will compile a source code file named testmath1.c directly into an executable, assuming that the program makes use of functions in the math library such as sin() or pow().
      gcc -Wall -c testmath1.c -lm  
    4. [3 pts] Write out a command that will compile two source code files named test1a.c and test1b.c directly into a single executable named test1.
      gcc -Wall -o test1 test1a.c test1b.c  
  • Header files:
    1. [3 pts] What header file must be included if your program uses the strlen()function?
      string.h  
    2. [3 pts] What header file must be included if your program uses the atoi()function?
      stdlib.h  
    3. [3 pts] What header file must be included if your program uses the isspace()function?
      ctype.h  
    4. [3 pts] What header file must be included if your program uses the tan()function?
      math.h  
  • [3 pts] Write out a Linux command that will run an executable named "test1", which is in the current directory, and redirect its standard input from a text file named "test1.txt".
    ./test1 < test1.txt  
  • [3 pts] Suppose you are writing a program that must use integers between 0 and 100, and you must keep the program data as small as possible because memory is severely limited. What is the smallest "numeric" data type you can use?
    unsigned char
    2pt - short
    1pt - unsigned
     
  • [3 pts] Write out one C statement that declares a variable named xvalue, which can hold numbers with decimal points, and initializes it to -4.3×10-3.
    double xvalue = -4.3e-3; double xvalue = -.0043;  
  • [3 pts] Write out a single C statement that declares a variable named epsilon, which can hold high-precision real numbers (a.k.a. floating-point numbers), and initializes it to 0.123456789123456789×10-10. Use exponential notation. Do not use the pow() function.
    double epsilon = 0.123456789123456789e-10;
    2pt - float epsilon = 0.123456789123456789e-10;
     
  • [3 pts] Write out a C statement that declares an array named vowels that is exactly big enough to hold the text string "aeiou".
    char vowels[6]; OR char vowels[] = "aeiou";
    1pt - char vowels[5]
     
  • [3 pts] Write out a single C statement that declares an array named label which is exactly big enough to hold a ten-character-long ASCII text string.
    char label[11];
    3pt - char label[10]
     
  • [10 pts]

    Write a complete, correct C program that prints out each command-line argument except the program name, one per line, in reverse order.

    Include a comment with your name and the date. Demonstrate reasonable indentation.

        /* instructor 2017-10-05 */     // 2pts
        #include<stdio.h>         // 2pts
        int main(int argc, char **argv) // 2pts
        {
            int i;
            for (i = 1; i < argc; i++)   // 4pts
                printf("%s\n", argv[i]);    // 4pts
    
            return 0;                   // 1pt
        }
        
     
  • [15 pts]

    Write a function that accepts a floating-point value as its argument and returns an integer. If the argument is a value from -2.50 through 2.50, return a 0; if the argument is greater than 2.50, return a +1; if the argument is less than -2.50, return a -1. Name the function testval.

    Also write a complete C program that prompts the user for single floating-point value, calls your testval function with the user's input as its argument, and prints out the value returned by the function.

        int testval(float f)         // 2pts
        {
            if (f > 2.5)
                return 1;
            else if (f < -2.5)
                return -1;
            else
                return 0;
        }
        
     
  • [10 pts] Write a function that accepts a floating-point value and an integer as its arguments, and returns an integer. If the floating-point value and the integer are both negative, return a -1; otherwise if the integer and the floating-point value are equal, return a 0; otherwise return a +1. Name the function testvals.
        int testvals(float f, int i)         // 2pts
        {
            if (f < 0.0 && i < 0)
                return -1;
            else if (i = f)
                return 0;
            else
                return +1;
        }
        
     
  • [15 pts] Write a function that accepts a single character as its argument and returns a single letter depending on whether the argument is a lowercase vowel (return L), uppercase vowel (return U), punctuation character (return P), whitespace character (return W), or anything else (return O). (Don't write a program to use the function, just write the function itself.)

    Here is a prototype for the function:

    char testchar(char ch);

    The function must use a switch statement to decide whether the character is a lowercase vowel, an uppercase vowel, a punctuation character, or other character.

    Lowercase vowels are: ('a', 'e', 'i', 'o', and 'u')
    Uppercase vowels are: ('A', 'E', 'I', 'O', and 'U')
    Whitespace characters are: (' ', '\n', '\t', '\r', '\f', and '\v')
    Punctuation characters are: ('.', '?', '!', and ',')

        char testchar(char ch)         // 2pts
        {
            switch (ch) {
                case 'a': case 'e': case 'i': case 'o': case 'u': return 'L';
                case 'A': case 'E': case 'I': case 'O': case 'U': return 'U';
                case ' ': case '\n': case '\t': case '\r': case '\f': case '\v': return 'W';
                case '.': case '?': case '!': case ',': return 'P';
                default: return 'O';
            }
        }
        
     
  • [15 pts]
      0.445
      1.587
     -4.194
     -4.211
     -1.123
     -2.145
     -1.183
     -4.729
      2.986
     -4.887
    

    Write a program that gets a command-line argument which is an integer specifying an array. It must use the argument to create an array of doubles, then receive a series of floating-point values from standard input.

    An example set of ten numbers is shown on the right. These can be supplied by command-line redirection from a data file, or typed in by hand from the keyboard.

    Also write and use a function that accepts a pointer to the array and the size of the array; and returns the average of the elements in the array. The main() routine must then print out the average, using exponential notation.

    Your program must use pointers to access array elements, not array indexes.

    Here is the function's prototype:

    double average(double *ary, unsigned size);
    $
    $ ./prog-floats 3
    Enter 3 floating-point values:
    17.17
    -9.5
    1111.2222
    
    average: 3.729641e+02
    $
    
        double average(double *ary, unsigned size);
        {
            double s = 0;
            double *end = ary + size;                      // 2pts
            for ( ; ary & end; ary++ )
                s += *ary;
            return s/size;
        }
        
     
  • [15 pts]
    Without row and column labels:
    
       1.00   0.50   0.33   0.25
       2.00   1.00   0.67   0.50
       3.00   1.50   1.00   0.75
    
    __________________________________
    
    With row and column labels:
    
              1      2      3      4
           ----   ----   ----   ----
     1:    1.00   0.50   0.33   0.25
     2:    2.00   1.00   0.67   0.50
     3:    3.00   1.50   1.00   0.75
    
    

    Write a program that receives two command-line arguments, which it converts to unsigned integers. It must then call a function that prints out a table of the fractions formed by all integers from 1 up to and including the integers, showing two decimal places for the fractions. The first argument counts the numerators (rows); the second argument counts the denominators (columns).

    For full credit, print out row labels and a row of of column labels as well.

    For example, if the two arguments were 3 and 4, your program's function should print out one of the tables shown at right:

    Here is a prototype for the function (you can use shorter parameter names if you wish):

    void print_table(unsigned numerator, unsigned denominator);
        void print_table(unsigned numerator, unsigned denominator)
        {
            unsigned i, j;                      // 2 pts
            printf("    ");                     // 2 pts
            for (j = 1; j <= denominator; j++)          // 3 pts
                printf("  %5u", j);                     // 3 pts
            printf("\n");
            for (i = 1; i <= numerator; i++) {       //
                printf("%2u: ", i);                     //
                for (j = 1; j <= denominator; j++)        // 5 pts
                    printf("  %5.2f", (float)i/(float)j); // 2 pts
                printf("\n");
            }
        }
        
     
  • [15 pts]

    Write a complete, correct C program that prompts for and reads in a text phrase. The phrase may be up to a thousand characters in length. It must use a switch statement to count (a) the number of spaces and tabs, (b) the number of commas and question marks, and (c) the number of colons and semicolons, in the phrase. It must then print back these counts, followed by the phrase itself, each on a separate line. Include a comment with your name and the date.

    Here is an example run of the program:

    $
    $ ./prog-switch
    Enter phrase:
    Q: Why do ducks have big flat feet?     A: To stamp out forest fires;   Q: Why do elephants have big flat feet?      A: To stamp out flaming ducks.
    
    27 spaces and tabs
    3 periods and question marks
    5 commas, colons and semicolons
    Q: Why do ducks have big flat feet?     A: To stamp out forest fires;   Q: Why do elephants have big flat feet?      A: To stamp out flaming ducks.
    $
    
        
     
  • [15 pts]

    Write a complete, correct C program that inputs personal information about a number of people and displays it back.

    The program must include a declaration for a struct that holds the personal information, and store each person's information in a struct. The struct must contain the following information:

    1. up to 1000 characters for the person's full name (including any spaces );
    2. the person's age, as an unsigned integer;
    3. the person's weight, as a floating-point number; and
    4. height, in feet and inches, as integers.

    Input format. Your program must receive two file names as command-line arguments. It must read the information from the first file, which is formatted as follows:

    • The first line contains an integer that indicates how many people are in the file.
    • Each person is represented by three lines:
      1. person's full name
      2. person's age
      3. person's weight, height

    Two sample data files are shown, and linked, below.

    Output format. Your program must also write the information back to an output text file, whose filename is the second argument on the command line. Each line of the output file must contain (a) a person's weight, formatted to two decimal places; (b) their calculated height in inches only; and finally (c) the person's name.


    presidents info
        6
    George Washington
    67
    175.0  6 2
    James K. Polk
    53
    174.0  5 8
    Abraham Lincoln
    56
    185.0  6 4
    William Howard Taft
    72
    332.0  6 0
    Franklin Delano Roosevelt
    63
    188.0  6 2
    John Fitzgerald Kennedy
    46
    182.0  6 1
    
       
    Mercury-7 astronauts info
        7
    John Herschel Glenn
    95
    163.0  5 9
    Virgil Ivan (Gus) Grissom
    40
    150.0 5 7
    Leroy Gordon (Gordo) Cooper, Jr.
    77
    155.0  5 8
    Alan Bartlett Shepard
    75
    170.0  5 11
    Walter Marty (Wally) Schirra, Jr.
    84
    185.0  5 10
    Donald Kent (Deke) Slayton
    69
    165.0  5 11
    Malcom Scott Carpenter
    88
    160.0  5 10