| Midterm Exam | Name ______answer key________________ |
CSC 7000-002 Algorithms & Programming
Fall 1999, Mendel 115
Dr. David Matuszek
if then else while do repeat until signed
top_job register _important_ id#5
$limit Default
i = 5; j = i++; |
i=6, j=5 |
i = 5; j = --i; |
4 |
i = 0; j = 10; |
i=10, j=9 |
int a[10]; |
a[2]=-2 |
i = rand() % 10 / 100; |
i=0 |
i = 15; |
15 17 f |
char *p; |
ifmmp |
i = 2; |
twoonezero |
i = 10; j = 3; |
k=1 |
i = 1; j = 2; |
0 |
do...while loop controls
only a single statement.a & b |
bitwise AND of a and b |
& b |
address of b |
a && b |
logical AND (short circuit) of a and b |
a % b |
mod, remainder of a/b |
a += b |
add b to a |
a != b |
a not equal b |
a >> b |
right shift a, b bits |
a . b |
field b of record a |
a -> b |
field b of record *a |
void write_message (char *filename, char *message) { /* Declare a FILE variable to use in this function */FILE *f;/* Open the file for writing */f = fopen (filename, "w");/* Write the message onto the file */fprintf (f, "%s\n", message);/* Close the file and return */fclose (f);}
main actually has arguments, but most of our programs so far
have ignored those arguments. Write a prototype for main that shows what the
arguments actually are.int main (int argc, char *argv[]);
\n,
followed by a newline. That is, it should print the three characters \, n,
and newline. printf ("\\n\n");
a[100] to all zeros.for (i = 0; i < 100; i++) a[i] = 0;
person to hold a person's name, age, and phone
number. struct person {
char *name;
int age;
long int phone;
};
(b) Now declare two variables of the type you have just defined.
struct person jack, jill;
(c) Write code to fill in all the fields of one of your variables with reasonable values.
jack.name = (char *) malloc (20);
strcpy (jack.name, "Jack");
jack.age = 17;
jack.phone = 5551212L;
fscanf(), that can
you use to read in an entire line as a string?fgets(), gets()
p is of type char*, what does the expression p
== "hello" actually test?Whether p has been set to point to the exact storage location used to hold the literal string "hello". If p points anywhere else, even to another string "hello", the test returns false.
string (for
example, string s,t;). Write the one statement that defines string
to mean the same thing as char*. typedef char *string;
Short answer:
Declaring says you have such a variable, and what its type is; defining does this, but also allocates storage for the variable. Note: neither of these has anything to do with assigning a value to the variable.
Going outside the bounds of an array; following a null or illegal pointer;
Using an array index that's negative or too large.
Putting a string in a storage area that's too small for it .
Forgetting the & in a scanf() function call (or in any function call where a pointer is expected).
Putting in a * or & where it doesn't belong.
Following a pointer that's null, not properly initialized, or freed too soon.
To help you understand what the code is doing, particularly after you haven't looked at it for a while.
It's a good habit to use
ias the index of a loop (jandkfor nested loops). Loop indices are typically not used elsewhere, so they are "throwaway" variables, and the consistent naming makes it easier to remember what to use for an index inside the loop.
Numbers appearing in your program that could just as easily be replaced by named, defined constants. You should avoid them in order to make your program easier to understand and easier to change.