Anda di halaman 1dari 13

# UNIVERSITY OF MAURITIUS

FACULTY OF ENGINEERING

MAY 2010
PROGRAMME

## BSc (Hons) Electronics and Computer Science

BSc (Hons) Information and Communication Technologies

MODULE NAME
DATE

Computer Programming
Tuesday

MODULE CODE

CSE 1018Y(1)

25 May 2010
TIME
NO. OF
QUESTIONS SET

## 09.30 12.30 Hrs

5

DURATION

3 hours

NO. OF QUESTIONS
TO BE ATTEMPTED

INSTRUCTIONS TO CANDIDATES

## There are 2 Sections in this paper: Section A and Section B.

Section A consists of 2 questions. Answer ALL questions from Section A
Section B consists of 3 questions. Answer any 2 questions from Section B
Use separate answer books for each Section
All questions carry equal marks.

## COMPUTER PROGRAMMING CSE 1018Y(1)

SECTION A
Answer all questions from this Section. All questions in this section should
be answered using the C programming language.
Use separate answer books for each Section
Question 1 (25 marks)
(a)

## Consider the following problem:

A group of friends decides to go for a lunch fair (tasting different types of food)
together. They agree to share the overall total cost for the lunch equally. Each
person pays for one meal, and later on money is exchanged to even out the
amount of money spent.
Each person's meal cost are rounded up and tallied, then afterwards money is
exchanged among the friends so that the real cost spent (or net cost) by each
person is met.
You are required to compute, from a list of meal costs, the total amount of
money that must change hands in order to equalize all the persons' costs.
Your program should accept an integer number n, which denotes the number
of friends, followed by n lines of input, for each meal cost per person in rupees.
Example input:
Enter number of friends: 4
meal cost(Rs) for person no 1: 12.00
meal cost(Rs) for person no 2: 15.50
meal cost(Rs) for person no 3: 14.00
meal cost(Rs) for person no 4: 6.00
Output:
minimum total cost exchanged(Rs): 6.00
Note: Person no 2 and 3 have to give person no 4, Rs4.00 and Rs2.00
respectively, in order to equalize the money.
/Contd next page

Page 1 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

Question 1 (Contd)
[8 marks]

i.

ii.

## Write a program in C to compute the above problem. Assuming there

can be a maximum of 20 friends.
You may find the function ceil() useful from the <math.h> library which
returns a value as double.
e.g: x=7.1, ceil(x) = 8.0
x=7.9, ceil(x) = 8.0
[14 marks]

(b)

## Suppose we call scanf as follows:

float x, y;
int i;
scanf("%f%d%f", &x, &i, &y);
If the user enters
12.3 45.6 789
What will be the value of x, i and y after the call? Justify your answer.
[3 marks]

## Question 2 (25 marks)

(a)

Suppose you are asked to read prices from the file, price.dat, and to compute the
tax and total amount which are then written to the file, total.dat. All prices have
a tax rate of 5%.

A sample of the contents of the input and output files are as follows:
price.dat
12.50
32.75
90.80
total.dat
For price 12.50, Tax amount 0.62 and Total amount 13.12
For price 32.75, Tax amount 1.64 and Total amount 34.39
For price 90.80, Tax amount 4.54 and Total amount 95.34

...
Page 2 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

Question 2 (Contd)
(i)

Before writing the program, you need to decide on a few design issues. You
decide to use C programming language and that it is preferable to read each
line from the file, price.dat, and keep the value in an array of characters. After
analyzing the file, you noticed that a price cannot have a length greater than 10,
i.e there is no price greater than 9999999.99. Write the line of codes to set the
length and the tax rate as constant global values accordingly.
[2 marks]

(ii)

You decide to write a separate function computeTax(), which takes the price (a
float value) as parameter and returns the Tax amount. Write the codes for the
function computeTax().
Suppose price is 12.50, then 5% tax amount = 0.625

(iii)

[6 marks]

You would like to call your above function before it has been defined within
your main program. Write the statement that is required to allow this.
[2 marks]

(iv)

Assume that you can use only <stdio.h> and <stdlib.h> libraries and the
statements and functions defined above. Write the main function in C
programming language in order to read the price.dat file and write to total.dat
file as shown in the sample above.
You may find the functions fgets() and atof() useful.
[10 marks]

Page 3 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

Question 2 (Contd)
(b)

## Consider the following C program, strexample.c:

#include <stdio.h>
#include <stdlib.h>
main(){
int i = 0;
char str[] = "Are we having fun yet?";
int n =0, count=0;
n = strlen(str);
while (i < n)
{
if (str[i] == ' ')
count++;
i++;
}
printf("%d\n", count);
return 0;
}

## strexample.c: In function main:

strexample.c:11: warning: incompatible implicit declaration of built-in
function strlen

(i)

Explain the above compiling error and what needs to be done to correct
the problem?
[3 marks]

(ii)

above error?
[2 marks]

Page 4 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

SECTION B
Use separate answer books for each Section
Answer any two questions from this section. All questions in this section should be
answered using the C++ programming language.
All questions in this section will pertain to a system to store student information
Question 3 (25 marks)
Consider the following codes making use of C++ as a structured language.
1
2
3
4

