CS 2213/1, Spring 2005, Exam 1      
Exam 1 Answers
Last Name:                            First Name:                  


Questions about Unix basics:

  1. (3) Give a Unix command to take a file named rec5.txt in the current directory and make a copy of it under the name rec5_back.txt in the same directory. (There are several methods.)
    cp rec5.txt rec5_back.txt or
    cat rec5.txt > rec5_back.txt

  2. (3) Give a Unix command that will execute a program named prog1 in the current directory, taking its input (the standard input) from a file named data.in (in the current directory) and sending its output (the standard output) to the terminal.
    prog1 < data.in or
    ./prog1 < data.in

  3. (4) Use the cat utility to append file2.txt to the end of filel.txt and save as file3.txt.
    cat file1.txt file2.txt > file3.txt or
    cat file2.txt >> file1.txt; cat file1.txt > file3.txt


Questions about C input/output:

  1. (6) Use scanf and a loop to read the following 5 numbers as values for an array declared double a[5] (you must use a loop): Ans: see next part.

  2. (6) Use printf and a loop to print the above 5 numbers that are stored in the array declared double a[5] in exactly the following form (you must use a loop, and the output must be exactly as shown, including a comma after each number except the last):

Questions about Random Number Generators: Consider the following function for generating random numbers that was discussed in class:

  1. (3) What variable needs to be declared and initialized outside this function?

  2. (7) Give a formula involving a call to this function that will produce a random int between 1 and 6 inclusive (for example, to simulate the roll of a die).


Question about copy programs:

  1. (10) Write a short program segment in C that will copy the standard input to the standard output, and will transform any upper-case letter to lower-case, making no other changes. (It should copy to end-of-file. As an example, the input Joe is 4 years old on Feb 29! should be copied asjoe is 4 years old on feb 29!)
First version Second versionThird version
#include <stdio.h>
#include <ctype.h>
int main(void) {
   char ch;
   while ((ch = getchar()) != EOF)
      putchar(tolower(ch));
}
#include <stdio.h>

int main(void) {
   char ch;
   while ((ch = getchar()) != EOF) {
      if (ch >= 'A' && ch <= 'Z')
         ch = ch -'A' + 'a';
      putchar(ch);
   }
}
#include <stdio.h> /* by J. Edwards */
#include <ctype.h>
int main(void) {
   char buff[99];
   char ch;
   int i;
   for (i = 0; (ch = getchar()) != EOF; i++)
      if (i < 98) buff[i] = tolower(ch);
      else break;
   buff[i] = '\0';
   printf("%s", buff);
}
% cc -o p8 p8.c
% p8
Joe is 4 years old on Feb 29!   (Return, CTRL-D)
joe is 4 years old on feb 29!


Questions about separate compilation and OOP: Consider the following short program:

Program First File:
sqr_main.c
Second File:
sqr.c
Header File:
sqr.h
#include <stdio.h>
int sqr(int x) {
   return x*x;
}
int main() {
  int i, sum = 0;
  int primes[] =
      {2,  3,  5, 7,
       11, 13, 17};
  for (i = 0; i < 7; i++)
      sum += sqr(primes[i]);
  printf("sum: %i\n", sum);
}
#include <stdio.h>
#include "sqr.h"
int main() {
  int i, sum = 0;
  int primes[] =
      {2,  3,  5, 7,
       11, 13, 17};
  for (i = 0; i < 7; i++)
      sum += sqr(primes[i]);
  printf("sum: %i\n", sum);
}
#include "sqr.h"
int sqr(int x) {
   return x*x;
}
int sqr(int );
OutputOutput
sum: 666
sum: 666

  1. (10) At the right above, rewrite the program so that it uses a separate file for the function sqr and uses a separate header file. (Follow the style that you have been taught in class. Clearly give the contents of the three files using names of your choosing.)

  2. (8) Using "faked" OOP in C, what corresponds to a class? A separate file.

    What corresponds to a public function (that is, public method)? A normal function definition (not declared "static").

    What corresponds to a private function (that is, private method)? A function declared "static".

    What corresponds to a private data member? A regulare variable declaration (a public variable is declared "extern").

    (See the first table at Object-oriented programming in C


Questions about pointers:

Consider the following program:

  1. (6) Say very carefully what will be printed.

  2. (3) Write the array element a[i] without using brackets. *(a+i)

  3. (6) Draw a diagram showing what each of the following pointers is pointing to at the end of the program: xp, q, and a+3.


Questions about strings:

  1. (10) Write a short program that uses the increment operator ++ and no square brackets to count the number of characters in the string char s[] = "How long am I?";. [If you are unable to fulfill all the conditions of the question, write any sort of program for partial credit, but don't use the strlen function.]

    First version Second versionThird version
    #include <stdio.h>
    
    int main() {
       char s[] = "How long am I?";
       char *p = s;
       int i = 0;;
       while (*p++)  /* or (*p++ != '\0') */
          i++;
       printf("Length: %i\n", i);
    }
    #include <stdio.h>
    
    int main() {
       char s[] = "How long am I?";
       int i = 0;
       while (*(s + i))
          i++;
       printf("Length: %i\n", i);
    }
    #include <stdio.h>
    int stringlen(char *);
    
    int main() {
       char s[] = "How long am I?";
       printf("Length: %i\n", stringlen(s));
    }
    
    int stringlen(char *s) {
       int i = 0;
       while (*s++)
          i++;
       return i;
    }
    
    Common Output
    % cc -o p14 p14.c
    % p14
    Length: 14
    

    Methods that DO NOT WORK
    First FAILURE Second FAILURE
    #include <stdio.h>
    int main() {
       char s[] = "How long am I?";
       int i = 0;
       while (*s++)
          i++;
       printf("Length: %i\n", i);
    }
    
    #include <stdio.h>
    int main() {
       char s[] = "How long am I?";
       int i = 0;
       while (*(s + i++)) /* or (*(s + i++) != '\0') */
          ;
       printf("Length: %i\n", i);
    }
    
    % cc -o p14_3 p14_3.c
    "p14_3.c", line 5: operands must 
       have scalar type: op "++"
    
    % p14_2
    Length: 15
    

  2. (15) This question is concerned with string buffers.

    1. Give either of the declarations of storage to use for a string -- what I called a string buffer in class.

      Standard mistake: char *s; as answer. This does not give any storage for characters, but only creates storage for an uninitialized pointer.

    2. Give C code that will use getchar() to read characters until encountering a newline character, and will put these characters into the string buffer above, creating a string.

    3. Print this string by any method.

      See Strings in buffers.
      Note: It is very important to check that you do not go beyond the bounds of the buffer. For simplicity, I left this off in the code on the link above, and I didn't expect it on the exam, but I should have emphaisized it more. The third version of Problem 8 above shows this type of check.


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