Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

University of Mauritius

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

UNIVERSITY OF MAURITIUS

FACULTY OF ENGINEERING

SECOND SEMESTER/YEARLY EXAMINATIONS

MAY 2011
PROGRAMME

BSc (Hons) Electronics with Computer Science


BEng (Hons) Electrical and Electronic Engineering/
BEng (Hons) Electronic and Communication Engineering
BSc (Hons) Information and Communication Technologies

MODULE NAME
DATE

Computer Programming
Monday

MODULE CODE

16 May 2011

TIME
NO. OF
QUESTIONS SET

13:30 16:30 Hrs


5

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

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


SECTION A
All questions in this section should be answered using the C Programming
Language.
Question 1 [Total 25 marks]
(a)

Consider the following C problem:

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)

Draw the flowchart diagram for the above program.

(ii)

Describe the output generated by the above C program. Show your work
by tracing the above program.
[4 marks]

(iii)

Give an alternative C statement(s) equivalent to line number 9 in the


above program.
[3 marks]

[6 marks]

You are required to write a C program, 'repVowels.c', which takes a series


of
alphabets and afterwards displays the repeating vowels with the corresponding
occurrence, each on its own line. The vowel and the occurrence are separated by
a tab. Assume the input consists of only alphabets and can contain both lower
and uppercase letters.
Sample Input
Enter alphabets: AsdatEhjTeEUuklIti

Sample Output
Repeating vowels are
a
2
e
3
i
2
u
2

/Contd next page

Page 1 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)

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.

toupper() converts a lowercase character to uppercase without affecting


other characters.
[12 marks]
Question 2 [Total 25 marks]

(a)

(i)

Write a function in C Programming Language, isEven(), that takes an


integer value as argument and returns 1 if the latter is even, otherwise
returns 0 (zero).
[5 marks]

(ii)

The function isEven() is saved to the file 'usefulfunctions.h'. You are


required to write a C program that allows the user to enter integer
numbers. To end we use 0 (zero) as the last number. Your program
should make use of the isEven() function from 'usefulfunctions.h' and print
the odd numbers on screen.

Sample Input
2 4 13 78 54 55 31 88 0

Sample Output
13 55 31

[8 marks]
(iii)

(b)

Give an advantage of saving the isEven() function in a separate file.


[2 marks]

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:

You should make use your function isEven() defined


'usefulfunctions.h'. You may find the following function useful:

in

/Contd next page

Page 2 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


Question 2 [Contd]

int fscanf(FILE *stream, const char *format, ...);


int fprintf(FILE *stream, const char *format, ...);
char *fgets(char *s, int size, FILE *stream);
int feof(FILE *stream);

It returns non-zero if the EOF flag is set, i.e. if you reach the end of
the file during a read.

A sample of the contents of the 'INPUT.dat' and 'EVEN.dat' files are as


follows:
INPUT.dat
2
3
1
2
6
7
33
48
101
EVEN.dat
2
2
6
48

[10 marks]

Page 3 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


SECTION B
All questions in this section should be answered using the C++ Programming
Language.
Question 3 [Total 25 Marks]
Transformers have started to make their appearances on earth. You have been asked as
scientist to write a small structured program in C++ to keep track of those
transformers. We want to record their names, origin, type ('g' for good and 'b' for bad),
the vehicle they transform into and the number of weapons they carry. Some
transformers can also become traitors where there type changes.
The following program is written. The program should
1.
record details of transformers in an array.
2.
prompt the user about the position in the array for which a transformer become
a traitor. The respective transformer type is modified using the function
becomeTraitor().
3.
Repeat step 2 above as long as the user inputs an int value within the array
bounds, else go to step 4
4.
Display details of the transformers in the array.
1 #include <iostream>
2 using namespace std;
3
4
5
6
7
8
9
10

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

void displayVerboseType(char type)


{
cout<<"\tType: \t";
if(type == 'g')
cout<<"good\t";
if(type == 'b')
cout<<"bad\t";
}//end verboseType

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

void inputDetails(Transformer &tran)


{
cout<<"Enter the name of the transformer"<<endl;
cin.getline(tran.name, 100, '\n');
cout<<"Enter the origin of the transformer"<<endl;
cin.getline(tran.origin, 100, '\n');
cout<<"Enter the type of transformer: g for good and b for bad "<<endl;
cin>>tran.type;
cin.ignore();
cout<<"Enter the vehicle type the tranformer changes into"<<endl;
cin.getline(tran.vehicle, 100, '\n');
cout<<"Enter the number of weapons for the tranformer"<<endl;
cin>>tran.numweapons;
cin.ignore();
}//end inputDetails

34 void becomeTraitor(Transformer tran)


