






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
Various examples of c++ programs, including functions for computing bar needs, drawing triangles, and searching for values in arrays. It also includes examples of file input/output and formatted output.
Typology: Exams
1 / 11
This page cannot be seen from the preview
Don't miss anything!
Q1 continues…
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):
#include
cout << "main : a = " << materazzi << ", b = " << zambrotta << ", c = " << nesta << endl;
cout << zidane(materazzi, zambrotta, nesta) << endl;
cout << "again: a = " << materazzi << ", b = " << zambrotta << ", c = " << nesta << endl;
cout << "\na06_q1a" << endl; } //----------------------------------------------------------------------- int zidane( int & zambrotta, int nesta, int & toni) { nesta = nesta - 6; toni = zambrotta / 2; while( nesta < 5) { zambrotta++; nesta++; } return toni; } //-----------------------------------------------------------------------
Q1 continues…
Q1 continued Q1c (8 marks)
The following code does not keep the children happy as you can see from the test run appended. Please explain why it fails and then modify the code so that it works satisfactorily. (NOTE:Trace all your workings so marks can be given for partial credit.)
#include
void main(void) { int barsNeeded;
cout << "\nhow many bars do you have?.."; cin >> bars; cout << "\nhow many children are there? .."; cin >> children; barsNeeded = ComputeBarNeeds(); cout << "\nbuy " << barsNeeded - bars << " bars so that we have one for everybody" << " with some left over"; cout << "\na06_q1ce" << endl; } //---------------------------------------------------- int ComputeBarNeeds() { if(children > bars) { bars = children + 2; //more than enough } else { bars = bars + 2; //just to be sure } return bars; } //---------------------------------------------------- /*
how many bars do you have?..
how many children are there? ..
buy 0 bars so that we have one for everybody with some left over a06_q1ce
*/
Q1 continues…
Q1 continued Q1d (8 marks) The following program connects with the file f:\s06_q1d.data and sends output to it. What will this output file contain after the following keyboard input: cababbbcbbbaca (NOTE:Trace all your workings so marks can be given for partial credit.)
#include
ofstream fout; fout.open("d:\a06_q1d.output"); assert(fout.is_open());
char ch; for(int i = 1; i <= 6; 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 << "\na06_q1d" << endl; cout << "\na06_q1d" << endl; } //-----------------------------------------------------------------
Q1 ends
Q1 continued Q1f [8 marks] The following code has horrendous style (it tries to do too much in the for line), yet it works. State clearly what the code does and then rewrite the code using a do-while loop or a while loop or an infinite for loop ( i.e. for(;;) instead of the for loop given.
#include
sum = 0; addend = 1; for(cnt = 1; addend != 0; sum += addend) { cout << cnt << ": "; cin >> addend; cnt++; } cout << " sum = " << sum << endl; cout << "\na06_q1f" << endl; } //---------------------------------------------------
Q2 ends
Q2 [25 marks]
The Perfect IT (PIT) has the following data files on file:
The class data file for DCom1 is called dCom1File , is a text file and, on each line, holds the
Registration Number, Class Code, Possible Attendances, Actual Attendances
for a student in the class e.g. .. 1234 dcom1 200 175 1324 dcom1 200 155 1456 dcom1 200 99 .. while the student data file is called theCollegeFile , is a text file and, on each line, holds the
Registration Number, Class Code, Surname, Initials
for a student in the college e.g. .. 1023 dlx1 McGonigal LM 1024 bst3 Semple ST 1033 dme3 Steingrabber CD .. Both files are sorted by Registration Number in increasing order.
Write a C++ program which reads the data from both files as necessary, creates a file called lowAttendances_DCom and sends to it the details of those students in that class who have missed 25% or more of class hours. The low attendance detail for any student will consist of four lines line 1: Registration Number, Student Surname, Student Initials line 2: Possible hours to attend, Actual hours attended line 3: % attendance ( two digits followed by a % sign) line 4: blank
The file lowAttendances_DCom1 will be a text file, sorted in increasing order of Registration Number, which will be headed by the message
Low Attendance List - Dcom
If there are no low attendances, then the message
No low attendances
is to follow immediately after the heading line. The file is to conclude with a count of the total number of students being reported in the file.
NOTE: You may assume the following information(but may not have any use for it)
Q4 continues
Q4[25 marks]
The following code has been submitted to demonstrate Insertion Sort. It is well written but it uses C arrays. I want a version written using vector technology. a) (20 marks) Please rewrite the code with vectors replacing C arrays.
b) ( 5 marks) Write a short note outlining the benefits of the vector version.
#include
LoadTheArray(myArray, CAPACITY, size); DisplayTheArray(myArray, CAPACITY, size); InsertSort_Increasing(myArray, CAPACITY, size); DisplayTheArray(myArray, CAPACITY, size); } //--------------------------------------------------------------------------------- void LoadTheArray(double aArray[], const int aCapacity, int & aSize) { double hold; int k = 0; for(;;) { cout << "anArray[" << setw(2) << k << "] ---> "; cin >> hold; if(cin.eof())break; aArray[k] = hold; k++; if(k == aCapacity) { cout << "\n the array is full. Move on..."; break; } } aSize = k; } //--------------------------------------------------------------------------------- void DisplayTheArray(const double bArray[], const int bCapacity, int bSize) { assert(0 <= bSize); assert(bSize <= bCapacity);
for(int k = 0; k < bSize; k++) { cout << "\n .. " << bArray[k]; } cout << "\n//*********************\n"; } //---------------------------------------------------------------------------------
Q4 ends
void InsertSortNextItem( double cArray[], const int cCapacity, int cSize, int p) { assert(0 <= cSize); assert(cSize <= cCapacity); assert(0 <= p); assert(p <= cCapacity);
int q; bool inserted;
double nextItem = cArray[p]; inserted = false; q = p; while(!inserted) { q = q - 1; if(nextItem < cArray[q]) { cArray[q + 1] = cArray[q]; if(0 == q) { cArray[q] = nextItem; inserted = true; } } else { cArray[q + 1] = nextItem; inserted = true; } } } //--------------------------------------------------------------------------------- void InsertSort_Increasing(double dArray[], const int dCapacity, int dSize) { assert(0 <= dSize); assert(dSize <= dCapacity);
if(dSize > 1) { for(int k = 1; k < dSize; k++) { InsertSortNextItem(dArray, dCapacity, dSize, k); } } } //---------------------------------------------------------------------------------