Professional Documents
Culture Documents
FACULTY OF ENGINEERING
MODULE NAME
DATE
Computer Programming
Tuesday
MODULE CODE
CSE 1018Y(1)
25 May 2010
TIME
NO. OF
QUESTIONS SET
DURATION
3 hours
NO. OF QUESTIONS
TO BE ATTEMPTED
INSTRUCTIONS TO CANDIDATES
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)
Page 1 of 12
i.
ii.
(b)
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
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]
#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;
}
(i)
Explain the above compiling error and what needs to be done to correct
the problem?
[3 marks]
(ii)
Page 4 of 12
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
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
{
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
(c)
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)
(e)
(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
overloaded
version of
[3 marks]
(a)
(b)
#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
Question 4 (Contd)
When compiling the above codes, you have the following error.
(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]
get rid of the error. Justify your answer.
(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
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]
You want to input details about students (Computer Science and Engineering
Students) into a vector.
#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]
Page 12 of 12