home *** CD-ROM | disk | FTP | other *** search
- // ========================================================================
- // TwotyFruity.cs
- //
- // This program adds up the costs and quantities of selected fruit types
- // and outputs the results to the display. This module is a variation
- // of the the FruitLoopy.cs module designed to demonstrate how to use
- // functions.
- // ========================================================================
-
- function initializeFruit( )
- // ------------------------------------------------------------------------
- // Set the starting values for our fruit arrays, and the type
- // indices
- //
- // RETURNS: number of different types of fruit
- //
- // ------------------------------------------------------------------------
- {
- %numFruitTypes = 5; // so we know how many types are in our arrays
- $bananaIdx=0; // initilize the values of our index variables
- $appleIdx=1;
- $orangeIdx=2;
- $mangoIdx=3;
- $pearIdx=4;
-
- $names[$bananaIdx] = "bananas"; // initilize the fruit name values
- $names[$appleIdx] = "apples";
- $names[$orangeIdx] = "oranges";
- $names[$mangoIdx] = "mangos";
- $names[$pearIdx] = "pears";
-
- $cost[$bananaIdx] = 1.15; // initilize the price values
- $cost[$appleIdx] = 0.55;
- $cost[$orangeIdx] = 0.55;
- $cost[$mangoIdx] = 1.90;
- $cost[$pearIdx] = 0.68;
-
- $quantity[$bananaIdx] = 1; // initilize the quantity values
- $quantity[$appleIdx] = 3;
- $quantity[$orangeIdx] = 4;
- $quantity[$mangoIdx] = 1;
- $quantity[$pearIdx] = 2;
-
- return(%numFruitTypes);
- }
-
- function addEmUp(%numFruitTypes)
- // ------------------------------------------------------------------------
- // Add all prices of different fruit types to get a full total cost
- //
- // PARAMETERS: %numFruitTypes ûthe number of different fruit that are tracked
- //
- // RETURNS: total cost of all fruit
- //
- // ------------------------------------------------------------------------
- {
- %total = 0;
- for (%index = 0; %index < %numFruitTypes; %index++)
- {
- %total = %total + ($quantity[%index]*$cost[%index]);
- }
- return %total;
- }
-
-
- // ------------------------------------------------------------------------
- // countEm
- //
- // Add all quantities of different fruit types to get a full total
- //
- // PARAMETERS: %numFruitTypes ûthe number of different fruit that are tracked
- //
- // RETURNS: total of all fruit types
- //
- // ------------------------------------------------------------------------
- function countEm(%numFruitTypes)
- {
- %total = 0;
- for (%index = 0; %index < %numFruitTypes; %index++)
- {
- %total = %total + $quantity[%index];
- }
- return %total;
- }
-
- function runTwotyFruity()
- // ------------------------------------------------------------------------
- // Entry point for program. This program adds up the costs
- // and quantities of selected fruit types and outputs the results to
- // the display. This program is a variation of the program FruitLoopy
- //
- // ------------------------------------------------------------------------
- {
- //
- // ----------------- Initialization ---------------------
- //
-
- %numFruitTypes=InitializeFruit(); // set up fruit arrays and variables
- %numFruit=0; // always a good idea to initialize *all* variables!
- %totalCost=0; // (even if we know we are going to change them later)
-
- //
- // ----------------- Computation ---------------------
- //
-
- // Display the known statistics of the fruit collection
- for (%index = 0; %index < %numFruitTypes; %index++)
- {
- echo("Cost of " @ $names[%index] @ ":$" @ $cost[%index]);
- echo("Number of " @ $names[%index] @ ":" @ $quantity[%index]);
- }
-
- // count up all the pieces of fruit, and display that result
- %numFruit = countEm(%numFruitTypes);
- echo("Total pieces of Fruit:" @ %numFruit);
-
- // now calculate the total cost
- %totalCost = addEmUp(%numFruitTypes);
- echo("Total Price of Fruit:$" @ %totalCost);
- }
-