Name: ________________________________________________________
Umail Address: __________________________________@ umail.ucsb.edu
Circle Lab section: 8AM 10AM 11AM noon
Link to Printer Friendly PDF Version
Please write your name only on this page. That allows me to grade your exams without knowing whose exam I am grading.
This exam is closed book, closed notes, closed mouth, cell phone off,
except for:
There are 100 points worth of questions on the exam, and you have 50 minutes to complete the exam.
A hint for allocating your time:
countOccurences(a,n)
, or maxValue(a,n)
—then include definitions of those functions also.isPositive(i)
—then include definitions of those functions also.(10 pts) Write the definition of a C function that takes two parameters—an array of integers, and a second integer indicating the size of the array.
The function should be called averageOfIntArray. The function should return the average of all the integers in the array.
If you use any helper functions—for example, if you call a function sumOfArray(a,n)
—then include definitions of those functions also.
Write ONLY the function definition—for this question, I do NOT want a complete C program, so do NOT include any extraneous stuff such as #include <stdio.h> or a main function. (See solution and compare it with this incorrect solution)
Hints:
struct Point {
double x;
double y;
};
Write the definition of a function that matches this prototype:int countPointsCloserThan(struct Point p,
struct Point *others,
double distance, int numPts);
others
that are closer to the point p
than the value of the parameter distance
(i.e. the distance between p and the point from others is strictly less than distance
. The parameter numPts
indicates the size of the array others
) double distanceBetween(struct Point p1, struct Point p2);
distanceBewteen
—and will not receive any credit for doing so (not even partial credit.)
void doIt1()
{
int a[] = {10,20,30,40};
printf("%d\n",a[3]);
}
void doIt2()
{
int a[5] = {0};
printf("%d\n",a[5]);
}
void doIt3()
{
int a[5] = {1};
printf("%d\n",a[4]);
}
void doIt4()
{
int a[5];
int i;
for (i=0; i<5; i++)
a[i] = 5-i;
printf("%d\n",a[2]);
}
(25 pts) Together with this exam, there is a program (on a separate handout).
Assuming each of the expressions below appeared in this program, indicate the type they would have, or write error if the expression is not valid, e.g.
The first few are done for you as an example.
Hints--for full credit:
See solution
Expression | Type | Expression | Type | |
a | int | f->radius | ||
&a | int * | f->center.x | ||
*a | error | f->center->y | ||
*b | int | &g | ||
e | struct Circle | *h | ||
b | &(h->x) | |||
& b | *(h.y) | |||
c | i.m | |||
*c | j.d | |||
d | i->y | |||
&d | j->m | |||
*e | (*j).m | |||
e.x | argc | |||
e.radius | argv[0] | |||
f.y | argv[0][0] |
rwxr-xr-x
rw-r--r--
. rw-r--r--
, can the owner of this file run this file as a program? rwxr--r--
, can users other than the owner of the file make changes to it?foobar
to have the permission string rwxr-xr-x
foobar
?Total points: ?
Program for question about types
// types.c Code for exam question, 11/15/2009 // P. Conrad for CS16, 09F, UCSB #include <stdio.h> struct Point { double x; double y; }; struct Date { int d; int m; int y; }; struct Circle { struct Point center; double radius; }; int main(int argc, char *argv[]) { int a; int *b; double c; double *d; struct Circle e; struct Circle *f; struct Point g; struct Point *h; struct Date i; struct Date *j; // Program does no useful work // It is just the basis of a homework assignment about types
// Pretend there is useful code here, and then // answer questions about the types of various expressions // as if they appeared right here. return 0; }