#include <cstdlib>
#include <iostream>

5
6
7
8
9
10

struct Student{
string id;
string fname;
string sname;
float cpa;
};

11
12
13
14
15
16
17
18
19
20
21
22

void inputStudent(Student s)
{
cout<<"Enter id"<<endl;
cin>>s.id;
cout<<"Enter First Name"<<endl;
cin>>s.fname;
cout<<"Enter Surname"<<endl;
cin>>s.sname;
cout<<"Enter cpa"<<endl;
cin>>s.cpa;
cin.ignore();
}

23
24
25
26
27
28
29

void displayStudent(Student s)
{
cout<<"ID :"<<s.id;
cout<<"\tSurname :"<<s.sname;
cout<<"\tFirstname :"<<s.fname;
cout<<"\tCPA: "<<s.cpa<<endl;
}

30
31
32

int main()
{
float totalStudent;

33

## const int MAXStudent = 2;

34
35
36
37
38
39
40
41

Student s1[MAXStudent];
cout<<"\nProcessing details of Student"<<endl;
for(int i=0; i<MAXStudent;i++)
{
cout<<"Details for student"<<i+1<<endl;
inputStudent(s1[i]);
}
for(int i=0; i<MAXStudent;i++)

Page 5 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

42
43
44
45
46
47

{
cout<<"Details for student"<<i+1<<endl;
displayStudent(s1[i]);
}
return EXIT_SUCCESS;
}

One of the problems with these codes is that the input entered by the user is not being
reflected at the output. For example, after entering valid input, the invalid output is
obtained as follows.
Processing details of Student
Details for student1
Enter id
123
Enter First Name
Tom
Enter Surname
Jones
Enter cpa
23.4
Details for student2
Enter id
234
Enter First Name
Angelina
Enter Surname
Jolie
Enter cpa
234
Details for student1
ID : Surname : Firstname :
CPA: -1.33445
Details for student2
ID : Surname : Firstname :
CPA: 3.98924e-34

(a)

Explain the problem with the codes above that cause the resulting behavior in
the output.
[5 marks]

(b)

What is the solution? Propose two different syntaxes for solving the problem.
For each syntax, highlight the different changes to be made to the overall code.
[6 marks]
Consider the following definition Student.h for the class Student:

Page 6 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

Question 3 (Contd)
#ifndef STUDENT_H
#define STUDENT_H
class Student
{
private:
char id[7];
char fname[100];
char sname[100];
float generalFee;
float penaltyFee;
float numberOfCredits;
public:
Student();
Student(char id[],char fname[],char sname[], float generalFee, float penaltyFee, float numberOfCredits);
virtual void inputDetails();
virtual void displayDetails();
virtual float getTotalAmountPaid()=0;
float getNumberOfCredits();
};
#endif

(c)

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

[3 marks]
The method inputDetails() is implemented as follows in Student.cpp:

void Student::inputDetails()
{
cout<<"Enter id"<<endl;
cin.getline(id,7,'\n');
cout<<"Enter First Name"<<endl;
cin.getline(fname,100,'\n');
cout<<"Enter Surname"<<endl;
cin.getline(sname,100,'\n');
cout<<"Enter amount for general fee"<<endl;
cin>>generalFee;
cout<<"Enter amount for penalty fee"<<endl;
cin>>penaltyFee;
cout<<"Enter number of credits earned"<<endl;
cin>>numberOfCredits;
cin.ignore();
}

(d)

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

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

(e)

## Illustrate how you

would
call your

(f)

Suppose, in the Student class, you want to replace the different attributes of type
array of chars with string attributes, how would the codes,
cin.getline(id,7,'\n'); need to be changed to have the same
functionality.
[3 marks]
Page 7 of 12

version of
[3 marks]

## Question 4 (25 marks)

Suppose we have a class Engineering, defined as EngineeringStudent.h, where Student.h
was defined as in Question 3 (b) above:
#ifndef ENGINEERING_STUDENT
#define ENGINEERING_STUDENT
#include "Student.h"
class Engineering:public Student
{
private:
float labFee;
public:
Engineering();
float getLabFee();
virtual void inputDetails();
virtual void displayDetails();
virtual float getTotalAmountPaid();
};
#endif

(a)

## Assume that displayDetails() has been implemented in the file

Student.cpp and outputs the different attributes. Write the
implementation for displayDetails() for the Engineering class by
making use of inheritance and also outputs the relevant Engineering
attribute.
[6 marks]

(b)

## Write the implementation of an overloaded constructor for the Engineering

class that takes parameters defined for all attributes of the Student class and the
labFee. Assume that the overloaded constructor for the Student class taking as
parameters all the relevant attributes and setting them accordingly is
implemented in Student.cpp.
[6 marks]
You have yet another class, ComputerScience to represent a student of
ComputerScience defined as:

