Docsity

C plus plus Program - Computer Programming - Exam, Exams of Computer Engineering and Programming

Partial preview of the text

Download C plus plus Program - Computer Programming - Exam 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)

Summer 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. M.G.Murphy

Q1 continues…

Q1 [50 marks] Answer each of the following questions:

Q1a (10 marks) Show the output of the following C++ program? (NOTE: Trace your workings so partial credit may be given for incomplete work):

#include #include using namespace std ; //------------------------------------------------------------------- double Cork(double p, double q); void Fermoy(double & p, double & q); //------------------------------------------------------------------- void main(void) { double a = 0.1, b = 0.3, c = -0.2; cout << fixed << showpoint << setprecision(2); c = Cork (a,b); cout << " a is " << setw(5) << a << " b is " << setw(5) << b << " c is " << setw(5) << c << endl; Fermoy (a,c); cout << " a is " << setw(5) << a << " b is " << setw(5) << b << setw(5) << " c is " << c << endl; Fermoy (a,c); cout << " a is " << setw(5) << a << " b is " << setw(5) << b << setw(5) << " c is " << c << endl; } //------------------------------------------------------------------- double Cork (double p, double q) { double a; a = p + q; cout << " a is " << setw(5) << a << endl; return a * a; } //------------------------------------------------------------------- void Fermoy (double & p, double & q) { static double b = 2.3; b += ++p; q += p; q -= b; cout << " p is " << setw(5) << p << " q is " << setw(5) << q << " b is " << setw(5) << b << endl; } //-------------------------------------------------------------------

Q1 continues…

Q1 continued Q1c (10 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 = 16; int a,b;

for(a = limit; a > 0; a--) { for(b = limit; b > 0; b--) { if(a == b) { cout << '*'; } else if(a + b == limit + 1) { cout << '#'; } else if(1 == a || limit == a || 1 == b || limit == b) { cout << 'E'; } else { cout << ' '; } } cout << endl; } }

Q1 continues…

Q1 continued Q1d (10 marks) Show the output of the following C++ program. (NOTE: Trace your workings so partial credit may be given for incomplete work):

#include #include using namespace std; //--------------------------------------------------- char bantry(int n); int beara(int a, int b); //--------------------------------------------------- int main(void) { int x; for(x = 4; x != 7; x = (x + 4) % 11) { cout << "\n..." << setw(3) << x << "+++"; cout << bantry( beara( x, 6 ) ); } return 0; } //--------------------------------------------------- int beara(int yellow, int red) { return( (yellow < red)? yellow : red); } //--------------------------------------------------- char bantry( int blue) { char haven; switch (blue) { case 1: haven = 'c'; break; case 2: haven = 'a'; break; case 3: case 4: haven = 's'; break; case 5: haven = 't'; break; case 6: haven = 'l'; break; case 7: haven = 'e'; break; default: haven = '!'; } return haven; } //---------------------------------------------------

Q1 continues…

Q1 continued Q1f (6 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 #include #include using namespace std; //----------------------------------------------------------- void damian(int r, char & ch, ofstream & fin); int robbie(int r, int s, int t, ofstream & fin); //----------------------------------------------------------- int main (void) { ifstream fin1, fin2; ofstream fout; int a, b, c; char d, e, f;

fin1.open("z:\M1.in"); if (fin1.fail() ) { cout << "Unable to open file M1.in for input. \n\n"; exit(-1); }

fin2.open("z:\M2.in"); if (fin2.fail() ) { cout << "Unable to open file M2.in for input. \n\n"; exit(-1); }

fout.open( "z:\M3.out"); if (fout.fail() ) { cout << "Unable to open file M3.out for output. \n\n"; exit(-1); }

fin1 >>a >>b; fin1 >>c; fin2 >>d >>e >>f; cout << "\nIn main -->>" << a << "\t" << b << "\t" << c << "\n";

while (a < b) { damian(a, f, fout); a++; cout << "\nIn main -->>" << d << "\t" << e << "\t" << f << "\t" << a << "\t" << b << "\t" << c << "\n"; }

a = robbie(a, b, c, fout); cout << "\nIn main -->>" << a << "\t" << b << "\t" << c << "\n"; fin1.close(); fin2.close(); fout.close();

} //-----------------------------------------------------------

Q1 ends

Q1 continued

where the files mentioned contain the following

z:\M1.in z:\M2.in

Dublin City..

void damian(int r, char& ch, ofstream & f) {

for (int j = r; j > 0; j--) { ch = ch + 1; f << "In damian -->>" << ch <<"\t" <<j <<"\n"; } return; } //------------------------------------------------------------- int robbie(int r, int s, int t, ofstream & f) {

int a, b; a = r + 4; b = r + s + t; t = r; f << "In robbie -->>" <<a <<"\t" <<b <<"\t" <<r <<"\t" <<s <<"\t" <<t <<"\n"; return(a); } //-----------------------------------------------------------

Q2 ends

(2) write the functions SortTheData() and InsertSortNextItem() to sort the vector as

required. SortTheData() traverses the vector and, at each element visited, calls

InsertSortNextItem() to insert this element into its correct position among the elements

already visited (12 marks)

thePerfectFunctions.h

struct Result { int regNr; string classGroup, surName, foreName, fullName; char initial; int math, programming, accounting, architecture, peopleSystems; double average; int absences;

}; //-------------------------------------------------------------------------- void ConnectToInputFile(ifstream & in); void ConnectToOutputFile(ofstream & out); void LoadTheData(ifstream & in, vector & holder); void SortTheData(vector & holder); void InsertSortNextItem(vector & holder, int p); void StowTheData(ostream & out, vector & holder); //--------------------------------------------------------------------------

q2_thePerfectIT.cpp

#include // cin, >>, cout, <<, endl #include // ifstream, ofstream functions #include // exit() #include // strings #include using namespace std;

#include "thePerfectFunctions.h"

void main(void) { vector holder; ifstream in; ofstream out;

LoadTheData(in, holder); in.close();

SortTheData(holder); StowTheData(cout, holder);

ConnectToOutputFile(out); StowTheData(out, holder); out.close();

Q3 ends

Q3 [25 marks] Answer any two of the following. a) (13 marks)

Write a C++ function reverse() which will accept a string as reference parameter and return it reversed e.g.

before after

Hello World dlroW oolleH

a a

12abc345 543cba

Hi iH

there ereht

Give an example of function usage ( i.e. C++ code that demonstrates how you would use this function). Can we use this

to check for anagrams, simple ones like like “mom”, “tot”, more complex ones like “Able was I ere I

saw Elba”?

b) (13 marks) Write a C++ function called reverse() which will accept an integer as value parameter and will return that integer reversed. Thus reverse(1234) Î 4321, reverse(-45678) Î --87654. Give an example of function usage ( i.e. C++ code that demonstrates how you would use this function).

c) (13 marks) Write a program that reads text from a file and prints a table indicating the number of occurrences of each letter of the alphabet in the text. For example if the input file contains

To be or not to be that is the question.

the table would be like (just part of it is visible here) ‘

Letter Number of Occurrences

a, A 1

b, B 2

c,C 0

t,T 6

z,Z 0

d) (13 marks) Write a function that receives two integers as value parameters and returns the larger of the two numbers. If the numbers are equal return that value.

Write a function that receives two doubles as value parameters and returns the larger of the two numbers. If the numbers are equal to the same value return that value.

Rewrite the function so that the one function works for all data types.

Demonstrate usage of this final function.