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 Examples, Exams of Computer Engineering and Programming

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

2012/2013

Uploaded on 03/28/2013

duraid
duraid 🇮🇳

4.3

(3)

75 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Q1 continues…
Cork Institute of Technology
Bachelor of Science (Honours) in Software Development – Stage 1
(NFQ – Level 8)
Autumn 2006
COMPUTER PROGRAMMING
Time: 3 Hours
Answer Question 1 and 2 other questions. Examiners : Dr. M. O’Cinneide
Mr. M. Donnelly
Dr M. G. Murphy
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

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

Q1 continues…

Cork Institute of Technology

Bachelor of Science (Honours) in Software Development – Stage 1

(NFQ – Level 8)

Autumn 2006

COMPUTER PROGRAMMING

Time: 3 Hours

Answer Question 1 and 2 other questions. Examiners : Dr. M. O’Cinneide

Mr. M. Donnelly

Dr M. G. Murphy

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 //pow() using namespace std; //----------------------------------------------------------------------- int zidane( int & zambrotta, int nesta, int & toni); //----------------------------------------------------------------------- void main(void) { int materazzi, zambrotta, nesta; materazzi = 10; zambrotta = pow(materazzi - 7, materazzi - 8); nesta = ( zambrotta + materazzi ) % 2;

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 //cout, <<, cin, >> using namespace std; //--------------------------------------------------- int ComputeBarNeeds(); //this computes how many chocolate bars are needed //--------------------------------------------------- int bars, children;

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 #include #include using namespace std; //----------------------------------------------------------------- void main(void) {

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

f:\a06_q1d.output

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 //cout, << using namespace std; //--------------------------------------------------- Void main(void) { int cnt, sum, addend;

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:

  • for each class group, a class data file recording attendance;
  • for the PIT as a whole, a student data file storing relevant details on individual students.

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)

  • registration numbers are positive numbers not exceeding 9999;
  • class codes have at most 5 characters;
  • the attendance figures are integers, not exceeding 500;
  • the surnames have at most 25 characters;
  • the initials have at most 3 characters
  • The Class Code for DCom1 is “dcom1”

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 #include //setw() #include //assert() using namespace std; //--------------------------------------------------------------------------------- void LoadTheArray( double aArray[], const int aCapacity, int & aSize); void DisplayTheArray(const double aArray[], const int aCapacity, int aSize); void InsertSortNextItem( double cArray[], const int cCapacity, int cSize, int p); void InsertSort_Increasing( double dArray[], const int dCapacity, int dSize); //--------------------------------------------------------------------------------- int main(void) { const int CAPACITY = 10; double myArray[CAPACITY]; int size;

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