







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
A series of c++ programming exercises and their corresponding solutions. The exercises cover various topics such as functions, vectors, arrays, and file input/output. The solutions are provided with detailed explanations and workings.
Typology: Exams
1 / 13
This page cannot be seen from the preview
Don't miss anything!
Answer Question 1 and 2 other questions. Examiners : Dr. D. Chambers Mr. P. O'Connor Mr. E. A. Parslow
Dr MGMurphy
Q1 continues…
Q1 [50 marks] Answer each of the following questions:
Q1a (8 marks) Show the output of the following C++ program? (NOTE: Trace your workings so partial credit may be given for incomplete work):
#include
bool cork(int mallow, double macroom); int kerry(char tralee, char dingle); //---------------------------------------------------------- void main(void) { int mallow = 5; char dingle = 'z'; char tralee = 'a'; double macroom = 1.25;
cout << "\nWelcome!\n"; cout << mallow << endl; cout << kerry(dingle, tralee) << endl; if(cork(mallow, macroom)) { mallow = (mallow * -1) + 2; } cout << mallow << " " << macroom << endl; cout << "q1a ends <---"; } //---------------------------------------------------------- bool cork(int mallow, double macroom) { cout << "In cork: "; if(mallow < 1) { cout << "no deal then!" << endl; return false; } while(mallow > 1) { macroom = macroom * 2; --mallow; } cout << mallow << " " << macroom << endl; return true; } //---------------------------------------------------------- int kerry(char tralee, char dingle) { cout << "In kerry: " << tralee << " " << dingle << endl; return THE_CONSTANT; }
Q1 continues…
Q1 continued Q1c (8 marks) Show the output of the following program? (NOTE: Trace your workings so partial credit may be given for incomplete work):
#include
for(a = limit; a > 0; a--) { for(b = 1; b <= limit; b++) { if(1 == a || limit == a || limit == b || a == b) { cout << limit - b; } else if(b > a) { cout << '#'; } else { cout << ' '; } } cout << endl; } cout << "q1d ends <---"; } //-----------------------------------------------------------------
Q1 continues…
Q1 continued Q1d (8 marks) Show the output of the following C++ program. (NOTE: Trace your workings so partial credit may be given for incomplete work):
#include
for(k = 0; k < 10; k++) { m[k] = 10 - k - 1; }
for(k = 9; k >= 0; k--) { cout << "\n ->" << setw(3) << k << " ->" << setw(3) << m[k]; } cout << endl; for(k = 0; k < 10; k++) { cout << "\n -->" << setw(3) << k << " -->" << setw(3) <<m[m[k]]; } cout << endl; for(k = 0; k < 10; k++) { cout << "\n --->" << setw(3) << k << " --->" << setw(3) << m[k]; } cout << "q1c ends <---"; } //----------------------------------------------------
Q1 ends
Q1 continued Q1f (8 marks)
What is the output(to screen and to file) of the following program (NOTE: Trace your workings so partial credit may be given for incomplete work):
#include
for(;;) { cout << "\n\ngimmee an integer ---->"; cin >> theInteger; if(0 == theInteger)break; aHolder = theInteger; whatIsThis = 0; for(;;) { whatIsThis = whatIsThis + aHolder % 10; aHolder = aHolder / 10; if(0 == aHolder)break; } cout << endl << endl << theInteger << " ---> " << whatIsThis << flush; } cout << "q1f ends <---"; } //-----------------------------------------------------------
Q2 ends
Q2 [25 marks] The Perfect IT (PIT) has the following data files on disc:
The class data file for DCom1 is called dCom1_rawResults.dat , is a text file and, on each line, holds the Registration Number, Class Code, Surname, Firstname, Initial together with the SubjectCode, SubjectResult for each of 5 subjects for a student in the class as shown here
1234 dcom1 Board Bill S m56 p88 s78 c45 k 1324 dcom1 Ringing Isobel S s19 k75 m99 p21 c 1456 dcom1 Dupp Stan D c82 k43 s34 m84 p ...
where each mark is entered with a single letter code to designate the subject to which the following mark refers viz. Programming (p), Accounting (k), Architecture (c), Mathematics (m), People&Systems (s). A mark of –1 is entered for a subject where the student has not sat that particular subject. As you can see the marks come in no fixed order. This file, like all College files, is sorted by Registration Number in increasing order.
The class coordinator(your humble servant) wishes to have the results presented
Architecture, Mathematics, People&Systems
and Mathematics. as shown in dcom1_ theResults.dat below:
The data structure, the driver and the function library header file have been agreed and are shown below. It remains to write the functions for the implementation file. Please prepare the following code:
file, ( 4 marks)
file, ( 4 marks)
DCom1 Results: Student Number Acc Prog Arch Math P&S Average
... ... Bill S Board 1234 22 88 45 56 78 57. ... ...
Stan D Dupp 1456 43 67 82 84 34 62. ... ... Isobel S Ringing 1324 75 21 66 99 19 56. ... ...
Mathematics: max 99 min 56 average 79. Programming: max 88 min 21 average 58.
Q3 ends
Q3 [25 marks] Answer any two of the following. a) (13 marks) Write a C++ function NormalToFormal_a() which will accept a string, search it for a full-stop and return another string (leaving the original string unchanged) which consists of the part after the full-stop and blank followed by a comma and then the part up to and including the full-stop. Thus:an original string normalName holding
will be accepted and a string returned that can be saved in a string filingName holding
b) (13 marks) Write a C++ function NormalToFormal_b() which will accept a string, search it for its last blank and return another string (leaving the original string unchanged) which consists of the part after the blank followed by a comma and a blank and then the part before the blank. This function will be much better than that in a). The table shows original strings normalName which will be accepted and the corresponding string returned that can be saved in a string filingName.
normalName filingName
c) (13 marks) Write a C++ function
for a digital clock which will add one second to the current time. The inputs/outputs are three separate integers for the hour (0 – 12), the minute (0 – 59), the second (0 – 59) together with one character (‘A’ or ‘P’) to indicate AM or PM. Sample updates are shown in the table. You can think of many more. Your function must handle them all correctly
d) (13 marks) Write a function
to accept aVector, a vector of integers, and print to screen each integer in a aVector followed by that number of asterisks with a marker instead of each 10 th^ asterisk. Thus when array contains 25, 38, 12, 7, 43, 18 the output is
Q3 ends
e) (13 marks) Write a program that reads text from a file and prints to screen the file and a list indicating each letter of the alphabet which did not appear in the text neither in upper nor in lower case. For example if the input file d:\q3.dat contains
the output might be
int LocateTheSmallestAfterThis(const double cArray[], const int cCapacity, int cSize, int f) { assert(0 <= cSize); assert(cSize <= cCapacity);
int p = f; for( int k = f + 1; k < cSize; k++) { if( cArray[k] < cArray[p]) { p = k; } } return p; } //-------------------------------------------------------------------------- void SwapThese(double & a, double & b) { double c = b; b = a; a = c; } //-------------------------------------------------------------------------- void SelectionSort_Increasing (double dArray[], const int dCapacity, int dSize) { assert(0 <= dSize); assert(dSize <= dCapacity);
for(int k = 0; k < dSize; k++) { int m = LocateTheSmallestAfterThis(dArray, dCapacity, dSize, k); if(m > k) { SwapThese(dArray[k], dArray[m]); } } } //--------------------------------------------------------------------------