159.101 Computer Science Fundamentals - Massey - Exam - S1 2016
159.101 Computer Science Fundamentals - Massey - Exam - S1 2016
159.101 Computer Science Fundamentals - Massey - Exam - S1 2016
101 CP
ALB
Internal
MASSEY UNIVERSITY
ALBANY CAMPUS
EXAMINATION FOR
159.101 PROGRAMMING FUNDAMENTALS
TOTAL: 55 MARKS
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>
int m, y;
int main() {
m = 34;
y = 99;
while (y != 2) {
y = m % 3;
m--;
exam(m, y);
}
printf("-->%d", m);
}
Page 2 of 5
1601/159.101 CP
ALB
Internal
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]
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]
[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:
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 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);
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