35 {

Page 4 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


36
if(tran.type=='b')
37
tran.type='g';
38
else
39
tran.type='b';
40 }//end becomeTraitor
41 void displayDetails(Transformer tran)
42 {
43 cout<<"Name: \t"<<tran.name<<"\tOrigin: \t"<<tran.origin;
44 displayVerboseType(tran.type);
45 cout<<"\tvehicle: \t"<<tran.vehicle<<"\t Number of Weapons:
\t"<<tran.numweapons<<endl;
46 }//end displayDetails
47
48
49
50
51
52
53
54

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]

/Contd next page

Page 5 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


Question 3 [Contd]
(c)

Consider the interface definition for Alien:

#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

Explain the use of the preprocessor directives #ifndef, #define, #endif.


[3 marks]
(d)

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

On compiling your codes, you have the following error:


testalien_1.cpp: In function int main():
testalien_1.cpp:7: error: cannot allocate an object of abstract type Alien
alien.h:4: note:
because the following virtual functions are pure within Alien:
alien.h:13: note:
virtual void Alien::inputDetails()

Explain the nature of these errors and how they can be prevented.
[4 marks]
(e)

Write the implementation of an overloaded version of the inputDetails() method


that takes all the fields required for the Alien class as parameters and sets the
appropriate attributes.
[5 marks]

Page 6 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


Question 4 [Total 25 marks]
You fine tune your set of programs such that the hierarchy of classes we are interested
in is as follows:
Alien

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

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


Question 4 [Contd]
#ifndef DECEPTICON_H
#define DECEPTICON_H
#include "transformer.h"
class Decepticon:public Transformer
{
private:
char form[100];//the form the decepticon changes into
public:
Decepticon();
virtual void inputDetails();
virtual void displayDetails();
virtual void becomeTraitor();
};//end class
#endif

(a)

Write the constructor for the Autobot such that the type is assigned at creation
time.
[3 marks]

(b)

Write the implementation of inputDetails() for Transformer making use of


inheritance.
[6 marks]

(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)

Write the codes for becomeTraitor() for the Autobot class.


[6 marks]

Page 8 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


Question 5 [Total 25 Marks]
To finalize who will finally take control over earth, it has been decided to have duels
between Autobots and Decepticons. Each team will propose an equal number of fighters,
and these fighters are to be ordered according to their strength. Then, the strongest
fighter from each team fight until one wins. Successive duels are carried with the
remaining pairs of fighters and the results tallied. The team that wins the most duels
will rule the world.
You have been asked to write a program that outputs the team which is expected to
rule after a war, based on the strength of the members. In case of a tie between
Decepticons and Autobots, humans will rule.
Part of the program, containing missing codes is given:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

#include
#include
#include
#include
#include
#include
#include

<iostream>
"transformer.h"
"alien.h"
"decepticon.h"
"autobot.h"
<vector>
<algorithm>

using namespace std;


int main()
{
int numfighters;
int victoryautobot = 0;//variable to count the number of duels won by
//autobots
int victorydecepticon = 0;//variable to count the number of duels won by
//decepticons
cout<<"Enter number of fighters for both sides "<<endl;
cin>>numfighters;
cin.ignore();
//Create the vectors for both clans
vector <Transformer*> autobots(numfighters);
vector <Transformer*> decepticons(numfighters);
//capturing details for autobots
cout<<"Enter details for autobot fighters"<<endl;
for (int i=0; i<numfighters; i++)
{
cout<<"\nDetails for autobot fighter "<<(i+1)<<endl;
autobots[i]= new Autobot();
autobots[i]->inputDetails();
}
//capturing details for decepticons
cout<<"Enter details for decepticon fighters"<<endl;
for (int i=0; i<numfighters; i++)
{
cout<<"\nDetails for decepticon fighter "<<(i+1)<<endl;
decepticons[i]= new Decepticon();
decepticons[i]->inputDetails();
}
//Missing Codes to sort each vector
cout<<"\nAfter the war"<<endl;
//Missing Codes to simulate the war between each pair
if(victoryautobot > victorydecepticon)
cout<<"Autobots will rule"<<endl;
else

Page 9 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


47
48
49
50
51
52
53 }

if (victoryautobot < victorydecepticon)


cout<<"Decepticon will rule"<<endl;
else
cout<<"Humans will rule"<<endl;
return 0;

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

/Contd next page

Page 10 of 11

COMPUTER PROGRAMMING CSE1018Y(1)/ELEC1055Y(1)


Question 5 [Contd]
Explanations for above output:
In the above sample, there will be two warriors on each side. After the input, the
warriors on each side are to be sorted according to strength such that Optimus Prime
should fight against Megatron and Bubble Bee against Black Out. Since the outcome of
both duels are in the favor of Autobots, the Autobots will rule.
You are required to complete the above program. Make sure you indicate where your
changes will fit. You are not required to rewrite the entire sets of codes, but indicate
where your changes will fit. Some of the missing codes have been highlighted. You
may find the at(int index) method for the vector class useful, which accesses an element
at the index specified.
[25 marks]

END OF QUESTION PAPER

/ph

Page 11 of 11

You might also like