Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1601/159.

101 CP
ALB
Internal

MASSEY UNIVERSITY
ALBANY CAMPUS

EXAMINATION FOR
159.101 PROGRAMMING FUNDAMENTALS

Semester One 2016

Time allowed: THREE (3) hours

ANSWER ALL QUESTIONS

TOTAL: 55 MARKS

Calculators are permitted, with no restrictions on type of calculator used.

All computer programs may be written in pencil

Page 1 of 5
1601/159.101 CP
ALB
Internal

1. Write down exactly what the following program will display on the screen.
Note that this is a correct program and it will display something on the screen.
It is a good idea to do a walkthrough and show all the variables.

#include <stdio.h>

void exam(int a, int b);

int m, y;

int main() {
m = 34;
y = 99;
while (y != 2) {
y = m % 3;
m--;
exam(m, y);
}
printf("-->%d", m);
}

void exam(int a, int b) {


int c;
c = a + 35;
printf("%d*%d*%c!\n", c, b, c);
}
[10 marks]

2. (a) What is the difference between exit and break?


[2 marks]

(b) Given the function prototype:


bool nameless(int count, char *z, bool which);
(i) Write down all possible values that it can return.
(ii) How many parameters does it have?
[2 marks]

(c) What do programmers use to shorten and simplify binary numbers?


[1 marks]

(d) Which of the following are reserved words in C?


else, prompt, string, while, integer, file
[1 marks]

(e) Name two of the functions in the library called stdio.h


[1 marks]

Page 2 of 5
1601/159.101 CP
ALB
Internal

3. (a) Assume that the strcmp function is not available.


Write your own C function which has the function prototype:

int stringcompare(char* a, char* b)

This function must work in a similar manner to the way that strcmp works.
This means that it returns a negative result if a is alphabetically before b,
it returns zero if a is identical to b and it returns a positive value if a is
alphabetically after b.

[5 marks]

(b) Assume that the strcpy function is not available.


Write your own C function which has the function prototype:

void stringcopy(char* a, char* b)

This function must work in a similar manner to the way that strcpy works.
This means that string a is changed to be exactly the same as string b.

[5 marks]

4. (a) Write the decimal number 57 in binary and hexadecimal.

[2 marks]

(b) Assume a computer that uses twos complement arithmetic with a 6-bit
word length. Show how the calculation 3 – 7 would be performed.
Show how to check your answer. Include all working.

[6 marks]

Page 3 of 5
1601/159.101 CP
ALB
Internal

5. A government task force is experimenting with a new system for voting for a
president for New Zealand. There will be two candidates standing for
president and each region of New Zealand will vote. There will be a file called
President.txt that contains the results from the voting in each region.
Each line in the file will use the format:

region;candidate1;votes1;candidate2;votes2

For example, the first few lines in the file could look like this:
Canterbury;Winston Peters;951;Mad Butcher;1187
Auckland;Mad Butcher;4382;Winston Peters;3716
Bay of Plenty;Winston Peters;840;Mad Butcher;472

Write a program that reads the data from the file and lists which candidate
won in each region and also lists the total votes for each candidate. When
your program is running the screen should look very similar to the example
below:

Mad Butcher wins in Canterbury


Mad Butcher wins in Auckland
Winston Peters wins in Bay of Plenty

Winston Peters has a total of 5507 votes


Mad Butcher has a total of 6041 votes

Important notes about the program:

1) The exact number and names of the regions are unknown.

2) There are always exactly two candidates.

3) The program will be used each time there is a presidential election and the
candidate names will be different for each election.

4) The data is not sorted in any way – the regions can be in any order and the
candidates can be in any order on a line.

5) Assume that all data in the file has been entered correctly.

[10 marks]

Page 4 of 5
1601/159.101 CP
ALB
Internal

6. The following program reads in a year and the day of the week for the first of
January in that year. For example, 01 January 2016 was on a Friday. Then it
reads in a date and calculates the day of the week for that date. For example,
25 April 2016 was on a Monday. Assume that all data is entered correctly and
remember that April, June, September and November have 30 days.

The following numeric code is used for the days of the week:
0=Monday, 1=Tuesday, 2=Weds, 3=Thursday, 4=Friday, 5=Sat, 6=Sunday

There are ten (10) errors in the program. Write down each error and how to
correct it. Do not rewrite the whole program.

#include <stdio.h>

int firstday, finalday, totaldays, monthdays, year, month, day, i;

int main() {
printf("Enter the year, e.g. 2016 ");
scanf("%d", currentyear);
printf("Enter the day for 01 January in %d.\n", currentyear);
printf("0=Mon, 1=Tues, 2=Weds, 3=Thurs, 4=Fri, 5=Sat, 6=Sun\n");
scanf("%d", &firstday);

printf("Enter a date in the same year, e.g. 15/8/2016 ");


scanf("%d/%d", &day, &month, &year);
month = month - 1; // convert to index

i = 0;
totaldays = 0;
while (i != month) {
totaldays = totaldays + monthdays[i];
}
totaldays = totaldays + day - 1;
finalday = (firstday + totaldays) % 7;
printf("0=Mon, 1=Tues, 2=Weds, 3=Thurs, 4=Fri, 5=Sat, 6=Sun\n");
printf("The day of the week for this date is %d\n");
}

void loadarray(int y) {
int m;
for (m = 0; m > 12; m++) {
if ((m == 3) || (m == 5) || (m == 8) || (m == 10)) {
monthdays[m] = 30;
} else {
monthdays[m] = 31;
}
}
if ((y % 400 == 0) || (y % 4 == 0) && (y % 100 != 0)) {
monthdays[1] = 29;
}
}
[10 marks]
++++++++

Page 5 of 5

You might also like