• Best of luck to the class of 2024 for their HSC exams. You got this!
    Let us know your thoughts on the HSC exams here
  • YOU can help the next generation of students in the community!
    Share your trial papers and notes on our Notes & Resources page
MedVision ad

comp115/155 past paper questions (1 Viewer)

...

^___^
Joined
May 21, 2003
Messages
7,723
Location
somewhere inside E6A
Gender
Male
HSC
1998
http://teaching.ics.mq.edu.au/units/comp155/paper.pdf

2. 20 marks in total
(a) (4 marks) Complete the following function which when given an integer will return
true if the integer is negative and otherwise false. WRITE ONE LINE ONLY.
bool isNegative(int n)
{ // supply this code - write ONE line only
}
(b) (8 marks) Complete the following function that will determine and return the smallest
of three supplied integer values. Supply only the missing lines.
int minValue(int a, int b, int c)
{ //supply this code


umm..i have no idea what do to with bool functions :(:(

any help is appreciated!
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
ok

it may help you to remember that bool's are basically just integers with 0 being false and non zero (ie positive or negative) being true. yes, this means that if (-1) will be true (try it and see for yourself).

for q 2a)

return(n < 0);

qb)

theres a gagillion ways to do this, id just use the builtin min() function, or write your own.

return(min(a, min(b, c)));

min() is easily written as

int
min(int a, int b)
{
&nbsp;&nbsp;&nbsp;&nbsp;return(a < b ? a : b);
}

or

int
min(int a, int b)
{
&nbsp;&nbsp;&nbsp;&nbsp;if (a < b)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(a);
&nbsp;&nbsp;&nbsp;&nbsp;return(b);
}

&nbsp;
 

...

^___^
Joined
May 21, 2003
Messages
7,723
Location
somewhere inside E6A
Gender
Male
HSC
1998
hmm..another one

=D
(b) (10 marks) Write a function in C++ that when given an integer array A and its size
n will determine if all the items in A[0..n-1] are sorted in non-descending order.
For example if A contains 1,2,2,3,7,7,9 then A is in non-descending order and the
function should return true. Note that non-descending order means that each item
is less than or equal to all the items which follow it.
bool isSorted(int A[], int n)
/* pre : A[0..n-1] is an array of ints
post : returns true if A[0..n-1] is sorted in non-descending
order and otherwise false
{ \\ complete this code


this is my solution..i think i screwed it

for (i = 1; i <= n - 1; i++)
if ( a[0] < a)
return true
else
return false
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
for (i = 1; i < n; i++){
&nbsp;&nbsp;&nbsp;if (a < a[i-1])
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return(false);
}
return(true);
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
hehe

noone comes out of the womb knowing this stuff, everyone has to learn it sometime (well, everyone who does comp at least) :D
 

redruM

Breathe and Stop
Joined
May 11, 2004
Messages
3,954
Gender
Male
HSC
2003
what are going to be the toughest questions??
 

...

^___^
Joined
May 21, 2003
Messages
7,723
Location
somewhere inside E6A
Gender
Male
HSC
1998
another question :(:(

(a) (10 marks) Write a function in C++ that when passed a string S and a character ch
will return the number of times ch occurs in S. There are some useful string functions
in the Appendix.
int occurrences (string S, char ch)
/* pre : none
post : returns a count of the number
of times ch occurs in S */
{ // complete this code


(10 marks) Complete the following function that when passed a string (not containing
any blanks) will determine if the string is a palindrome. Recall that a string is a
palindrome if it is the same when reversed. For example madam, noon, and radar
are palindromes whereas madman is not. You may assume that the string contains
only lower case letters. There are some useful string functions in the Appendix.
bool isPalindrome (string s)
/* pre : none
post : returns true if s is a palindrome
and otherwise false */
{ // complete this code



for c) i can do it if the integer input is numbers, not characters like abc :(:(
 

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
a)

count = 0;
for (i = 0; i < s.length() i++) {
&nbsp;&nbsp;&nbsp;&nbsp;if (s.at(i) == ch)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;count++;
}
return(count);

palindrome:

isPalindrome(string s)
{

if (s.length() <= 1)
&nbsp;&nbsp;&nbsp;&nbsp;return(true);

//if 1st char matches last char
if (s.at(0) == s.at(s.length() - 1))
&nbsp;&nbsp;&nbsp;&nbsp;return(true && ispailindrome(s.substr(1, s.length() - 2)));
//they dont match, not a palidrome
return(false);
}

or something :p
 
Last edited:

doe

Member
Joined
Jan 23, 2004
Messages
751
Gender
Undisclosed
HSC
N/A
you could actually make that last one:

isPalindrome(string s)
{
if (s.length() <= 1)
&nbsp;&nbsp;&nbsp;&nbsp;return(true);
return((s.at(0) == s.at(s.length() - 1)) && ispalindrome(s.substr(1, s.length() - 2)));
}

... hehe
 

Orange Juice

so worthless i am
Joined
Nov 23, 2002
Messages
3,886
Location
Room 112
Gender
Male
HSC
2007
i could only do 23!
triple D post your answers up...
i dont want to post mine if everyone else compares em and stuffs up :p
 

...

^___^
Joined
May 21, 2003
Messages
7,723
Location
somewhere inside E6A
Gender
Male
HSC
1998
1.E
2.C
3.C
4.D
5.C
6.E
7.D
8.C
9.B
10.A
11.B
12.A
13 D <==i thnk
14.B
15.A
16 D <==i thnk
17 :( <==need to read the text
18.D
19.D
20.D
21.B
22.D
23.A
24.B
25.B
26.C
27:(
28.E
29 :(
30 :(
 

Orange Juice

so worthless i am
Joined
Nov 23, 2002
Messages
3,886
Location
Room 112
Gender
Male
HSC
2007
how did you do 9 and 10?
q12 i got d
q16 i got e
what did you get for 17?
i see a smiley
 

...

^___^
Joined
May 21, 2003
Messages
7,723
Location
somewhere inside E6A
Gender
Male
HSC
1998
9 was posted in here somewhere

basically its finding wat statment is the table relating
ah shit, 16 u got it right

and as for 12, i dunno..like both seems right.. =\
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top