Computer Science homework help. Variable Declarations
(2 POINTS EACH)
1. A single character named c
2. Define TRUE with an appropriate value
3. Define FALSE with an appropriate value
4. An array of 10 real numbers named w
5. An array of 5 integers named z
Special Symbols
(2 POINTS EACH)
1. assignment operator
2. a real number in a printf statement formatted to have 2 digits followed by 3 decimal places (ie XX.XXX)
3. indicate the address of a variable
4. indicate a variable is a pointer
5. combine a compound condition with “OR”
Output to Code Segments
(2 POINTS EACH)
• If the there is no output, write “No Output” (Do NOT leave it blank!)
• If the loop is infinite, write at least the first 3 items in the output and then write “Infinite”.
1. x = 3;
y = 5;
while (x <= y) {
y = y – 2;
x = y – x + 1;
}
printf(“x is %d, y is %d\n”, x, y);
2. if (x = 0) {
printf(“Zero\n”);
}
else {
printf(“Not Zero\n”);
}
• x = -1
• x = 0
• x = 1
3. for (i = 5; i > 0; i−−)
printf(“%d ”, i);
4. if ((x >= 1)||(y <= 10))
printf(“OK\n”);
else
printf(“WRONG\n”);
• x = 0, y = 0
• x = 1, y = 11
• x = 11, y = 0
• x = 0, y = 11
5. for (i = 1; i <= 5; i++);
printf(“%d ”, i);
Passing Parameters
(5 POINTS)
1. Write the output of the following program and explain answer.
#include <stdio.h>
void stuff (int *, int);
int main (void) {
int x = 4, y = 5;
stuff(&x,y);
printf(‘‘%d %d\n”,x,y);
}
void stuff (int *a, int b) {
*a = 3; b = 2;
}
Type Definitions
(5 POINTS EACH)
1. define a stack type that points to a “stacknode” structure for a stack that will manipulate real numbers
2. define a tarver type that consists of two parts (“part1” and “part2”) which contain an array of 5 integers and
a single real number, respectively.
3. Given the following type definition, draw a picture to show how X and Y are physically represented.
typedef struct mynode {
int p1;
float p2;
char p3;
} *mytype;
mytype X;
struct mynode Y;
Stacks
(5 POINTS EACH)
Given the following stack. Draw a picture to reflect the final result and then explain the steps necessary to complete
the process.
Note: Each question applies to the stack shown below NOT the result of the previous question.
top
4
8
6
1. Push a 2 on the stack.
2. Pop an item off the stack.
Queues
(5 POINTS EACH)
Given the following queue. Draw a picture to reflect the final result and then explain the steps necessary to complete
the process.
Note: Each question applies to the queue shown below NOT the result of the previous question.
9 3
front back
5
1. Add a 7 to the queue.
2. Fetch an item from the queue.
Binary Trees
(5 POINTS EACH)
1. Using the Add function given below, draw a binary tree and put the input 10 7 13 9 11 5 15 8 9 12 10 into it.
void Add(binarytree *T,int x){
if (is_empty(*T)){
*T = (binarytree)malloc(sizeof(struct treenode));
(*T)->data = x;
(*T)->left = NULL;
(*T)->right = NULL;
}
else
if (x < (*T)->data)
Add(&(*T)->left,x);
else
Add(&(*T)->right,x);
}
2. Use the following code to list the contents of the binary tree.
void print_tree(binarytree T) {
if (!is_empty(T)) {
print_tree(T->left);
printf(“%d\n”, T->data);
print_tree(T->right);
}
}
A
B C
D E F G
H I J K L
M N
O
3. Given the following tree, draw a picture to show what the tree would look like after each specified item is
deleted. Note: All deletions apply to the original tree, not the result of the previous deletion.
7
10
12
11
3 6
5
4 15
17
16
(a) Delete the 11.
(b) Delete the 6.
(c) Delete the 10.
(d) Delete the 15.