peor oss
Curtin?
University oF Technology
Department of Computing
End of Semester 2 Central Examinations - November 2008
Attendance Mode: Internal
Contre Bontloy Campus, Mi Sarawak Campus
Unit(s: 10183. - Introduction to Programming Environ. 152
10225 - Introduction to Programming Environ. 502
Duration: 2Hours Prior fo commencement ofthe examinston ther wil bea
‘minute reading porto. During ths period nots may be writen
(Commencement of te
‘xamationwibe ndcaed by Be superisr.
Total Marks: 120
Calculator: No, not alowed
‘Supplied by the University:
Standby 8 page answer book for students if needed.
‘Supplied by the Student:
None
THIS IS A CLOSED BOOK EXAMINATION
IMPORTANT INFORMATION
‘The posieision ous
Mobile phones or any ther deve cepabe of cemmurictngnrmiion, ae pohiies ing
camo
Elecuonle Organisers? (uth te excopson of autor) other sie dvlns capable o omng
{ext or resrete nloratn are roi tg examina
‘Any breach o examination regulations willbe considered ehesting and appropriate ction wil be
{alan n secordance with Unierty pole.
(ther information:
‘The exam consis of ELEVEN (11) questions
ANSWER ALL QUESTIONS.
Examinaton contioutes 605 ooveral assessment.
‘Student ID: Family Name:
[TTTTTTT) Other Names:
Examination paper IS NOT to be released to student
Page 1 of 15Introduotion to Programming Environments 152/502, Semester 2, 2008
Question L [3 marks)
‘The following segment of code contains atleast three (3) syntax erors tat will cause
compilation fo fi. [dently each ofthese erors and specify the carect code, Indicate
fm the source code below where the errors occu, [I marks each, 3 marks in total]
include
#include
#define PI 3.14159;
int maia(voia)
‘
double min = 3.2;
Souble max = 713)
double ine = 0:24;
double radiue, volume;
radius = min;
while ( radius < max )
«
volume = 4.03.0 * PI * pow( radius, 3);
Peince("radius: 28.26 volume #8.2£\n", Fediue,
voune)
Fodiue + ~ ney
>
>
Page? of 15Introduction to Programming Environments 152/502, Semester 2, 2008,
Question 2 (6 marks]
“The following segment of code is supposed to output the first 20 Fibonacci numbers
4.0.0, 1, 1,2,3,5,8, 13,21, 34, $5, 89, 144, 233,377,610, 987, 1597, 2584, 4181.
Unlortinatly the code conisins at east three (3) logical errors that wil result in
‘successful compilation but not result in the intended operation of the code wien
‘exeouted. Identify each ofthe erors and specify the solution. Indicate onthe source
code below where the errors occur. [2 marks each, 6 marks in total]
finelude
ine eab(int =)
‘ int temp;
ite)
Temp = 0)
use Fa #2)
temp S17
a
emp = £ib(x-1) + £6b(x+2);
return temp;
Page 3 of 15Introduction to Programming Environments 152/502, Semester 2, 2008,
Question 3 [6 marks}
‘Study the following code and state what the output would be iran.
#include
feefine ROWS 2
Paefine COLS 4
void locations(int *vale);
Ant main(voia)
‘
Amt values{ROWS]{COLS) = (47, 4, 23, 5),
48,10, 20, 15397
Locations (6 9310);
Locations evaiues(1](1})+
return 0;
>
void locations ine val)
«
print (*Sd\n", tvaly;
printf ("ta\n", *val #1);
peintf("¥d\n", *val +5);
y
Paged of 15Innodwoion o Programming Envioamens 152502, Semester 2, 2008
(veton 44 marks)
dy the following code and sate what th output would be iru
#include
#dofine Rows 2
Pefine Cons &
void locattons int vaist}{C0L8))7
int matn(vosay
‘ Ant values [ROWS](COLS] = {{9, 5, 3, Bb, {2, 7, 6, S}}7
rooations (valves):
return 0;
»
void locations (int vail 1{COuS})
‘
printe(nsd\ns, *(+val))7
Drinef(vad\n", *(+val+/2))
peinef(radia", *((eval + 1)
prinef(-ea\n", ({eval + 2)
Page Sof 15Introduction to Programming Environments 152/502, Semeste 2,2008
‘Question 5 (22 marks in total)
'5(@) Explain how and why you would use the reserved word const in a program when
specifying a variable type when passing the variable to a function. [2 marks}
5(b) State the #de fine statement to define a macro to compute the volume ofa cuboid
Biven the length, width and depth ofthe cube. Bear in mind the macro could be ealled
‘anywhere in aC program. [3 marks)
5(@) Explain hy you have to be careful when passing parameters to C program wien
‘using recursion. [3 marks}
Page 6 of 15Introduction to Programming Environments 152/502, Semester 2, 2008
'5(@) Explain the difference berween deep copying and shallow copying inthe context of|
dynamically created data structures. [3 marks]
'5(@) Explain with example code, how to use ado. «while loop to check that a
variable va Lue inputted from the keyboard is between two values: max andmin. (3
marks]
(0 Describe a situation where you would use @ switch) statement and not an
4S£...then. . .elge siatement (2 marks)
Page7 of 1SIntroduction to Programming Environments 152/502, Semester 2,2008,
5(g) State the C code to create a state array of 1000 double precision values. (2 marks]
5(h) Explain what each ofthe following two variable definitions means,
[2marks each, 4 marks in ttl)
+ ane *funPer(y?
+ dnt (reunPee) (V7
Page 8 of ISIntroduction to Programming Environments 152/502, Semester 2,2008
(Question 6 [10 marks}
A program consists of a number of files withthe following dependencies:
tral
me fislSub.0 proto
proc
tralSub.h
nial trial Sub. proce
ack ints from a file to one dependent on that fl. Design a nake£ ie 10
accommodate these dependencies such that only necessary compilation and linking is
performed when the command make trial is executed. The following compiler
Sptions and libraries are tobe used:
Wall pedantic -ansi ~std-c99
im for the maths library
Use variables to allow the definition ofthese compiler options and libris to link.
Include a command inthe maiei2e to remove un-nesded files once the program is
compiled.
Paged of 1S} 7 Question 7 [8 marks]
{An Intgrated Development Environment consists ofa umber of program development
tools, State four (4) ofthese tools and describe what each does [2 marks each, 8 marks
in tot
Question 8 [2 marks}
Explain why it sa good idea to break up large complex programs into a number of
separate header and source files.
Page 10 oF 15Introduction to Programming Environments 152/500, Semester 2,2008
Question 9 [12 marks)
“Write a complete C progr that generates a square of the integers each consisting of
{he gts withthe fern the second row being two times that of he at ow abd
theintegr in the thd vow being thes tmes thao he stow. 1a ation each of he
digits to 9 can ony be ved ence An example square shown Below
is
318
7
4
6
‘Write a C program to find all legal occurences of such squares. Hint: use an array to
sore the occurrences ofeach digit.
Page 11 of 15Introduction o Programming Environments 152/502, Semester 2, 2008
Question 10 [12 marks)
‘Waite the C code to define anew datatype called IMAGE that contains data to describe
«two-dimensional colour image. The parameter to beheld inthe datatype are the
‘width and height ofthe image and sone means of string the sctual image data. Think
‘of an appropriate way of holding the image data using dynamic arays and declare this
type as part of the data structure. Write & C code segment to initilisea variable of type
IMAGE that is 640 pixels wide by 480 pixels high including allocation of space forthe
image.
Page 12 of 15Introduction to Programming Environments 152/502, Semester 2,2008
Question 11 (35 marks}
You have been asked to write a C program to process data representing a sock of books.
Jina Bookshop. The data is held in sequential file and, foreach book, consists of a7
digit stock number, the price of the book, is year of publishing and the edition number,
‘The data inthe file i in no particular arr. The bookshop owmer would like fo sd
10% tothe price of each book and store te data in another fle. You decide thatthe best
‘way to deal with the daa iso read it ino a linked-list and proces itin memery. You