2430 : Introduction to Programming in C

Final exam and solution, and a couple of things

First of all, one of our students Seigi made us this community weblog for discussing things with the class:
http://128.32.250.45:8080/introtochomeworkhelp/
You can login with e-mail skarasaki@comcast.net and password seigi and post on it. You can use this place to communicate among you guys even after the end of the course.

Below is the final exam solution.
1. You have the code below:
int charlene = 7;
int gillian = 20;
int* edison;
int* shawn;

edison = &gillian;
shawn = edison;
// fill in column 1 at this point
*edison = 8;
*shawn += 10;
// fill in column 2 at this point
charlene = 15;
*edison = &charlene;
// fill in column 3 at this point
*edison -= gillian;
// fill in column 4 at this point
And below would be the answers:
1234
charlene771515
gillian201850000
edison5004500450045004
shawn5004500450045004
2. Fill in the blanks (read the exam for the question):
#include <stdio.h>

void Diva(int* y);

main()
{
	int x = 1;

	Diva(&x);

	printf("%d\n", x); // this prints out 5, not 1!
}

void Diva(int* y)
{
	*y = 5;
}
3. Deferencing is basically using the * on a pointer variable to access the value being pointed to by the pointer.
4. Pass-by-reference is basically passing a pointer into a function to let it have access to variables not inside that function.
5. Fill in the blanks:
#include <stdio.h>

int* Popstar();

main()
{
	int* jay;

	jay = Popstar();

	printf("Array created.\n");
}

int* Popstar()
{
	int* chow;

	chow = malloc(20); // array creation

	return chow;
}
6. Equivalent way of saying arr[3] = 99; is:
*(arr+3) = 99;
This was from the lecture from week 5 when I talked about how arrays are actually pointers. Therefore, any other answers are worth either 0 or 1 point only. Something close to this will give you 2 or 3.
7. What I am looking for, as the difference, is that with struct date {...}; you declare your variable like: struct date today; whereas for the typedef one, you declare your variable like: date today;. Some of you explained the difference of them technically, I gave you 3 points for that, as you get -2 for not mentioning the variable declaration style above. However, you really only needed to mention that to get a full 5 points.
8. You are to write a function that concatenates two strings but do not modify them. Notice that using strcat directly on the two strings passed in WILL modify the first string. Therefore we need to make a copy of the first string first, then concatenate the second one onto the new copy.
char* strcatnew(char* first, char* second) {
	char* temp;
	temp = (char*)malloc((strlen(first) + strlen(second)) * sizeof(char));
	strcpy(temp, first);
	strcat(temp, second);
	return temp;
}
9. A function that compares two animals.
bool anicmp(animal one, animal two) {
	if (strcmp(one.name, two.name) == 0 && one.gender == two.gender && one.age == two.age)
		return TRUE;
	else
		return FALSE;
}
Of course you can also just put that whole condition in the return statement and have a one line function. This makes it slightly clearer what's going on.