Welcome! Log In Create A New Profile

Advanced

Exam 2008 Q & A.

Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Exam 2008 Q & A.
October 19, 2010 08:24AM
Hey guys feel free to have at this paper two. Will update the thread with the rest of the answers I have as the day goes on. O yeah and please tell me if you don't agree with my answers




Question 1:

Q 1.1 As seen in the code fragment, the counter data member is incremented whenever a prime number is found in primeCheck(). Rewrite the class PrimeCount into two separate classes to make use of signals and slots by making the following changes:

Q 1.1.1 Write a separate class Count, which has a data member counter, a slot
incrementCounter() and a function getCounter() to return the value of the
counter. Include both header and cpp files.


A 1.1.1:
Language: C++
//header file ... class Count : public QObject { Q_OBJECT public: int getCounter(); public slots: void incrementCounter(); protected: int counter };   //cpp file ... Count::Count() { counter = 0; }   void Count::incrementCounter() { counter++; }   int Count::getCounter() { return counter; }

Q 1.1.2 Rewrite the class PrimeCount to utilise the new Count class, in such a way
that the class emits a signal foundPrime()when a prime number is found.
Include both header and cpp files.


A 1.1.2:
Language: C++
//header file ...   class PrimeCount : public QObject { Q_OBJECT public : PrimeCount(int sNumber, int eNumber); void primeCheck(); signals: void foundFile(); protected: int sNumber, eNumber; }; #endif   //cpp file ... PrimeCount::PrimeCount(int sNum, int eNum) { sNumber = sNum; eNumber = eNum; }   void PrimeCount::primeCheck() { for(int num = sNumber; num<= eNumber; num++) { int flag =0; for(int i = 2; i< (int)num/2; i++) { if (num%i == 0) { flag = 1; break; } } if(flag == 0) emit foundPrime(); } }

Q 1.1.3: Make changes in the main() function to instantiate the new PrimeCount and
Count classes, to connect the foundPrime() signal to the getCounter() slot
and to output the number of prime numbers found between 10 and 25.


A 1.1.2:
Language: C++
//main() function int main(int argv, char* argc[]) { PrimeCount pc(10,25); Count c();   connect(&pc, SIGNAL(foundPrime()), &c, SLOT(incrementCounter());   pc.primeCheck(); int i = c.getCounter(); qDebug()<< i; return 0; }

Q 1.2 What is the primary use of signals and slots?
A1.2 It is used to communicate between classes. It can also be used along with a controller classes to facilitate communication while still keeping two classes decoupled.

Q 1.3 Explain one advantage of using signals and slots in relation with the PrimeCount and
Count classes in Question 1.1.
A 1.3 It decouples the classes from one another, thus enforcing functional independence; a change in one class’s functionality would not affect the other.




Question 2:

A graphical user interface (GUI) for the program segments on page 2 is required. It should:
??allow the user to enter the start and end numbers (E.g. 10 and 25)
??display strings to facilitate the entering of the start and end numbers
??present a button to activate the calculation
??display the result

Q 2.1 Identify the Qt widgets (classes) to achieve the above facilities.
A 2.1:
- QSpinBox
- QLabel
- QPushButton
- QLabel

Q 2.2 Draw a schematic diagram of how the widgets in Question 2.1 will be arranged using the
QHBoxLayout class?

A 2.2 Will be arranged horizontally, as follows
| QLabel instruct | QSpinBox no1 | QSpinBox no2 | QPushButton submit | QLabel result |

Q 2.3 State one of the controller classes in Qt with respect to GUI applications.
A 2.3 QMainWindow

Q 2.4 Write a regular expression to ensure that the user enters an int of minimum 1 digit and
a maximum of 3 digits for the start (or end) number.


A 2.4
Language: C++
QRegExp digitFormat(\d{1,3});




Question 3:

Q 3.1 Implement the changes required (both the header and cpp files) in the PrimeCount
class to enable threading.


A 3.1:
Language: C++
//header file ...   class PrimeCount : public QThread { Q_OBJECT public : PrimeCount(int sNumber, int eNumber); void primeCheck(); void run(); void stop(); signals: void foundFile(); protected: int sNumber, eNumber; };   //cpp file ... PrimeCount::PrimeCount(int sNum, int eNum) { sNumber = sNum; eNumber = eNum; }   void PrimeCount::primeCheck() { for(int num = sNumber; num<= eNumber; num++) { int flag =0; for(int i = 2; i< (int)num/2; i++) { if (num%i == 0) { flag = 1; break; } } if(flag == 0) emit foundPrime(); } }   void PrimeCheck::run() { primeCheck() }   void PrimeCheck::stop() { terminate(); wait(5000); }


Q 3.2 Implement the changes required in the main() function to have two PrimeCount
threads (one to check prime numbers from 10 to 25 and the second one to check prime
numbers from 30 to 50), one Count instance, and to execute the threads and display
the value of counter.


A 3.2:
Language: C++
//main() function int main(int argv, char* argc[]) { PrimeCount pc1(10,25); PrimeCount pc2(30,50);   Count c();   connect(&pc1, SIGNAL(foundPrime()), &c, SLOT(incrementCounter()); connect(&pc2, SIGNAL(foundPrime()), &c, SLOT(incrementCounter());   pc1.start() pc2.start() int i = c.getCounter(); qDebug()<< i; return 0; }

Q 3.3 What problem might occur when incrementing the counter as stated in Question 3.2.
A 3.3 Both threads might attempt to access the one instance of Count at the same or at intervals which could cause unexpected results or even a program crash. The program is not Thread safe.
Re: Exam 2008 Q & A.
October 20, 2010 02:43PM
A3.3 - just to give a longer answer

The problem is that an increment action is not atomic. What this means is that if you have one process performing an increment action, the procedure is as follows:
1) get the value (x=7)
2) add 1 to that value (x=7)

Then when you have two threads trying to perform the same action, what can happen is

T1) get the value (x=7)
T2) get the value (x=7)
T1) add 1 to the value (x=8)
T2) add 1 to the value (x=8) // but here x should really have been 9 if they had run one after the other

This is called the lost-update problem. So the program will have the wrong result (although I doubt that the program would crash).

To solve thread concurrency problems with Qt classes, one could make use of QMutex (to lock the object or section of code), QSemaphore (to lock a collection of resources) or a QWaitCondition (to put a thread into a non-busy blocking state where another thread can wake it up).
Re: Exam 2008 Q & A.
October 20, 2010 04:52PM
@rezrovs: Thanks man, yeah I wasn't so sure about the whole crash part either. Just figured there might be a possibility if they try to access the object at exactly the same time. Anyway, will try and put up the rest of my answers tonight. Have almost everything on paper. Just a bit busy with Internal Auditing at the moment...bleh! Would rather program any day smoking smiley
avatar Re: Exam 2008 Q & A.
October 20, 2010 05:02PM
@Marthinus, check this link: Exam 2008
Sorry, only registered users may post in this forum.

Click here to login