






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
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. 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
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
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
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
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
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
already visited (12 marks)
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
#include
#include "thePerfectFunctions.h"
void main(void) { vector
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)
before after
Give an example of function usage ( i.e. C++ code that demonstrates how you would use this function). Can we use this
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
the table would be like (just part of it is visible here) ‘
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.