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

Infinite Loop - Computer Programming - Exam, Exams of Computer Engineering and Programming

Main points of this past exam are: Partial Credit, Compile, Longer Commented Out, Namespace, Table Printed, Above-Average Numbers, Artistic Impression, Technical Merit, Standard Program, Appropriate Checks

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
Bachelor of Science (Honours) in Computer Development & Computer Networking –
Stage 1
(
NFQ – Level 8)
Summer 2007
Computer Programming
(Time : 3 Hours)
Answer Question 1 and 2 other questions. Examiners :
Dr. M. Ó Cinnéide
Mr. M. Donnelly
Dr. J. Buckley
Dr. A. Kinsella
Dr. M.G.Murphy
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Infinite Loop - Computer Programming - Exam 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

Bachelor of Science (Honours) in Computer Development & Computer Networking –

Stage 1

( NFQ – Level 8)

Summer 2007

Computer Programming

(Time : 3 Hours)

Answer Question 1 and 2 other questions. Examiners : Dr. M. Ó Cinnéide Mr. M. Donnelly Dr. J. Buckley Dr. A. Kinsella 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): 5 marks This code will not compile when the line marked // 2 is no longer commented out- why not? 2 marks This code will get locked into an infinite loop when the line marked //1 is no longer commented out

  • explain why. 2 marks

#include using namespace std ; //---------------------------------------------------------

void main ( void ) { int r = 9, s = 4; for ( int t = r ; t >= s ; t-- ) { int u = t; for ( int v = r ; v > u ; v-- ) { cout << u << '-' << t * v ; //u-- ; //----------> } cout << '+' << endl; } //cout << u << endl ; //----------> } //---------------------------------------------------------

Q1 continues…

Q1 continued Q1c (8 marks)

What is the output from the following piece of code? (NOTE: Trace all your workings so marks can be given for partial credit.)

#include using namespace std; //--------------------------------------------------------- int sTringer ( int a ); void oGara ( int & a ); void oDriscoll ( int & a, int & b, int & c ); void dArcy ( int a, int b, int c ); //--------------------------------------------------------- void main ( void ) { int r = 50, s = 1, t = 2, u = 3 ; cout << r << " " << s << " " << t << " " << u << endl; cout << sTringer(s) << " is left." << endl ; cout << r << " " << s << " " << t << " " << u << endl; oGara ( r ); cout << r << " " << s << " " << t << " " << u << endl; dArcy ( s, t, u ); cout << r << " " << s << " " << t << " " << u << endl; oDriscoll( s, t, u); cout << r << " " << s << " " << t << " " << u << endl; } //--------------------------------------------------------- int sTringer ( int a ) { int b = 25 ; cout << a << "! Yummy Integer!" << endl; a -= b; return a ; } //--------------------------------------------------------- void oGara ( int & a ) { a = 0 ; } //--------------------------------------------------------- void oDriscoll ( int & a, int & b, int & c ) { int d; cout << "Move Over "<< a << " now there's " << b << endl; d = a; a = b ; b = c ; c = d ; } //--------------------------------------------------------- void dArcy ( int a, int b, int c ) { oDriscoll ( a,b,c ); cout << "No! I said sit still!" << endl; } //---------------------------------------------------------

Q1 continues…

Q1 continued Q1d (8 marks) The following C++ code correctly computes the average of doubles which it reads from the keyboard: But the result is of little use in analyzing the numbers entered. Modify the code so that the numbers are read as before but stored in a C-array of capacity 100, the average calculated and displayed as before but then a table printed displaying the “above-average numbers” (i.e. each and every number which is greater than average) and their location within the array.

#include //setw() using namespace std; //--------------------------------------------------------- int main(void) { integer howMany; double holdIt;

double sum, average;

cout << "\n how many numbers do you want to average?";

for(;;) { cin >> howMany; if(howMany > 0 )break; cout << " must be >0. Try again."; }

sum = 0; for(int k = 0; k < howMany; k++) { cout << "\n a[" << setw(3) << right << k << "] = "; cin >> holdIt; sum += holdIt; } average = sum / howMany; cout << "\n The average is : ... " << average; cout << endl << "---> q1d output <---" << endl; } //---------------------------------------------------------

Q1 ends

Q1 continued Q1f [8 marks] What is the output from the following code? (NOTE: Trace your workings so partial credit may be given for incomplete work)

#include //cout, << #include //setw() using namespace std; //--------------------------------------------------------- void main(void) { int limit = 5; int a,b;

for(a = limit; a >= 0; a--) { for(b = limit; b >= 0; b--) { if( (0 == a) || (limit == a) ) { cout << setw(2) << a; } else if( (limit == a + b) || (a == b)) { cout << "-*"; } else if(a < b) { cout << setw(2) << a; } else { cout << setw(2) << b; } } cout << endl; } } //---------------------------------------------------------

Q2 ends

Q2 [25 marks]

In competitive skating, each skater completes two programs. The first program for each skater is a ‘standard’ program specified by the judges. The second program for each skater is a ‘free-style’ program where the content is specified by the skater his/herself.

For each of these programs, nine judges score the program twice from 0 to 6, inclusive, in steps of 0.1. The first score is for technical merit. The second score is for artistic impression. These scores are stored on successive lines of the file s C:\StandardProgramScores.data and C:\FreeProgramScores.data as appropriate and as shown below.

The score for technical merit is obtained by discarding the lowest and highest scores and then adding the remaining scores. The score for artistic impression is obtained similarly. The total score for the program is the sum of the scores for technical merit and for artistic impression

The skaters skate the standard program first in order and then, in the same order, they skate the free program, When the competition is finished they are ranked according to their overall score which is the sum of the scores for the standard and for the free programs.

Write a C++ program to compute skating scores using the following I/O specifications:

Input: Taken from the text files C:\StandardProgramScores.data. and then C:\FreeProgramScores.data Each pair of lines contains the following details: · skater’s competition entry number,· judges’ scores for technical merit · skater’s competition entry number,· judges’ scores for artistic impression

Consider the following sample input data:

C:\StandardProgramScores.data 1234 4.5 4.7 4.3 4.9 4.6 4.5 5.1 4.8 4. 1234 5.2 5.1 5.5 5.0 4.9 5.5 5.3 5.7 5. 2345 5.8 5.5 5.0 5.3 4.5 5.8 5.2 5.5 5. 2345 5.0 4.8 5.8 5.4 5.3 4.8 5.0 5.1 5. 3456 5.5 5.5 5.5 5.0 5.0 5.0 5.5 5.5 5. 3456 5.7 5.5 5.3 5.9 5.2 5.4 4.6 4.9 3. 4567 5.2............... etc etc

C:\FreeProgramScores.data 1234 5.5 5.7 5.3 5.9 5.6 5.5 5.1 5.8 5. 1234 5.9 5.4 5.1 5.4 5.9 5.8 5.7 5.4 5. 2345 5.4 5.7 5.4 5.7 4.9 5.2 5.5 5.0 5. 2345 5.5 4.2 5.5 5.8 5.9 4.6 5.9 5.4 5. 3456 5.9 5.9 5.8 5.8 5.7 5.7 5.6 5.6 5. 3456 5.5 5.6 5.6 5.7 5.7 5.8 5.8 5.9 5. 4567 4.2............... etc etc Process: process the data from each file to generate the scores for all competitors. Make appropriate checks throughout.

Output: Publish the detailed results as a table containing the following details in the output file called c:\skate_results.rpt

Competition Standard Free Total Number Technical Artistic Technical Artistic 1234 32.9 37.1 39.3 39.8 149. 2345 3456 etc etc

Q3 ends

e) (13 marks) Given again the declarations struct Results { string name; char grade; int mark; }; ... ... vector myClass; of d) and given that grades have been allocated according to the following score grade score ≥ 70 A 70 > score ≥ 63 B 63 > score ≥ 55 C 55 > score ≥ 40 D 40 > score ≥ 0 E

