0

0

2537 days ago,
787 views

PowerPoint PPT Presentation
2. 3.1 Building Programs from Existing Information. Reuse of existing programsDevelop program in stagesCompile along the wayKeep it smallComments to portray activities. 3. Assertion versus Definition. An assertion indicates the properties of a variable - it determines a sort (e.g., int, buoy, or singe) and a name. For instance, the accompanying explanations are substantial affirmations in C or C : int age,

Part 3: Top-Down Design with Functions and Classes Jason C. H. Chen, Ph.D. Educator of MIS School of Business Administration Gonzaga University Spokane, WA 99258 chen@gonzaga.edu

3.1 Building Programs from Existing Information Reuse of existing projects Develop program in stages Compile en route Keep it little Comments to depict activities

Declaration versus Definition An affirmation determines the properties of a variable - it indicates a sort (e.g., int, buoy, or roast) and a name. For instance, the accompanying articulations are substantial affirmations in C or C++: int age, check; skim compensation; burn name[34];/exhibit of characters

Declaration versus Definition (cont.) struct date { int day; int month; int year } birthday; If an announcement likewise dispenses capacity memory for the comparing substance, the assertion is additionally a definition .

Declaration versus Definition (cont.) The accompanying articulations are substantial presentations just: void compute_sum_ave/Function model (float, coast, /IN: qualities utilized as a part of calculation float& , //OUT: entirety of num1 and num2 float& ); //OUT: normal of num1 and Note that: glide &/CALL-BY-REFERENCE

FILE: Circle.cpp (p.107) #include <iostream> utilizing namespace sexually transmitted disease; int fundamental () {/Declaration segment/Local information const skim PI = 3.14159; drift span; glide territory; drift circum;/Executable Statements Section/Read range of circle cout << "Enter the circle sweep: "; cin >> span;/Compute region of circle region = PI * sweep * range;/Compute periphery of circle circum = 2 * PI * span;/Display zone and outline cout << "The zone of the circle is " << zone << endl; cout << "The boundary of the circle is " << circum << endl; return 0; } Sample outpu: Enter the circle span: 5.0 The zone of the circle is 78.5397 The perimeter of the circle is 31.4159 Press any key to proceed

/File: washers (p.110)/Computes the heaviness of a clump of level washers. #include <iostream> utilizing namespace sexually transmitted disease; int principle() { const skim PI = 3.14159; drift holeDiameter;/input - distance across of opening buoy edgeDiameter;/input - breadth of external edge glide thickness;/input - thickness of washer buoy thickness;/input - thickness of material utilized buoy amount;/input - number of washers made buoy weight;/yield - weight of washer clump coast holeRadius;/sweep of gap buoy edgeRadius;/span of external edge drift rimArea;/zone of edge buoy unitWeight;/weight of 1 washer/Get the internal width, external measurement, and thickness. cout << "Inner measurement in centimeters: "; cin >> holeDiameter; cout << "Outer distance across in centimeters: "; cin >> edgeDiameter; cout << "Thickness in centimeters: "; cin >> thickness;/Get the material thickness and amount made. cout << "Material thickness in grams per cubic centimeter: "; cin >> thickness; cout << "Quantity in clump: "; cin >> amount;

/Compute the edge region. holeRadius = holeDiameter/2.0; edgeRadius = edgeDiameter/2.0; rimArea = PI * edgeRadius * edgeRadius - PI * holeRadius * holeRadius;/Compute the heaviness of a level washer. unitWeight = rimArea * thickness * thickness;/Compute the heaviness of the group of washers. weight = unitWeight * amount;/Display the heaviness of the cluster of washers. cout << "The expected weight of the bunch is " << weight; cout << " grams." << endl; return 0; } Sample Output: Inner breadth in centimeters: 1.2 Outer measurement in centimeters: 2.4 Thickness in centimeters: 0.1 Material thickness in grams per cubic centimeter: 7.87 Quantity in cluster: 1000 The normal weight of the clump is 2670.23 grams

3.2 Library Functions Goal of Structured Programming Error free code Reusability Don't reevaluate the wheel C ++ gives accumulation of capacities Organized in Libraries Library illustrations Table 3.1

