CS 2213 Advanced Programming
Spring 2004 -- Exam 1
Directions: Fill in answers on the pages below.
Don't spend too much time on any one problem.
Even if you have forgotten some things, fake it as well as you can.
Questions about Unix basics:
- 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.)
- 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.
- Use the cat utility to append
file2.txt to the end of filel.txt
and save as file3.txt.
Questions about C input/output:
- 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):
4.31 3.6 8.27 1.23 7.9
- 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):
4.3, 3.6, 8.3, 1.2, 7.9
Questions about Random Number Generators:
Consider the following function for generating random numbers that was
discussed in class:
double rand() {
double a = 16807.0,
m = 2147483647.0;
double q;
seed = a*seed;
q = floor(seed/m);
seed = seed - q*m;
return(seed/m);
}
- What variable needs to be declared and initialized outside this function?
- 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:
- 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!)
Questions about separate compilation and OOP:
Consider the following short program:
#include <stdio.h>
int sqr(int x) {
return x*x;
}
int main() {
int sum = 0;
int primes[] = {2, 3, 5, 7, 11, 13, 17};
for (i = 0; i < 7; i++) sum += sqr(primes[i]);
printf("sum: \n", %i);
}
- 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.)
- Using "faked" OOP in C, what corresponds to a class?
What corresponds to a public function (that is, public method)?
What corresponds to a private function (that is, private method)?
What corresponds to a private data member?
Questions about pointers:
Consider the following program:
#include <stdio.h>
int main() {
int x, y, z, i;
int *xp = &x;
int *q;
int a[] = {10, 20, 30, 40};
*xp = 47;
y = *&x;
q = &a[2];
*q = 37;
*(a+3) = 16;
printf("%i\n", y);
for (i = 0; i < 4; i++) printf("%i ", a[i]);
printf("\n");
}
- Say very carefully what will be printed.
- Write the array element a[i] without using brackets.
- 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:
- 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.]
- This question is concerned with string buffers.
- Give either of the declarations of storage to use for a string -- what I called
a string buffer in class.
- 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.
- Print this string by any method.
Copyright © 2011, Neal R. Wagner. Permission is granted
to access, download, share, and distribute, as long as this notice remains.