write a C++ function AverageForTheGrade() which will accept the vector myClass and a char theGrade and then return the average of the student scores earning that grade.

f) (13 marks) Write a C++ function called toOctal() which will accept as value parameter an int n and will return its octal (base 8) equivalent. Thus toOctal(17) returns 21, toOctal(197) returns 305, toOctal(20) returns 24,. toOctal(47) returns 57, toOctal(48) returns 60 toOctal(49) returns 61,.

Q

Q4[25 marks]

A “ Text Concordance ” is an alphabetical listing of all the distinct words in a piece of text; e.g. the Text Concordance for “It was the best of times, it was the worst of times.” is “best it of the times was worst”.

A C++ function is required to perform such a Text Concordance on the text contained in a file and store it in another report file. The function parameters are one input file stream fin and one output file stream fout. It may be assumed that the required files have been opened in main() and connected through fin and fout.

Please write two versions of the C++ function: TextConcordance_0(ifstream & fin, ofstream & fout); TextConcordance_1(ifstream & fin, ofstream & fout); Both versions will store successive words, from the fin stream, in a vector but a) version1 will (i) add each word only if it is NOT already in the vector, (ii) use an appropriate function template from the library to sort the vector when all words have been processed and then (iii) send the words to the output file; [8 marks] b) version2 will (i) add each word “blindly” (i.e. no checking at all) to the vector, (ii) use appropriate function templates from the library to order the vector and then to remove duplicates, and then (iii) send the words to the output file. [9 marks]

NOTE: It should be convenient to convert all words to lower case so code the function void LowerTheCase(string & aWord); [4 marks] and don’t forget to strip any punctuation from the end of a word(assume there is at most one) when you code the function void DropAnyPunctuation(string & aWord);. [4 marks]