1701/159.
101
ALBANY
Internal
CP
MASSEY UNIVERSITY
ALBANY CAMPUS
EXAMINATION FOR
159.101 PROGRAMMING FUNDAMENTALS
Semester One 2017
Time allowed: THREE (3) hours
ANSWER ALL QUESTIONS
Total: 55 marks
CALCULATORS ARE PERMITTED
All computer programs may be written in pencil
The exam paper will be made available on the Massey University Library website in
due course website.
The examination question paper is not to be removed from the exam room.
Page 1 of 5
1701/159.101
ALBANY
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 f);
char a[20];
int main() {
int i, j;
a[3] = 'H';
a[1] = 'M';
i = 1 + 2 * 3;
j = 3;
a[2] = 'S';
exam(j);
printf("%d %s %d\n", i, a, j);
}
void exam(int f) {
int j;
j = 11 % f;
printf("%d hello %d\n", f+3, j);
a[2] = '\0';
a[0] = 'K';
}
[10 marks]
2. (a) What is the difference between sscanf and fscanf?
[2 marks]
(b) Write the function prototype for the strcat function.
[2 marks]
(c) Give an example of a syntax error.
[1 marks]
(d) Give the decimal and binary values of the ASCII code
that represents the character ‘4’
[1 marks]
(e) Name a function in the library called stdlib.h
[1 marks]
Page 2 of 5
1701/159.101
ALBANY
Internal
3. Write a program that places a name instead of X in a message.
Example 1: the message is “Dear Mr X, how are you?” and the name is
“Baggins” so the final message will be “Dear Mr Baggins, how are you?”.
Example 2: the message is “Call a meeting with X” and the name is “Pippin
Took” so the final message will be “Call a meeting with Pippin Took”.
You must include the following function in your program:
void replaceX(char *big, char *small);
This function will find X in the big string and replace X with the small string.
When the function ends, the big string will be changed to be the final string.
You must use the following algorithm for your program:
Step 1 – read in the message (with X somewhere in the message)
Step 2 – read in the name
Step 3 – use the function to place the name into the message
Step 4 – display the final message
Do not check for invalid input. Assume that there is always X (and only one
X) somewhere in the message.
[10 marks]
4. (a) Write the decimal number 63 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 5 – 9 would be performed.
Show how to check your answer. Include all working.
[6 marks]
Page 3 of 5
1701/159.101
ALBANY
Internal
5. A phone company offers a monthly plan in which customers can select how
much data, call time and texts they want. This information is stored in a file
called Plans.txt and each line in this file uses the following format:
name;phone number;data;call minutes;texts
For example, the first few lines in the file could look like this:
May Wang;062491;100;300;200
Fen Singh;061825;300;50;300
Anne Smythe;068432;200;150;500
Write a program that reads the data from the file and calculates the cost of the
plan for each customer. The cost is calculated as follows:
Cost for data is $10 for 100 MB
Cost for calls is $2 for 50 minutes
Cost for texts is $3 for 100 texts
When your program is running the screen should look very similar to the
example below:
May Wang (062491) 100MB 300mins 200txt Plan costs $28
Fen Singh (061825) 300MB 50mins 300txt Plan costs $41
Anne Smythe (068432) 200MB 150mins 500txt Plan costs $41
Notes:
1. Do not check for invalid data.
2. Data will be provided in 100s (e.g. you will not get 167MB)
3. Calls will be provided in 50s (e.g. you will not get 232 minutes)
4. Texts will be provided in 100s (e.g. you will not get 518 texts)
[10 marks]
Page 4 of 5
1701/159.101
ALBANY
Internal
6. The following program reads in a number and then searches through an array
for a number that is closest to that number. There are ten (10) errors in this
program. Write down each error and how to correct it. Do not rewrite the
whole program.
#include <stdio.h>
#include <stdlib.h>
int i, num, a[50], search, temp, diff1, diff2;
int main() {
num = 6;
/* read values into the array */
for (i = 0; i < num; i--) {
printf("Enter a value: ");
scanf("%d", &a[i]);
}
/* sort the array */
for (pass = 0; pass < num; pass++) {
for (i = 0; i < num - 1; i++) {
if (a > a[i+1]) {
temp = a[i];
a[i+1] = pass;
}
}
}
/* ask for a number */
printf("Enter a search integer: ");
scanf("%d", search);
/* find the closest number in the array */
i = 0;
while ((search < a[i]) || (i < num)) {
i++;
}
if (i == num) {
printf("Closest value to %d is: %d\n", search, a[num]);
} else {
diff1 = abs(search - a[i-1]);
diff2 = abs(search - a[i]);
if (diff1 < diff2) {
printf("Closest value to %d is: %d\n", search, a[i-1]);
} else {
printf("Closest value to %d is: %d\n", search, a[i+1]);
}
}
}
[10 marks]
++++++++
Page 5 of 5