Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

C++ Code Analysis for Exam Q1, Exams of Computer Engineering and Programming

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

2012/2013

Uploaded on 03/28/2013

duraid
duraid 🇮🇳

4.3

(3)

75 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Q1 continues…
Cork Institute of Technology
Bachelor of Science (Honours) in Software Development – Stage 1
(NFQ – Level 8)
Summer 2006
COMPUTER PROGRAMMING
Time : 3 Hours
Answer Question 1 and 2 other questions. Examiners : Dr. M. O’Cinneide
Mr. M. Donnelly
Dr M. G. Murphy
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Code Analysis for Exam Q1 and more Exams Computer Engineering and Programming in PDF only on Docsity!

Q1 continues…

Cork Institute of Technology

Bachelor of Science (Honours) in Software Development – Stage 1

(NFQ – Level 8)

Summer 2006

COMPUTER PROGRAMMING

Time : 3 Hours

Answer Question 1 and 2 other questions. Examiners : Dr. M. O’Cinneide

Mr. M. Donnelly

Dr M. G. Murphy

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 #include using namespace std; //------------------------------------------------------- int shirt = 10;

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 //cin, >>, cout, << #include //istream, ostream #include //string using namespace std; //------------------------------------------------------- void main(void) { int alex, edel, oisin; string david;

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"; } //-------------------------------------------------------

a:\s06_q1c.data

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 #include using namespace std; //---------------------------------------------------------- void main(void) { ofstream fout; fout.open("f:\s06_q1d.output"); if(fout.fail()) { cout << "\nERROR opening file****"; return 1; }

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; } //----------------------------------------------------------

a?baba*a

s06_q1d

f:\s06_q1d.output

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 //cout, << using namespace std; //------------------------------------------------------------------------- void main(void) { int limit = 6; int a,b;

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:

  • to a file C:\WordsWith.th

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

  • to the screen

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 holder) to return the range of values in the vector array of int ,. The function will return D -1 when the holder is empty; D (^) 0 when the (^) holder has just one member. D 13 when the largest value is 7 and the smallest value is –6 , etc

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:

Q

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

  • to implement the header file in the file s06_q4_implementation.cpp and
  • to drive the library in the file s06_q4_driver.cpp

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 & aVec); void DisplayTheVector(const vector & bVec); int LocateTheSmallestAfterThis(const vector & cVec, int f); void SelectionSort_Increasing( vector & dVec); void SwapThese(double & a, double & b); //----------------------------------------------------------------------

s06_q4_header.h