Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems.

Similar presentations


Presentation on theme: "1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems."— Presentation transcript:

1 1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems

2 2 Call By Reference w Call by reference means passing the original variable to the function for changing w For example compute_sum_and_mean(int k, int j, int l, int& sum, float& mean) w It has three INPUT arguments and two REFERENCE arguments that are used to output the sum and mean

3 3 Lab Exercise 1 w Develop a program that calls a function to order two integer values in the increasing numerical order. w Function name order_values(int& num1, int&num2) w when passed values (x,y), it orders the same as (y,x) if it finds y to be less than x. w The function is void as it returns the values through reference arguments

4 4 Lab Exercise Demo Required Sec-01 Oct 28 Sec-02 Oct 30 w 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 calls another function to display 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.

5 5 Sample Program For Converting Letters to Uppercase w #include w using namespace std; w void main() w { w char letter; w letter='e'; w letter = toupper(letter); w cout<<letter<<endl; w }

6 6 Sample Session w Enter the file name: lab8doc.txt w Total Number of characters: 2654 w A: 108 w E: 251 w I: 118 w O: 129 w U: 74

7 7 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).

8 8 Practice Exercises for Functions w Chapter 7 Programming Warm Up Exercise 12 w Chapter 7 Programming Problem 3


Download ppt "1 Lab Session-8 CSIT-121 Fall 2003 w Call by Reference w Lab Exercise 1 w Lab Exercise for Demo w Practice Problems."

Similar presentations


Ads by Google