University of Mauritius
University of Mauritius
University of Mauritius
FACULTY OF ENGINEERING
MAY 2011
PROGRAMME
MODULE NAME
DATE
Computer Programming
Monday
MODULE CODE
16 May 2011
TIME
NO. OF
QUESTIONS SET
CSE1018Y(1)/
ELEC1055Y(1)
DURATION
NO. OF QUESTIONS
TO BE ATTEMPTED
INSTRUCTIONS TO CANDIDATES
There are 2 Sections in this paper: Section A and Section B.
Answer any Four (4) questions
Section A should be answered using C Programming Language
Section B should be answered using C++ Programming Language
All questions carry equal marks.
3 hours
4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <stdio.h>
(b)
int main()
{
int a, b = 0;
int c[10] = {1,2,3,4,5,6,7,8,9,0};
for(a=0; a<10; ++a)
b += (c[a]%2 == 0) ? c[a] : 0;
printf("%d\n", b);
return 0;
getch();
}
(i)
(ii)
Describe the output generated by the above C program. Show your work
by tracing the above program.
[4 marks]
(iii)
[6 marks]
Sample Output
Repeating vowels are
a
2
e
3
i
2
u
2
Page 1 of 11
Question 1 [Contd]
Notes: Vowels are 'a', 'e', 'i', 'o', 'u'. You may find the functions tolower() or
toupper() useful.
tolower() converts an uppercase character to lowercase without affecting
other characters.
(a)
(i)
(ii)
Sample Input
2 4 13 78 54 55 31 88 0
Sample Output
13 55 31
[8 marks]
(iii)
(b)
Instead of reading inputs from screen as in question 2(a) above, we want to read
data from a file named 'INPUT.dat' which contains numbers. You are required to
write a C program which reads the file 'INPUT.dat' and writes all even numbers
to a file called 'EVEN.dat'.
Note:
in
Page 2 of 11
It returns non-zero if the EOF flag is set, i.e. if you reach the end of
the file during a read.
[10 marks]
Page 3 of 11
struct Transformer
{
char name[100];
char origin[100];
char type;//'g' for good vs 'b' for bad
char vehicle[100];
int numweapons;
};//end struct
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Page 4 of 11
int main()
{
const int NUMTRANSFORMERS = 4;
Transformer trans[NUMTRANSFORMERS];
for (int i=0; i< NUMTRANSFORMERS; i++)
{
inputDetails(trans[i]);
}//end for
55
56
57
58
traitor
59
60
61
62
63
64
65
66
67
68
int num ;
do
{
cout<<"Enter the position in the list, of the transformer who wants to be a
"<<endl;
cin>>num;
if(num<0 || num >=NUMTRANSFORMERS)
break;
becomeTraitor(trans[num]);
}while(num>=0 && num <NUMTRANSFORMERS);
//display details
for (int i=0; i< NUMTRANSFORMERS; i++)
{
displayDetails(trans[i]);
}//end for
69 }//end main
(a)
Explain the purpose of the line 'using namespace std;' in the program.
[3 marks]
(b)
One problem with the above codes is that the type of the Transformer does not
change when the becomeTraitor() function is called. Briefly explain why. Propose
two ways to solve the problem and write ALL the relevant changes to the codes
in each case. You need not rewrite the entire set of codes, but need only highlight
where your changes fit.
[3 + 3 + 4 marks]
Page 5 of 11
#ifndef ALIEN_H
#define ALIEN_H
class Alien
{
private:
char name[100];
char origin[100];
char type;//'g' for good vs 'b' for bad
public:
Alien();
virtual void inputDetails()=0;
virtual void displayDetails();
virtual void displayVerboseType();
char* getName();
};
#endif
You write the following driver program testalien_1.cpp to test your Alien class.
#include <iostream>
#include "alien.h"
using namespace std;
int main()
{
Alien* alien1 = new Alien();
alien1->inputDetails();
alien1->displayDetails();
return 0;
}//end main
Explain the nature of these errors and how they can be prevented.
[4 marks]
(e)
Page 6 of 11
Transformer
Autobot
Decepticon
The main difference between Autobots and Decepticons is that the former are of type 'g'
(good), while the latter is of type 'b' (bad) by default.
The interface for the Alien class is as in Question 3(c). The interface for the classes
Transformer, Autobot and Decepticon are as follows:
#ifndef TRANSFORMER_H
#define TRANSFORMER_H
#include "alien.h"
class Transformer:public Alien
{
private:
int strengthlevel;
public:
Transformer();
virtual void inputDetails();
virtual void displayDetails();
virtual void becomeTraitor();
int getStrengthLevel();
void setStrengthLevel(int strengthlevel);
};
#endif
#ifndef AUTOBOT_H
#define AUTOBOT_H
#include "transformer.h"
class Autobot:public Transformer
{
private:
char vehicle[100];//the vehicle the transformer converts into
public:
Autobot();
virtual void inputDetails();
virtual void displayDetails();
virtual void becomeTraitor();
};//end class
#endif
Page 7 of 11
(a)
Write the constructor for the Autobot such that the type is assigned at creation
time.
[3 marks]
(b)
(c)
Explain the importance of the virtual keyword in classes. Illustrate your answer
by considering the method getName(); in the interface for the Alien class versus
the other methods.
[4 marks]
(d)
Some of the Transformers can become traitors to their race, for example, some
Autobots can become bad (type= 'b') while some Decepticons might become good
(type= 'g').
You want to implement the method becomeTraitor() for the Autobot class as
follows: in case the Autobot is of type 'g', change the type to 'b', else output, xyz
is already a traitor to his race, where xyz is the name of the Autobot.
(i)
What are the changes that need to be made to the Alien interface defined
in Question 3 above so that the method becomeTraitor() can be used in
Autobot class. Justify your answer
[6 marks]
(ii)
Page 8 of 11
#include
#include
#include
#include
#include
#include
#include
<iostream>
"transformer.h"
"alien.h"
"decepticon.h"
"autobot.h"
<vector>
<algorithm>
Page 9 of 11
A sample run for the program with user input highlighted is given below:
Enter number of fighters for both sides
2
Enter details for autobot fighters
Details for autobot fighter 1
Enter name
Optimus Prime
Enter origin
Cybertron
Enter the strength level for the tranformer
8
Enter the vehicle the autobot transforms into
Truck
Details for autobot fighter 2
Enter name
Bubble Bee
Enter origin
Cybertron
Enter the strength level for the tranformer
5
Enter the vehicle the autobot transforms into
Chevrolet Camaro
Enter details for decepticon fighters
Details for decepticon fighter 1
Enter name
Black Out
Enter origin
Cryoton
Enter the strength level for the tranformer
3
Enter the form the decepticon transforms into on earth
Police Car
Details for decepticon fighter 2
Enter name
Megatron
Enter origin
Cryoton
Enter the strength level for the tranformer
7
Enter the form the decepticon transforms into on earth
Helicopter
After the war
Autobots will rule
Page 10 of 11
/ph
Page 11 of 11