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++ Programming Exercises and Solutions, Exams of Computer Engineering and Programming

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

2012/2013

Uploaded on 03/28/2013

duraid
duraid 🇮🇳

4.3

(3)

75 documents

1 / 13

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Cork Institute of Technology
Bachelor of Science (Honours) in Computer Applications) - Stage 1
(Bachelor of Science in Computer Applications- Stage 1)
(NFQ – Level 8)
Autumn 2005
Computer Programming
(Time: 3 Hours)
Answer Question 1 and 2 other questions. Examiners :
Dr. D. Chambers
Mr. P. O'Connor
Mr. E. A. Parslow
Dr MGMurphy
pf3
pf4
pf5
pf8
pf9
pfa
pfd

Partial preview of the text

Download C++ Programming Exercises and Solutions and more Exams Computer Engineering and Programming in PDF only on Docsity!

Cork Institute of Technology

Bachelor of Science (Honours) in Computer Applications) - Stage 1

(Bachelor of Science in Computer Applications- Stage 1)

(NFQ – Level 8)

Autumn 2005

Computer Programming

(Time: 3 Hours)

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 using namespace std; //---------------------------------------------------------- const int THE_CONSTANT = 9;

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

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 //cout, << #include //setw() using namespace std; //---------------------------------------------------- void main(void) { int m[10]; int k;

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 //endl, flush, cin, >>, cout, << using namespace std; //----------------------------------------------------------- void main(void) { int theInteger, whatIsThis, aHolder;

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:

  • for each class group, a class data file recording Summer Examination results;
    • for the PIT as a whole, a (much larger) student data file storing relevant details on individual students from all class groups.

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

dcom1_rawResults.dat

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

Æ sorted alphabetically by surname, then firstname (if necessary) and finally by initial(if necessary);

Æ subject marks stripped of subject designation and in the order Accounting, Programming,

Architecture, Mathematics, People&Systems

Æ an average mark for each student(ignore -1 marks for this calculation);

Æ for the whole class a maximum score, a minimum score and an average score for the subjects Programming

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:

(1) write the function ConnectToInputFile() to connect an ifstream to an appropriate input

file, ( 4 marks)

(2) write the function ConnectToOutputFile() to connect an ofstream to an appropriate output

file, ( 4 marks)

dcom1_theResults.dat

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

John X. Boland

will be accepted and a string returned that can be saved in a string filingName holding

Boland, John X.

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

John Boland Boland, John

John X. Boland Boland, John X.

John X.Y.Z. Boland Boland, John X.Y.Z.

c) (13 marks) Write a C++ function

UpdateClock(int& theHour, int& theMin, int& theSec, char& theAMPM)

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

input output

11:59:59 PM 12:00:00 AM

12:59:59 PM 01:00:00 PM

10:59:59 AM 11:00:00 AM

10:17:59 AM 10:18:00 AM

10:17:27 AM 10:17:28 AM

d) (13 marks) Write a function

void crude_histogram(vector aVector);

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

To be or not to be that is the question.

the output might be

the file contains: ------------------

To be or not to be, that is the question.

none of the following letters appeared ===>

-cC-dD-fF-gG-jJ-kK-lL-mM-pP-vV-wW-xX-yY-zZ

Q

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