90 likes | 206 Views
Lab Session-8 CSIT-121 Spring 2005. Call by Reference Lab Exercise for Demo Practice Problems. Call By Reference. Call by reference means passing the original variable to the function for changing For example compute_sum_and_mean(int k, int j, int l, int& sum, float& mean)
E N D
Lab Session-8 CSIT-121 Spring 2005 • Call by Reference • Lab Exercise for Demo • Practice Problems
Call By Reference • Call by reference means passing the original variable to the function for changing • For example compute_sum_and_mean(int k, int j, int l, int& sum, float& mean) • It has three INPUT arguments and two REFERENCE arguments that are used to output the sum and mean
Lab Exercise Due March 30 • Develop a program that calls a function to read a text file and count the occurrences of all the vowels in the file. The program then displays the results showing total number of characters in the file and number of times each vowel occurs. A sample program and a sample session are shown on the next slides.
Sample Program For Converting Letters to Uppercase • #include <iostream> • #include <cctype> • using namespace std; • void main() • { • char letter; • letter='e'; • letter = toupper(letter); • cout<<letter<<endl; • }
Sample Program For Calling a Function that counts vowels • #include <iostream> • #include <cctype> • using namespace std; • void main() • { • int acnt=ecnt=icnt=ocnt=ucnt=0; • count_vowels(acnt,ecnt,icnt,ocnt,ucnt); • cout<<Counts reported<<endl; • //Program displays all the count values • }
EOF Loop • You will need an EOF controlled loop in this program. This loop can be easily constructed with a priming read and using file-variable.get() function instead of file-variable>> to read single characters from the file.
Sample Session • Enter the file name: lab8doc.txt • Total Number of characters: 2654 • A: 108 • E: 251 • I: 118 • O: 129 • U: 74
Sample Text (Copy into lab8doc.txt file) CSCI 160 - Lab6: Functions - Pass by Reference, Arrays in C++ Goals: More practice with repetition and functions in C++ Obtain experience using pass by reference, arrays Preparation: After reading the problem description below, develop a simple solution algorithm for the problem. When you are ready to implement your algorithm, copy the lab directory to your account (enter the command cp -r ~csci160/Labs/Lab6 . ). Change to Lab6 directory (enter the command cd Lab6) and use the commands discussed in Lab1 to implement and test your program. Problem Description: A store sells five different models of mobile phones. It keeps records of the number of units sold from each model by each sales person. At the end of the month, a summary of total sales needs to be prepared. Write a program to first read the unit price of each model and then allow the user to enter any number of (model, units) pairs. The different models are coded by the numbers 0, 1, 2, 3 and 4. End of input is indicated by giving a pair with 0 as the number of units. When implementing the program, follow the instructions given in lab6.C template. Examples: In the example below, the values entered by the user are indicated in italics. Example 1 $./lab6 Enter unit price of model 0: $79 Enter unit price of model 1: $89 Enter unit price of model 2: $99 Enter unit price of model 3: $109 Enter unit price of model 4: $129 Enter model and number of units (Eg. 2 10) To end input, with any model enter 0 for units (Eg. 1 0) Enter model and number of units: 1 5 Enter model and number of units: 2 4 Enter model and number of units: 3 1 Enter model and number of units: 4 1 Enter model and number of units: 0 12 Enter model and number of units: 2 6 Enter model and number of units: 5 3 Invalid model number. Last entry ignored ... Enter model and number of units: 3 3 Enter model and number of units: 2 6 Enter model and number of units: 1 0 Summary of sales: Model Unit Price Units Total 0 79.00 12 948.00 1 89.00 5 445.00 2 99.00 16 1584.00 3 109.00 4 436.00 4 129.00 1 129.00 Total monthly sales = $3542.00 Assumptions/Restrictions: You may assume that the user will enter valid floating point values for the unit prices and integer values for model code and number of units. Test Set: To see which specific values are being used by the BATS routines, you may view the text files in the ~Lab6/tinp directory and the expected results in the ~Lab6/texp directory (using the Unix commands cat, more or less, e.g. cat tinp/test0).
Practice Exercises for Functions • Chapter 7 Programming Warm Up Exercise 12 (Use getline function and then use islower to count the lowercase characters) • Chapter 7 Programming Problem 4 (use cin>>first>>last and then compare to the names stored in the file. Prepare a file with 5 names and phone numbers)