C++ Math Library Functions in the Math library sqrt cos sin pow Examples Table 3.1 Function use in Assignments y = sqrt (x); sqrt is capacity name x is capacity contention Activated by a "work call" Result of execution is allocated to variable y

Result is 4.0 C++ Math Library (cont) Function sqrt as a "discovery" Square root calculation X is 16.0

C++ Library Functions We can adequately use existing capacities by figuring out how to peruse work models with the preconditions and postconditions. Illustration model (or mark) twofold sqrt(double x)/PRE: x >= 0.0/POST: Returns the square foundation of x.

Preconditions and Postconditions Comments that speaks to an agreement between the practitioner of a capacity and the client (customer) of that capacity. We'll take a gander at two such remarks: Precondition : What the capacity requires. Postcondition : What the capacity will do if the precondition is met.

Preconditions and Postconditions The preconditions are the conditions that must be valid before the capacity can effectively satisfy the guaranteed postconditions. Illustration (Precondition condenses to PRE: twofold sqrt(double x);/PRE: x >= 0/POST: Returns square base of contention X

/File: squareRoot.cpp (p.114)/Performs three square root calculations #include <cmath>/sqrt work #include <iostream>/i/o capacities utilizing namespace sexually transmitted disease; int principle() { glide first;/input: one of two information values skim second;/input: second of two information values drift reply;/yield: a square root esteem/Get first number and show its square root. cout << "Enter the principal number: "; cin >> first; answer = sqrt (first); cout << "The square foundation of the main number is " << answer << endl;/Get second number and show its/square root. cout << "Enter the second number: "; cin >> second; answer = sqrt (second); cout << "The square base of the second number is " << answer << endl;/Display the square foundation of the entirety of/first and second. answer = sqrt (first + second); cout << "The square base of the entirety of both numbers is " << answer << endl; return 0; } Sample Output : Enter the primary number: 9 The square foundation of the principal number is 3 Enter the second number: 16 The square base of the second number is 4 The square base of the whole of both numbers is 5

Other Mathematical Library Functions See Table 3.1 (p.115)

Methodology A procedure is a physical usage of the sensible framework advancement life cycle that fuses: Step-by-step exercises for each stage. Individual and gathering parts to be played in every action Deliverables and quality norms for every action, and Tools and procedures to be utilized for every movement

Divide and Conquer A familiar adage, "in the event that you need to learn anything, you should not attempt to learn everything - at any rate not at the same time." Therefore, we ought to separate a framework into its subsystems so as to all the more effectively vanquish the issue and construct the bigger frameworks. By separating an expansive issue (framework) into all the more effectively oversaw pieces (subsystems), the examiner can streamline the critical thinking process. A procedure is an arrangement of connected exercises that take an info and change it to make a yield.

Draw a Draw a Draw Circle Triangle converging lines Draw a meeting baselines 3.3 Top-Down Design and Structure Charts stickFigure.cpp (p.122; 126) Original Problem Draw a Level 0 figure Level 1 Detailed subproblems Level 2

3.4 Functions without Arguments Functions utilized as a part of Top-Down Design principle() is only a capacity called by OS C++ program is a gathering of Functions top level capacity is known as the fundamental() bring down level capacities User Defined or Libraries Example StickFigure.cpp (p.125)

Three Major Components for the Function Prototype outside (before) the primary() Function Calls inside the fundamental() Function Definition outside (after) the primary()

Function: General Form/Function Prototype: void function_name ();/Function Calls : int principle() { ... function_name (); … }/Function Definitions: void function_name () {/nearby factor assertions BODY OF FUNCTION }

/File: stickFigure.cpp (p.125)/Draws a stick figure #include <iostream> utilizing namespace sexually transmitted disease;/Functions Prototype void drawCircle();/Draws a circle void drawTriangle();/Draws a triangle void drawIntersect();/Draws converging lines void drawBase();/Draws a level line/Function Definitions/Draws a circle void drawCircle() { cout << " * " << endl; cout << " * *" << endl; cout << " * " << endl; }/end drawCircle/Draws a triangle void drawTriangle() { drawIntersect(); drawBase(); }/end drawTriangle/Draws meeting lines void drawIntersect() { cout << "/\

SPONSORS

No comments found.

SPONSORS

SPONSORS