






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The c++ code for solving various problems as part of an exam, including q1a, q1b, q1c, q1d, and q1e. The code includes functions with overloading, file i/o, and character manipulation.
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!
Q1 continues…
Q1 continues…
Q1 [50 marks] Answer each of the following questions: Q1a (9 marks) What is the output from the following piece of code? (NOTE: Trace your workings so partial credit may be given for incomplete work): 5 marks The Keano() functions are overloaded. What does that mean? 2 marks Sometimes overloaded functions can be replaced by a template function. Why can’t that be done here? 2 marks
#include
int Keano(int football, int shirt); int Keano(int & football); //------------------------------------------------------- void main() { int football = 20, shirt = 30, boots = 40;
cout << "ball = " << setw(5) << football << " " << "shirt = " << setw(5) << ::shirt << " " << "boot = " << setw(5) << boots << endl; football = Keano(football,boots); cout << "ball = " << setw(5) << football << " " << "shirt = " << setw(5) << shirt << " " << "boot = " << setw(5) << boots << endl; boots = Keano(shirt); cout << "ball = " << setw(5) << football << " " << "shirt = " << setw(5) << shirt << " " << "boot = " << setw(5) << boots << endl; } //------------------------------------------------------- int Keano(int football, int shirt) { int boots = 1; { int football = 100; shirt = boots; boots = football + shirt; } return football + shirt + boots; } //------------------------------------------------------- int Keano(int & football) { int boots = 1; { football = 100; shirt = boots; boots = football + shirt; } return football + shirt + boots; } //-------------------------------------------------------
Q1 continues…
Q1 continued Q1c (8 marks)
What is the output of the following program when it connects with the file a:\s06_q1c.data listed below. (NOTE:Trace all your workings so marks can be given for partial credit.)
#include
ifstream fin; fin.open("f:\s06_q1c.data"); if(fin.fail()) { cout << "\nERROR in opening file *******"; return 1; }
for( int adam = 0; adam < 5; adam++) { fin >> alex; for(int andrew = 0; andrew < alex; andrew++) { fin >> edel; } for(int alan = 1; alan < alex; alan++) { fin >> edel >> oisin; cout << edel + oisin << flush; } getline(fin, david); cout << "****" << adam << endl; } cout << "maksym"; } //-------------------------------------------------------
Q1 continues…
Q1 continued Q1d (8 marks) What is the keyboard input to the following program which connects with the file a:\s06_q1d.data and produces output to that file as listed below. (NOTE:Trace all your workings so marks can be given for partial credit.)
#include
char ch; for(int i = 1; i <= 6; i++) { cout << "\n ... " << i; cin.get(ch); switch(ch) { case 'a': fout << ch; cout << ch; break; case 'b': fout << 'a'<< flush; cout << 'a'<< flush; cin >> ch; switch(ch) { case 'a': fout << '?' << flush; cout << '?' << flush; break; case 'b': fout << "&" << flush; cout << "&" << flush; break; case 'c': fout << '' << flush; cout << '' << flush; break; } break; case 'c': fout << 'b' << flush; cout << 'b' << flush; break;
} } fout << "\ns06_q1d" << endl; } //----------------------------------------------------------
Q1 ends
Q1 continued Q1f [8 marks] What is the output from the following code? (NOTE: Trace your workings so partial credit may be given for incomplete work)
#include
for(a = limit; a >= 0; a--) { for(b = limit; b >= 0; b--) { if((0 == a) || (limit == a) || (limit == a + b) || (a == b)) { cout << a; } else { cout << '#'; } } cout << endl; } } //------------------------------------------------------------------------
Q2 ends
Q2 [25 marks]
A file C:\data.in contains text. Write a C++ program which will process this file and output the following information:
for every line in the file C:\data.in a corresponding line containing only those words containing the sequence “ th ” (ignore case here allowing each of Th, tH, TH, th), (thus a line with no “ th ” will generate a blank line).
the total number of words in the file C:\data.in the number of words in the file C:\data.in which contain “ th ”
Sample Input: C:\data.in Fortran95 is one of the most suitable programming languages for Solving mathematical problems. It is important to compare this language with more modern languages such as C and C++ and Java. Qwerty is here for fun These languages are more suitable for handling text files. Therefore care must be taken when choosing a language.
Sample Output:
File Output C:\WordsWith.th the mathematical this with
These Therefore
Screen Output The file C:\data.in contains 54 words including 6 words containing “th”
Q3 ends
d) (12.5 marks) Write a C++ function
string BonapartesRetreat(string anyString)
which, given the string anyString passed to it, will return the same string but converted to all lowercase if the string size is odd and converted to all uppercase otherwise.
Thus Shane Î shane, HELEN Î Helen, iAN Î ian, RuAdHaN Î ruadhan, SHAne Î shane, ChRIS Î chris, jAmEs Î james while Mark Î MARK, CiARaN Î CIARAN
e) (12.5 marks) Write a C++ function int theRangeOfValues(vector
f) (12.5 marks) Write a C++ function UpdateTime() which accepts the integer parameters hours, minutes, elapsedMinutes , adds elapsed Minutes to the time represented by hours and minutes (this is a 24 hour clock so hours takes the values 0, 1,..., 23) and returns the updated time to the calling function. Thus UpdateTime(23, 23, 50) Î 0: UpdateTime(12, 44, 123) Î 14:
Q4[25 marks]
To demonstrate Selection Sort , using vector technology, the following header file, s06_q4_header.h , has been agreed. Please write C++ code
You may if you wish abandon the library structure and develop the functions and driver in a single file called s06_q4_selectionSort.cpp
void LoadTheVector( vector