0% found this document useful (0 votes)
20 views

159.101 Computer Science Fundamentals - Massey - Exam - S1 2015

159.101 Computer Science Fundamentals - Massey - Exam - S1 2015 Massey University, NZ, 159.101 , 159101

Uploaded by

Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

159.101 Computer Science Fundamentals - Massey - Exam - S1 2015

159.101 Computer Science Fundamentals - Massey - Exam - S1 2015 Massey University, NZ, 159.101 , 159101

Uploaded by

Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

1501/159.

101
ALBANY
Internal
CP

MASSEY UNIVERSITY
ALBANY CAMPUS

EXAMINATION FOR
159.101 PROGRAMMING FUNDAMENTALS

Semester One 2015

Time allowed: THREE (3) hours

ANSWER ALL QUESTIONS

Total: 55 marks

CALCULATORS ARE PERMITTED

All computer programs may be written in pencil

Page 1 of 5
1501/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 blue, char yellow);

int row, letter, count;

int main(){
letter = 'a';
for (row = 0; row < 3; row++ ) {
count = 0;
while (count < row) {
printf("?");
count++;
}
exam(row, letter);
}
}

void exam(int blue, char yellow) {


int i;
for (i = blue; i < 6 - blue; i++) {
printf("%c", yellow + i);
}
printf("\n");
}
[10 marks]

2. (a) Write the prototype for a function that has two characters passed
to it and returns the difference between their ASCII codes.
[2 marks]

(b) A program is saved in a file called program.c


Write the fopen statement that is required for the program
to read itself.
[1 marks]

(c) Give the decimal and binary values of the ASCII code that
represents the character ‘3’
[2 marks]

Page 2 of 5
1501/159.101
ALBANY
Internal

(d) If a user entered on the keyboard:


hello world
what would be stored in the variable char t[50];
if the data was entered using gets(t);
[1 marks]

(e) If a user entered on the keyboard:


hello world
what would be stored in the variable char t[50];
if the data was entered using scanf("%s", t);
[1 marks]

3. A palindrome is a word that reads the same forwards and backwards.

Some examples of palindromes:


Deed
Level
Madam

Write a C program that reads in a word and decides if the word is a


palindrome or not. When your program is running the screen should
look something like this:

Enter a word: Elephant


Elephant is NOT a palindrome.

Note: it does not matter if letters are upper case or lower case. For example,
levEL should still be recognised as a palindrome.

[10 marks]

4. (a) Write the decimal number 59 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 4 – 8 would be performed.
Show how to check your answer. Include all working.
[6 marks]

Page 3 of 5
1501/159.101
ALBANY
Internal

5. A university has a CSV file called student.csv that contains data about the
students. Each line in the file uses the format:

student name,id number,degree

For example, the first four lines in the file could look like this:

Mouse Minnie,2010936,BSc
Uphill Pippin,1998148,BSc
Presley Elvis,1958072,BA
Duck Donald,2001375,BSc

The first 4 digits of the id number are the year that the student first enrolled in
the university. Eg. Student with id 2008291 first enrolled in 2008.

Write a C program that reads all the data from the file and displays the names
of all students who enrolled in the university before 2005 and are studying for
a BSc degree. Display the names in alphabetical order.

For example, the output of your program should look something like this:

List of students who enrolled in BSc before 2005:


Duck Donald
Uphill Pippin

[10 marks]

Page 4 of 5
1501/159.101
ALBANY
Internal

6. A hospital uses the following program to calculate which is the biggest group
of patients – based on the age of the patients. Ages are collected in groups of
10, for example there is a group for patients with ages 20 to 29 and another
group for patients with ages 30 to 39 and so on.

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 max, maxindex, count[15];

int main(){
counting();
maxindex = 0;
for (i = 1; i < 15; i++) {
if (count[i] > max) {
max = count[i];
i = maxindex;
}
}
printf("The age of the biggest group is %d to\n",
maxindex*10, maxindex*10 + 9);
printf("There were %d patients in this group.\n",
count[i]);
}

void counting() {
// function counts how many patients in each age group
int i, age;

for (i = 0; i < 15; i--) {


count[i] = 0;
}
while (true) {
printf("Enter the age of a patient (-1 to stop) ");
scanf("%d", age);
if (age = -1) {
return;
}
i = max / 10; // integer division
count[i]++;
}
}
[10 marks]

++++++++

Page 5 of 5

You might also like