Académique Documents
Professionnel Documents
Culture Documents
FACULTY OF ENGINEERING
MAY 2012
PROGRAMME
MODULE NAME
DATE
Computer Programming
Friday
MODULE CODE
CSE 1018Y(1)
04 May 2012
TIME
NO. OF
QUESTIONS SET
DURATION
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 3 questions. Answer ALL questions.
Use the MCQ Sheet to answer Question 1.
Section B consists of 2 questions. Answer ALL questions.
Use separate answer books for each Section.
3 hours
5
[20 x 1 Marks]
1. Select the correct value of variable b after executing the following code:
int a=5;
int b=5*(7/2);
A.
B.
C.
D.
E.
5
17.5
15
35.5
NONE of the above
2. Which of the following is the correct function prototype for a function,f, which
accepts 2 integers and return a float?
A.
void float f(int a, int b)
B.
void f(int a, int b)
C.
void f(float a, int b, int c)
D.
float f(int a, int b)
E.
NONE of the above
3. Trace the following code:
int a=7;
int b=5;
int c=7%5;
if (c==0)
printf( Hello!);
else
if(c==1)
printf( Hi!);
else
if (c==2)
printf( Bye!);
else
printf(Dear!);
printf(How are you?);
A.
B.
C.
D.
E.
2
7
6
3
NONE of the above
5. Select the correct syntax for a loop which runs exactly 5 times
A.
for (i=1;i<5;i++)
{
//code to be executed
}
B.
int i=8;
while (i<13):
{
//code to be executed
}
int i=9;
while (i>4)
{
//code to be executed
i--;
}
for (i=5;i>1;i--)
{
//code to be executed
}
C.
D.
E.
6. Which of the following contains all the possible value which a%b can yield, given
that both are ints?
A.
0,1,2.....a
B.
a.....b
C.
1,2,3,....a
D.
0,1,2,.....b-1
E.
NONE of the above
Page 2 of 13
8.
A.
B.
C.
D.
E.
9. Which of the following keywords is used to import header files so as to use the
functions they contain?
A.
import
B.
include
C.
define
D.
Return
E.
NONE of the above
10. Select the correct option for the output of the following code.
int I=6
for (I=8;I<10;I++)
printf(%d,I);
printf(-)
while (I>5)
{
printf(%d,I);
I--;
}
printf()
A.
B.
C.
D.
E.
678910-9876
6789-98765
678910-9876
89-109876
NONE of the above
Page 3 of 13
A.
B.
C.
D.
E.
5,6,7,8,9,
5,5,5,5,5,
5,6,7,10,11
f,f,f,f,f,
NONE of the above
A.
B.
C.
D.
E.
25
26
Any address
Garbage value
Compiler value
13. What will be output if you will compile and execute the following C code?
#include <stdio.h>
void main(){
int array[]={10,20,30,40};
printf("%d",-2*array[2]);
}
A.
B.
C.
D.
E.
-60
-30
60
Garbage value
Compiler error
Page 4 of 13
A.
B.
C.
D.
E.
" , n);
0 1 2 0
0 1 2 1
1 2 0 1
1 2 0 1
NONE of the above
The structures to maintain details about employees can be defined as given below.
Questions 16,17 & 18 refer to this struct.
struct date
{
int dd;
int mm;
int yy;
};
struct employee
{
int eid;
char ename[20];
struct date joined;
};
Page 5 of 13
A.
B.
C.
D.
E.
10 Deepak 1/22/2010
010 Deepak 22/1/2010
10 Deepak 22/1/2010
Deepak 22/1/2010
NONE of the above
Page 6 of 13
19. Which one of the following can replace the ???? in the code above to determine if
the file (myfile.txt) does not exist?
A. inFile==NULL
B. empty(inFile)
C. null(inFile)
D. inFile == NULL
E. NONE of the above
20. Which one of the following can replace the ***** in the code above to determine if
end-of-file is reached?
A. feof(inFile )
B. !eof(inFile)
C. eof(inFile )
D. inFile == NULL
E. NONE of the above
Page 7 of 13
in
in
in
in
For example, if John Smith is the 30th Student on the attendance sheet and has scored 50
in module 1, 70 in module 2 and 20 in module 3 - this implies that his name is in position
29 in Stud_Names, his marks for module 1 will be at position 29 in Stud_Marks_Mod1,
marks for module 2 will be at position 29 in Stud_Marks_Mod2 and so on.
Write a program in C which computes the average marks for each student and displays
the Name and Id of the student with the highest average.
[15 marks]
Question 3 [Total 15 marks]
A bookshop wishes to maintain a computerized list of books and for that purpose a
struct Book is defined as follows:
struct Book
{
char title[25];
char author[25];
float cost;
int pub_year;
int ssn;
};
(a)
(b)
(c)
Write a function input() in C that can be used to input details for a struct Book.
[6 marks]
Write a function output() in C that can be used to output details of a struct Book.
[3 marks]
Write a function main() in C that:
Employee
int num_child
float total_annual_salary
float tax_rate = 15%
Employee(int, char[], int,
float)
float CalculateTax()
Company
float total_annual_sales
float vat_rate = 17%
//using strcpy()
(a)
(i)
(ii)
State, with justifications, whether the following piece of code will compile:
Customer *acust = new Customer(569969, Route du Club, Vacoas);
[2 +2+2 Marks]
(b)
(c)
Write both the Interface and Implementation of the class Employee shown in the
above class diagram.
[12 Marks]
Read the following main program:
#include customer.h
#include employee.h
#include company.h
void main(){
Customer *cust_list[15];
cust_list[0] = new Employee (788877, Royal Road, Beau Bassin, 5, 788900.45);
cust_list[1] = new Company (988477, Plaine Lauzun , 7325900.45);
cust_list[2] = new Employee (611177, Remy Olier, Rose Hill, 0, 348900.88);
. . . . .
cust_list[13] = new Employee (788877, Royal Road, Beau Bassin, 2, 986900.45);
cust_list[14] = new Company (918827, Phoenix Industrial Zone, 50000000.00);
.
.
.
}
(i)
Include in the above main program a section of code that displays the total
tax paid to MRA by all the customers.
[4 Marks]
(ii)
Describe the modifications you would make to the system so the code of
part(c)(i) displays the details of all customers who pay taxes.
[3 Marks]
Page 10 of 13
The Employee Details and Company details as described in Question 4 are stored
in the files employee.txt and company.txt with each field stored on a separate
line. Assume that a displayDetails() function has been added to the different classes
to display the relevant details and that relevant accessor function have been
added to the different classes . The MRA requires that ALL the customer details
be displayed after sorting in order of TAN. Also, the total amount to be received
from all the customers is to be retrieved. The following codes are written:
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
#include "customer.h"
#include "employee.h"
#include "company.h"
//i) Missing codes to define functor to do sorting according to TAN
//ii) Missing codes to define function calculateTotalRevenue, which takes a vector as
parameter, to compute the total to be received by MRA
int main()
{
//iii) Missing codes to initialise vector of customers declared as customers;
int TAN;
char address[100];
int num_child;
float total_salary;
char dummy[10];
//Reading employees data
ifstream employeesfile("data/employees.txt");
if(employeesfile.is_open())
{
while(!employeesfile.eof())
{
employeesfile>>TAN;
employeesfile.ignore();
employeesfile.getline(address,100,'\n');
employeesfile>>num_child;
employeesfile>>total_salary;
employeesfile.ignore();
employeesfile.getline(dummy,10,'\n');
//iv) Missing codes to add the Employee to customers vector
}//end while
employeesfile.close();
}//end if
//Reading company data
ifstream companyfile("data/company.txt");
float total_sales;
if(companyfile.is_open())
{
while(!companyfile.eof())
{
Page 11 of 13
//vi) Missing codes to sort the customers vector according to the functor defined in
part i)
//vii) Missing codes to display the customers in the customers vector
//viii) Missing codes to display the total revenue received by MRA by calling function
calculateTotalRevenue defined in ii) above
return 0;
}//end main
Complete the different parts of missing codes to perform the desired behavior.
Note: You may find the following functions for the vector class useful:
push_back(), iterator, begin(), end(), size()
[17 Marks]
(b)
The MRA has stored the TANs of late tax payers for 2010 and 2011 in two
different files. The TANs are not ordered. It may also happen that a file may
contain a TAN more than once. You are required to write codes that give the
TANs of all the customers who were late payers both in 2010 and 2011. The
following codes have been written:
#include
#include
#include
#include
<iostream>
<fstream>
<algorithm>
<set>
Page 12 of 13
I. Explain why the set container is an ideal candidate to store the details of late
payers of the two years.
[3 Marks]
II. Complete the missing codes as indicated above.
[5 Marks]
Note:
You may find the set_intersection function defined in STL useful. The prototype of the
set_intersection is as follows:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result );
Set_intersection constructs a sorted range that is the intersection of the sorted ranges
[first1, last1) and [first2, last2). The return value is the end of the output range.
You may also find the inserter function useful.
template <class Container, class Inserter>
insert_iterator<Container> inserter (Container& x, Inserter i);
Page 13 of 13