Questions, comments and answers.
26 January 2011
/*When the programme (as shown below) compiles using the UNIX system
it prints the following:
1
1
2
3
3
3
2
2
2
1
My friend compiled it (the exact same programme ; I sent it via mail)
on his Mac and it printed out the following:
1
1
2
3
3
3
2
3
3
3
If you have a reasonable explanation and the time I'd be quite happy
to hear of it, but presumably all I need to know is that the second
part of my programme is just poor C++...Why? Anyway, thanks for your
time!
*/
________________________________________________________________
#include
#include
using namespace std;
int main ()
{
int c = 1;
cout<c++/++c
22 April 2011
Sample Paper How indicative of the actual exam is the Sample Paper? It's quite a nice paper!
Reply I set them at the same time and believe them to be of a similar standard.
7 May 2011
Rand I have a few questions about ' rand '. Is it included in the cstdlib? What's going on with it here? Do you include ctime for time(NULL)? What's the point of that? Why is there an s before rand?
#include
#include
#include
using namespace std;
int main()
{
srand(time(NULL));
int n=100;
int range=7;
for(unsigned int i=0;i
Also why declare int range=7; and then %range+1 instead of just %8? And I'm probably missing something really obvious but what stops it returning 0?
Reply
srand(time(NULL)) seeds the random number generator, that is,
it takes the time in seconds from some starting time which was ages
ago and is used by all computers and uses that as the 0th integer;
random number generators are deterministic, it works out each new
random number from the last one, so if you don't do this you will get
the same answer each time you run the program.
It is always good to have any numbers you uses as variables rather
than writing them straight into expressions, it makes the code easier
to update and change, so if you were to be changing this code it is
easy to see that the range, the number of different values, is 7,
working out that you wanted to change something in the rand()%7
command would take more thought. rand()%7+1 gives a random number
between 1 and 7 inclusive, rand()%8 gives a number between 0 and 7
inclusive, so they are different, adding the 1 stops it returning 0.
7 May 2011 Sample paper 4a
Hi, I'm looking at the sample paper at the moment. I don't understand
how you got the output for question 4a. I was wondering if you could
explain it please? Reply Well first of all m is
a vector of vectors; we make these two vectors one
by one, initially the first contains 0 1, this is line1 and the second 2 3, this is line2. These are pushed back onto m. Now m has two entries, m.at(0) is a copy of line1 and m.at(1) which is line2. Hence, when we print out $m.at(0).at(1) this is the second entry in the first vector, remembering we number from zero. The second entry in line1 is 1. m.at(1).at(0) is the first entry in the second vector, hence 2. Next we add a third line to m, to do this we reuse the name line1, remember the previous line1 is out of scope here. The name of the vector before it is pushed back is of no importance, what matters is that m now has three entries, each of which is a size two vector. m.at(2).at(0) for example is the first entry in the vector that is the third entry in m, that is 0. m.size() gives 3 since that is the number of vectors in m, m.at(0) is a vector and its size is 2, so m.at(0).size() gives 2. Let me know if you are still confused.
16 May 2011
Hi, will the C++ exam take the form of four questions, of which three are to be chosen? Sorry if you've already answered this, but I can't see it anywhere.
Reply That's correct!
16 May 2011 Unsigned intIn question 3 (e) on the sample exam, why are there brackets around unsigned int on the last line -
" print((unsigned int)(a))"?
Reply It gets confused otherwise, if it reads unsigned on it's own it doesn't know what it is, so you bracked (unsigned int) and it looks at the whole phrase and knows you mean the datatype. It is silly, the name for unsigned int should be uint or something.
|