COMP1511 18s2

COMP1511 18s2 Final

Questions

Question 1

Your task is to calculate an individual's tax given their income. The tax is calculated using the following table:

0 - $18,200Nil
$18,201 – $37,00019c for each $1 over $18,200
$37,001 – $90,000$3,572 plus 32.5c for each $1 over $37,000
$90,001 – $180,000$20,797 plus 37c for each $1 over $90,000
$180,001 and over$54,097 plus 45c for each $1 over $180,000

Your task is to add code to this function:


double calculateTax(int income) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}

Add code so that calculateTax returns the person's payable tax.

Assumptions/Restrictions/Clarifications.

An odd number is not divisible by 2.

calculateTax should return a single double.

calculateTax will be given an integer as input.

calculateTax should not call scanf (or getchar or fgets).

calculateTax can assume the income will be positive

calculateTax can assume the income will be less than $2 billion.

calculateTax function should not print anything. It should not call printf.

When you think your program is working you can run some simple automated tests. Make sure you add in test cases of your own.

./test_calculateTax

When you are finished working on this exercise you must submit your work by running submit:

submit q1

Question 2

Write a C program q2.c which reads integers from standard input, line by line, until it reaches end-of-input.

It should then print the strictly positive integers, each on a separate line, in the reverse order they occurred.

Match the the example below EXACTLY.

dcc q2.c -o q2
./q2
Enter a number: 1
Enter a number: 4
Enter a number: 0
Enter a number: 5
Enter a number: -6
Enter a number: 2
Enter a number: -3
Enter a number: 9

9
2
5
4
1

Assumptions/Restrictions/Clarifications.

Your program must read until the end of-input. End of input is signalled on a Linux terminal by typing the Ctrl and d keys together.
This is what indicates in the above examples.

You can assume the input will only contain integers between negative 2 billion and positive 2 billion, one per line.

The number zero is not included as a strictly positive integer.

You can assume your input contains at least one integer.

You can assume your input contains no more than 10000 integers.

You are free to write this program in any way you wish: there is no specific function that you need to implement. Note that your program will need to have a main function.

When you are finished working on this exercise you must submit your work by running submit:

submit q2

Question 3

Complete the given function, which calculates the range of an array. The function takes in an array and its length as arguments. The range of the array is the difference between its largest value and smallest value.

Your task is to add code to this function:


int arrayRange(int array[], int arrayLength) {
    // PUT YOUR CODE HERE (you must change the next line!)
    return 42;
}
Example
The array [-5, 1, 2, 3, -6, 2, 5, 10] has a range of 16, because its largest element is 10 and its smallest element is -6. So the difference is 16.

Assumptions/Restrictions/Clarifications.

You can assume the input will contain at least one integer.

You can assume the array will only contain integers between -2000000000 and 2000000000.

When you are finished working on this exercise you must submit your work by running submit:

submit q3

Question 4

In your directory, you should find the file cardMatch.c

Your task is to add code to this function in cardMatch.c:

int cards_match(Card cardDeck[], int cardsLength, Card card) {

    // PUT YOUR CODE HERE (change the next line!)
    return 42;
}
Note cardMatch.c uses the following familiar data type:

typedef struct card {
    Color color;
    Value value;
    Suit suit;
} Card;

cards_match is given three arguments.

Add code to cards_match so that it returns the number of cards in cardDeck that matches with the given card.

Two cards are matching if at least one of their value, color, or suit are the same.

Furthermore, two cards are also considered matching if at least one of them has a value of ZERO.

When you are finished working on this exercise you must submit your work by running submit:

submit q4

Question 5

In your directory, you should find the file insertDescending.c.
Complete the following function that inserts a provided value into a given linked list, such that the resulting linked list remains in descending order. The provided linked list is already in descending order.

Your task is to add code to this function in insertDescending.c:

struct node *list_descending(struct node *head, int value) {

    // PUT YOUR CODE HERE (change the next line!)
    return 42;
}
Note list_descending.c uses the following data type:

struct node {
    struct node *next;
    int data;
};
list_descending is given two arguments.

When you are finished working on this exercise you must submit your work by running submit:

submit q5

Question 6

In your directory, you should find the file product_sum.c.
Complete the following function that calculates a pairwise sum on two linked lists. For example, the pairwise sum of [1,2,3,4,5,6] and [7,8,9] is 1*7 + 2*8 + 3*9.
The 4, 5, 6 from the first linked list is ignored as there is no corresponding term to multiply with in the second linked list.

Your task is to add code to this function in product_sum.c:

int productSum(struct node *head1, struct node *head2) {

    // PUT YOUR CODE HERE (change the next line!)
    return 42;
}
Note product_sum.c uses the following data type:

struct node {
    struct node *next;
    int data;
};

productSum is given two arguments.

When you are finished working on this exercise you must submit your work by running submit:

submit q6

Question 7

Write a program q7.c that takes the name of an input file as a command line argument, and analyses it. The file contains syntax similar to HTML. Elements are written with a start tag and an end tag, with the content in between. The tag is composed of the name of the element surrounded by angle brackets. The end tag has a slash after the opening angle bracket to distinguish it from the start tag. For example, the following represents a sentence wrapped in a p tag.
<p>
i am retarded
i have crippling depression
</p>
Tags can also be nested:
<autism>
i am retarded
<big>
i have crippling depression
</big>
nice!
</autism>
However, this is incorrect nesting:
<autism>
i am retarded
<big>
i have crippling depression
</autism>
nice!
</big>
Write code which finds errors in nesting and prints out the line number.

When you are finished working on this exercise you must submit your work by running submit:

submit q7

Question 8

Write a program q8.c that takes two positions on a chessboard (start point and end point) and prints out a sequence of moves that allows a knight to move from the start point to the end point. If there are multiple such paths, you must print all possible paths in alphabetical order.
dcc q8.c -o q8
./q8 d4 b3
d4 b3
./q8 d4 a1
d4 b3 a1
d4 c2 a1

When you are finished working on this exercise you must submit your work by running submit:

submit q8