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++ Program for Processing Customer Credit Data, Exams of Computer Engineering and Programming

A c++ program that processes customer credit data from two text files. The first file, junecreditcustomers2008.dat, is loaded into a vector and the second file, julycredittransactions2008.dat, is processed line by line to update the customer data in the vector. The updated vector is then saved to a new file, julycreditcustomers2008.dat. The program also displays the customer ids of those who have exceeded their credit limit.

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
Cork Institute of Technology
Bachelor of Science (Honours) in Software Development &
Computer Networking - Stage 1
(NFQ – Level 8)
Autumn 2007
COMPUTER PROGRAMMING
Time : 3 Hours
Answer Question 1 and 2 other questions. Examiners :
Dr. J. Buckley
Dr. A. Kinsella
Dr M.G. Murphy
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download C++ Program for Processing Customer Credit Data and more Exams Computer Engineering and Programming in PDF only on Docsity!

Cork Institute of Technology

Bachelor of Science (Honours) in Software Development &

Computer Networking - Stage 1

(NFQ – Level 8)

Autumn 2007

COMPUTER PROGRAMMING

Time : 3 Hours

Answer Question 1 and 2 other questions. Examiners : 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):

#include using namespace std; //----------------------------------------------------------- int Killeagh(int cork); int Youghal(int cork); //----------------------------------------------------------- void main(void) { for( int waterford = 0; waterford < 7;waterford++) { cout << waterford << "-" << Youghal(waterford) << endl; } } //-------------------------------------------------Killeagh() int Killeagh(int cork) { cork += 12; return cork/3; } //--------------------------------------------------Youghal() int Youghal(int cork) { switch(cork + 2) { case 2 : return 10; case 4 : case 6 : return Killeagh(cork - 2); case 8 : return 30; case 10 : return Killeagh(cork - 4); default : return 50; } } //-----------------------------------------------------------

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 // cin, >>, cout, << etc #include //setw() using namespace std; //----------------------------------------------- double First(double a, double b); void Second(double & a, double & b); //----------------------------------------------- int main(void) { double x = 0.1, y = 0.3, z = First(x,y); cout << " x is " << setw(6) << x << " y is " << setw(6) << y << " z is " << setw(6) << z << endl; Second(x,z); cout << " x is " << setw(6) << x << " y is " << setw(6) << y << " z is " << setw(6) << z << endl; Second(x,z); cout << " x is " << setw(6) << x << " y is " << setw(6) << y << " z is " << setw(6) << z << endl; return(0); } //----------------------------------------First() double First(double a, double b) { double x; x = a + b; cout << " x is " << setw(6) << x << endl; return x*x; } //---------------------------------------Second() void Second(double& a, double& b) { static double y = 2.3; y += ++a; b += a; b -= y; cout << " a is " << setw(6) << a << " b is " << setw(6) << b << " y is " << setw(6) << y << 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 is calculated and displayed as before but then
  • a table is printed displaying each of the “below-average numbers” (i.e. each and every number which is less than average) and its location within the array,
  • the minimum is found and displayed with its location(s) 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]

The file a:\hill.txt, over , contains the classic text you learned at your father’s knee. ↵ is the ‘\n’

character, □ is the space character and ∇ is the end of file marker)

What is values are output by the code below? What do these values signify?

#include //cout, << #include //ifstream #include //string #include //isalpha() using namespace std; //--------------------------------------- void main(void) { ifstream fin; int x, y, z; string s; char c; //----------------------------------- fin.open("a:\hill.txt"); x = 0; for(;;) { fin.get( c ); if(fin.eof())break; if(isalpha(c))x++; } fin.close(); fin.clear(); //------------------------------------ fin.open("a:\hill.txt"); y = 0; for(;;) { getline(fin, s); if(fin.eof())break; y++;

fin.close(); fin.clear(); //------------------------------------ fin.open("a:\hill.txt"); z = 0; for(;;) { fin >> s; if(fin.eof())break; z++; } fin.close(); //----------------------------------- cout << " x: " << x << endl << " y: " << y << endl << " z: " << z << endl; }

a:\hill.txt Jack and Jill □□ ↵ □↵ Went up the hill↵ □□↵ to□fetch□a□pail□of□water□↵ □□□↵ □□□□↵ □□□□□□↵ □□□□□□□↵

Q2 ends

Q2 [25 marks]

The VeryBest department store has a list of customers to whom it allows credit. It keeps on file the details of what each customer owes at the end of each month.

For the end of June this file is called juneCreditCustomers2008.dat. This is a text file which on each line, holds the Customer Number , balance (amount owed in €), credit limit ( in € ) for a customer availing of the stores credit e.g. 1000 55 200 1001 0 500 1002 999 1000 1003 650 500 1004 678 2000 1005 1234 3000 so that customer 1005 owes the store €1234 but is still within the credit limit of €3000 allowed This file is sorted by Customer Number and is kept in increasing order. The credit limit is set at the start of each year.

A file called julyCreditTransactions2008.dat stores the details of all credit transactions during the month of July. This, also, is a text file and on each line, holds the detail Customer Number , credit purchase , payment for each credit transaction during the month e.g.

1234 0.00 100.00 records customer 1234 paying £100 off the account; or 1234 50.00 0.00 records customer 1234 buying £50 of goods on credit; or 1234 75.00 50.00 records customer 1234 buying £75 of goods and making a payment of £50;

If customer 1234 has no transaction during the month, there will be no entry for 1234 in the file. This file is NOT sorted in any way.

Write a C++ program which

  • loads the data in the file juneCreditCustomers2008.dat into a vector of appropriate type ( holdAll say);
  • line by line loads the transaction data in the file julyCreditTransactions2008.dat into a container of appropriate type, scans the vector holdAll for the customer id and then updates the customer data in that vector;
  • downloads the updated vector holdAll to the file julyCreditCustomers2008.dat; displays at the screen the customer id of each customer who has exceeded the credit limit together with the relevant balance and limit.

Q3 ends

d) (12.5 marks) (i)[9.5 marks] Write the C++ definition for the function LinearSearch ( ) that uses a linear search to find the position of a target number within a vector array of unsorted numbers. The function prototype/declaration is void LinearSearch(vector theArray, int theTarget, int & theLocation) where the parameters are, in turn, the search array, the target element, the location sought. The function is to return -1 as this location should the target not be in the array.

(ii)[3 marks] Modify the function LinearSearch ( ) in (i) to give a function

void LinearSearchForSorted(vector theArray, int theTarget, int & theLocation)

which would be a more efficient, but still linear, when the vector array is sorted.

e) (12.5 marks) Write a C++ function that accepts a single string parameter which would analyse the string, character-by-character, to determine if it contains an embedded apostrophe or double quote mark and display an appropriate message. For example

String input Message displayed

John O’Brien An^ apostrophe was found in^ position 7.

She said, “Yes!” A double quote^ was found in^ position 11.

A double quote was found in position 16.

A test case No^ special quotation marks^ were found.

f) (12.5 marks) Write a C++ function that takes an input file stream in and a char target as parameters and returns the number of occurrences of target in the file connected through in.

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]