C
Programming Test Exercises:
1. What does this print?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char* word = (char *) malloc(sizeof(char) * 30);
strcpy(word,"good one 123 two 456");
char wordarr[20] = "working memory";
wordarr[4] = '\0';
char suba[4];
suba[3] = '\0';
strncpy(suba,word+5,3);
suba[1] = toupper(suba[1]);
int x;
int y;
char string1[30];
char * string2 = string1;
char * string3 ;
string3 = (char *) malloc(sizeof(char) * 30);
sscanf(word,"%s%s%i%s%i",string1,string2,&x,string3,&y);
char builtBySprintf[100];
sprintf(builtBySprintf,"the first number was %i and the second number was %i\n",x,y);
printf("the value of wordarr is: %s and the length is %lu\n", wordarr, strlen(wordarr));
printf("the value of word is: %s\n",word);
printf("the value of suba is: %s\n",suba);
printf("the value of string1 is: %s \n",string1);
printf("the value of string2 is: %s \n",string2);
printf("the value of string3 is: %s \n",string3);
printf("the value of x is %i and y is: %i \n",x,y);
printf("the value of builtBySprintf is: %s \n",builtBySprintf);
printf("tricky: the value of wordarr after the null at 4 is: %s\n",wordarr+5);
}
· the value of wordarr is: _________ and the length is _______
· the value of word is: ___________________________________________
· the value of suba is: _________
· the value of string1 is: _________
· the value of string2 is: _________
· the value of string3 is: _________
· the value of x is _________ and y is: _________
· the value of builtBySprintf is: ___________________________________________
· tricky: the value of wordarr after the null at _________ is: ____________________
2. What does this print?
#include <stdio.h>
struct record {
int v;
int num[3];
};
int mystery(int * numbIn, int vIn, struct record * struIn );
int main(void){
int *p , *q ;
struct record spreadsheet[2];
p = &(spreadsheet[0].v);
*p = 30;
q = p;
spreadsheet[0].num[0] = *q;
p = spreadsheet[0].num;
p[2] = 22;
spreadsheet[1].num[2] = 6;
*q = 10;
spreadsheet[0].v = 11;
p++;
*(p+3) = 16;
p = &spreadsheet[0].num[1];
*p = 10;
printf("the size of a spreadsheet[0].num is %lu\n",sizeof(spreadsheet[0].num));
mystery (spreadsheet[0].num, spreadsheet[1].v, &(spreadsheet[1]));
spreadsheet[1].v = mystery (spreadsheet[0].num, spreadsheet[0].v, &(spreadsheet[1]));
//What are the last spreadsheet.values of spreadsheet.v and spreadsheet.num ?
printf("last spreadsheet.value of *p is %i \n",*p);
printf("last spreadsheet.value of *q is %i \n",*q);
printf("last spreadsheet.value of spreadsheet[0].v is %i \n",spreadsheet[0].v);
printf("last spreadsheet.value of spreadsheet[1].v is %i \n",spreadsheet[1].v);
int c,countRow;
for (countRow = 0; countRow < 2; countRow++){
for (c = 0; c < 3; c++){
printf("last spreadsheet.value of spreadsheet[%i].num %i is %i\n",
countRow,c,spreadsheet[countRow].num[c]);
}
}
}
int mystery(int * numbIn, int vIn, struct record * struIn ) {
printf("the size of a numbIn is %lu. Why?\n",sizeof(numbIn));
static int z = 0;
int * pIn = numbIn;
int y = 0;
y = y + 150;
z = z + 10 + y;
pIn[1] = 100;
vIn = vIn + 60;
struIn->v = y;
struIn->num[1] = 304;
return (z);
}
· the size of a spreadsheet[0].num is _________
· the size of a numbIn is _________. Why?
· the size of a numbIn is _________. Why?
· last spreadsheet.value of *p is _________
· last spreadsheet.value of *q is _________
· last spreadsheet.value of spreadsheet[0].v is _________
· last spreadsheet.value of spreadsheet[1].v is _________
· last spreadsheet.value of spreadsheet[0].num 0 is _________
· last spreadsheet.value of spreadsheet[0].num 1 is _________
· last spreadsheet.value of spreadsheet[0].num 2 is _________
· last spreadsheet.value of spreadsheet[1].num 0 is _________
· last spreadsheet.value of spreadsheet[1].num 1 is _________
· last spreadsheet.value of spreadsheet[1].num 2 is _________
3. Functions pass by reference and local, both array and integer and random.
Write a small program with 2 functions:
main: Create an array of 5 dice, each with a random number between 1 and 6. Also create one more integer to hold the magic number, which is also a random number between 1 and 6. Call the game function, passing it the magic number and the dice and receive back the total. Pass both the magic number and the dice array by reference. Then print the magic number, and all the dice and the total.
game: input parameters: dice array, magic number
output : total score
process: Calculate the total score by adding all the dice, but if a die equals the magic number, change the die to die +1 before adding it to the score, and add 1 to the magic number. If the magic number goes over 6, it is okay.
4. Read and print formatting and String comparison and decisions:
Write a small program that reads in 2 integers, 1 decimal and 2 words and 1 date in the form YY-MM-DD. The integers should be read into 2 cells of an integer array. 1 word should be created as char word[30] and the other should be created as char * word2. Print what the user entered to the screen. Then print the decimal as currency with 2 decimal places. Then print whether the words matched.
5. Arrays – sizes and copies and Dynamic Arrays – creating and using
Ask the user for the size of an array. Then create 2 integer arrays of the same size – one should be static and the other dynamic. (Dynamic means it is created in memory without an array name.) Fill them both with random numbers. Then look through them both to see whether they have the same contents. Print that the result of your test, stating whether the contents are the same.
6. Questions about string functions:
a) What is the difference between char * and char [ ].
b) When do you need to use free?
c) Given the variables zip_code_first_five, zip_code_last_four and city and state, write a sprintf command that will put these variable values into the variable address_line_2 in city, state xxxxx-xxxx (with the last part being zip_code_first_five - zip_code_last_four ) format.
d) Given the string word containing "1,good,3", write a sscanf command that will extract the 1, good and 3 from the string word into two integer variables x and y and one character array myword.
e) Write a typedef statement that creates a new variable type Stringy that is actually just a pointer to a character.
7. Structure – declare and use as a function parameter
Main:
· Make a structure of pets that will hold a pet name, type and age. It should include the name of the pet, the type of pet (such as dog or cat) and the age of the pet. A pet type can only be 10 characters long but its name can be 20 characters long.
· Ask the user how many pets they have, and then create an array of the number of pets they mentioned.
· Ask the user for the name, type and age of each pet.
· Create one more structure variable of a single pet.
· Call the findBestPet function and then print the single pet variable values.
findBestPet
· takes in the array of pets and a pointer to the single pet. It should ONLY read through all the pets and find the oldest. Copy the values of the oldest pet into the single pet structure variable.
8. Comparison:
· Which paradigms do C, Java and Prolog each represent?
· How does C handle memory differently than Java?
· How is a class in Java different from a structure in C?
Prolog
Programming Exercises:
P1. Write 2 prolog facts:
· One fact to represent mr jones lives in a green house with a cat
· Another fact to represent that mr bozo lives in a yellow house with a dog.
P2. Given these facts, write the requested rules:
female(amy).
female(mary).
female(lucy).
female(sally).
male(tim).
male(jim).
male(frank).
parent(jim,mary).
parent(jim,amy).
parent(lucy,mary).
parent(sally,amy).
parent(sally,frank).
Note that the parent rule is meant to read the first parm is a parent of the second, so jim is a parent of mary.
Write a rule for sibling(Sib1,Sib2) to be true when a male or female has the same parent.
Write a rule for daughter(Parent,Child) to be true when the child is a female.
Write a rule for mother(X) to be true only when the mother is both a parent and female.
P3. Write a prolog query given facts and rules.
Given this Knowledgebase
point(a,2,3).
point(b,1,3).
point(c,5,3).
point(d,4,2).
point(e,7,3).
point(f,2,9).
point(g,5,9).
point(h,4,8).
point(i,9,2).
point(j,9,8).
magicLetterPoints(A,B,C,D):-
point(A,Ax,Ay),
point(B,Bx,By),
point(C,Cx,Cy),
point(D,Dx,Dy),
Ay = By,
Ax = Cx,
Ay < Cy,
Ax < Bx,
Dy = Cy,
Bx = Dx.
Write a query that will print all the magicLetterPoints that are found in the knowledgebase.
What letter does it create?
How many answers are there to this query in the given knowledgebase?