#ifndef COMPUTER_SCIENCE
#define COMPUTER_SCIENCE
#include "EngineeringStudent.h"
class ComputerScience:public Engineering
{
private:
float practicalFee;
public:
ComputerScience();
float getPracticalFee();
virtual void inputDetails();
virtual void displayDetails();
virtual float getTotalAmountPaid();
};
#endif

Page 8 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

The method getTotalAmountPaid() is defined as in ComputerScience.cpp.
float ComputerScience::getTotalAmountPaid()
{
return generalFee + penaltyFee + labFee + practicalFee;
}

## You write a driver program TestComputer.cpp as follows to test the

ComputerScience class.
TestComputer.cpp
#include "ComputerScience.h"
#include <iostream>
using namespace std;
float computeTotalCredits(Student *s[],int sz)
{
float total=0;
for (int i=0; i<sz;i++)
total = total + s[i]->getNumberOfCredits();
}
int main()
{
const int NUMSTUDENTS = 2;
Student *students[NUMSTUDENTS];
cout<<"Enter details for Computer Science students"<<endl;
//getting input of students
for (int i=0; i< NUMSTUDENTS; i++)
{
students[i] = new ComputerScience();
students[i]->inputDetails();
}//end for
//now display details for students
cout<<"Now displaying details for Computer Science students"<<endl;
for (int i=0; i< NUMSTUDENTS; i++)
{
students[i]->displayDetails();
cout<<"\nTotal amount paid"<<students[i]->getTotalAmountPaid();
}//end for
//Now display the total number of credits
cout<<"\nTotal number of credits registered by all students is
"<<computeTotalCredits(students,NUMSTUDENTS)<<endl;
}

Page 9 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

Question 4 (Contd)

When compiling the above codes, you have the following error.

## Student.h: In member function virtual float

ComputerScience::getTotalAmountPaid():
Student.h:11: error: float Student::generalFee is private
ComputerScience.cpp:32: error: within this context
Student.h:12: error: float Student::penaltyFee is private
ComputerScience.cpp:32: error: within this context
EngineeringStudent.h:6: error: float Engineering::labFee is private
ComputerScience.cpp:32: error: within this context
Student.h: In member function virtual float
Engineering::getTotalAmountPaid():
Student.h:11: error: float Student::generalFee is private
EngineeringStudent.cpp:32: error: within this context
Student.h:12: error: float Student::penaltyFee is private
EngineeringStudent.cpp:32: error: within this context

(c)

Would changing the accessor type of the generalFee and penaltyFee in the
Student class and that of labFee in the Engineering class from private to protected
[4 marks]

(d)

Illustrate two other ways how the above compile error could be solved.
[6 marks]

(e)

One of the problems with the system defined is that some users are writing
negative numbers for the ID. You want to write codes to validate the ID. Which
file(s) would be the most appropriate to write these codes?
[3 marks]

Page 10 of 12

## Question 5 (25 marks)

Students have been required to submit a small assignment on the CSE forum. The
submission should consist of a container and a method available in that container from
the Standard Template Library (STL). Each submission takes the form of
container:methodname. Students were required to submit unique answers. However,
by their friends. You are required to determine whether their suspicion is founded or
not. The input will consist of a number n where(n>1), followed by n lines containing
the Container:methodname pairs. The output will contain the phrase duplicates
exist in case there are duplicate submissions or unique otherwise.
The following are sample input/output combinations:
Input
3
Set:begin()
Set:clear()
Set:size()

Output
unique

4
Deque:at()
deque:At()
Set:find()
Set:insert()

duplicates exist

Note that the input should be case-insensitive (deque:At() and Deque:at() are
considered as the same entry).
(a)

Why would the set container from the STL be adequate to solve the
above problem?
[4 marks]

(b)

[9 marks]

## Assume that you have a function convertToUpperCase(string) and

returns a string converted to uppercase.
(c)

You want to input details about students (Computer Science and Engineering
Students) into a vector.

Page 11 of 12

## COMPUTER PROGRAMMING CSE 1018Y(1)

Question 5 (Contd)
You have the following codes:
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

#include <cstdlib>
#include <iostream>
#include "Student.h"
#include "EngineeringStudent.h"
#include "ComputerScience.h"
#include <vector>
#include <algorithm>
using namespace std;
int main(int argc, char *argv[])
{
const int NUMSTUDENTS = 4;
vector <Student*> students(NUMSTUDENTS);
for (int i=0; i<NUMSTUDENTS; i++)
{
char choice;
cout<<"\nChoose student type: c Computer Science, e Engineering "<<endl;
cin>>choice;
switch (choice)
{
case 'c':{
students[i]= new ComputerScience();
break;
}
case 'e': {
students[i] = new Engineering();
break;
}
default : cout<<"Wrong input";
}//end switch
cin.ignore();
students[i]->inputDetails();
}//end for
//Details of Students before sorting
// Details of Students after sorting
return EXIT_SUCCESS;
}//end main

You want to add the necessary codes such that you can use STL to sort the students
according to their numberOfCredits. You also want to display the details of
students before and after sorting.
Write the codes for the above. You are not required to rewrite the entire set of codes,
but make sure that you highlight where you add codes (using the line numbers for
example)
[12 marks]

/ph

Page 12 of 12