0% found this document useful (0 votes)
47 views29 pages

Day3 PDF

This document discusses algorithms and how to write programs to calculate algorithms. It begins with defining an algorithm as a step-by-step set of instructions to solve a problem. Examples are provided, such as turning on a lamp and making a peanut butter and jelly sandwich. The document then demonstrates how to write an algorithm to calculate body mass index (BMI) with explicit steps and variables. It concludes by showing the C++ code to write a function that calculates BMI by taking in a height and weight variable and returning the BMI value.

Uploaded by

nisascr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views29 pages

Day3 PDF

This document discusses algorithms and how to write programs to calculate algorithms. It begins with defining an algorithm as a step-by-step set of instructions to solve a problem. Examples are provided, such as turning on a lamp and making a peanut butter and jelly sandwich. The document then demonstrates how to write an algorithm to calculate body mass index (BMI) with explicit steps and variables. It concludes by showing the C++ code to write a function that calculates BMI by taking in a height and weight variable and returning the BMI value.

Uploaded by

nisascr
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

CS53-C: Introduction to C++

C. Shaun Wagner Algorithms Writing a Program

Algorithms

Introduction

What is an algorithm?

Introduction

What is an algorithm? A step-b -step set o! e"plicit instructions !or sol#ing a problem.

Introduction

What is an algorithm? A step-b -step set o! e"plicit instructions !or sol#ing a problem. $. Wash %. &inse 3. &epeat

Introduction

What is an algorithm? A step-b -step set o! e"plicit instructions !or sol#ing a problem. $. Place 'e in loc'. $. Place the sil#er 'e in the top loc'.

(lo) Charts

Algorithm !or turning on a lamp:


*urn s)itch to +on+ position Is there light? -o -o Plug in lamp

,ou are done

,es

Plugged in? ,es

Change bulb

(lo) Charts

Algorithm !or turning on a lamp:


*urn s)itch to +on+ position Is there light? -o -o Plug in lamp

,ou are done

,es

Plugged in? ,es We can )rite another algorithm

Change bulb

(lo) Charts

Algorithm !or turning on a lamp:


*urn s)itch to +on+ position Is there light? -o -o Plug in lamp

,ou are done

,es

Plugged in?

.oes it al)a s )or'?

,es Change bulb

(lo) Charts

Algorithm !or turning on a lamp:


*urn s)itch to +on+ position Is there light? -o -o Plug in lamp

,ou are done

,es

Plugged in?

.oes it al)a s )or'?

,es Change bulb

(lo) Charts

Algorithm !or turning on a lamp:


*urn s)itch to +on+ position Is there light? -o -o Plug in lamp

,ou are done

,es

$. *urn on s)itch Plugged in? %. I! light is on/ 0uit. ,es 3. I! lamp is plugged in/ go to 5. 1. Plug in lamp. 2o to %. 5. Change bulb. 2o to %.

Change bulb

Step-b -Step Algorithm

3a'ing a peanut butter and 4ell sand)ich: 5as ans)er: Put peanut butter and 4ell on bread.

Step-b -Step Algorithm

3a'ing a peanut butter and 4ell sand)ich: Precondition: $. %. 3. ... Postcondition: Sand)ich is read to eat.

Step-b -Step Algorithm

3a'ing a peanut butter and 4ell sand)ich: Precondition: We ha#e peanut butter/ 4ell / and t)o slices o! bread. $. %. 3. ... Postcondition: Sand)ich is read to eat.

Step-b -Step Algorithm

3a'ing a peanut butter and 4ell sand)ich: Precondition: We ha#e peanut butter/ 4ell / and t)o slices o! bread. $. %. 2roup Pro4ect (ill in the steps )ith e"plicit instructions 3. ... Postcondition: Sand)ich is read to eat.

Step-b -Step Algorithm

3a'ing a peanut butter and 4ell sand)ich: Precondition: We ha#e peanut butter/ 4ell / and t)o slices o! bread. $. Place slices o! bread on plate/ side b side. %. Spread peanut butter on one slice o! bread. 3. Spread 4ell on the other slice o! bread. 1. Pic' up one slice and turn it o#er. 5. Place slice being held on top o! other slice. Postcondition: Sand)ich is read to eat.

Step-b -Step Algorithm

3a'ing a peanut butter and 4ell sand)ich: Precondition: We ha#e peanut butter/ 4ell / and What plate? t)o slices o! bread. $. Place slices o! bread on plate/ side b side. 6o)? %. Spread peanut butter on one slice o! bread. Which one? 3. Spread 4ell on the other slice o! bread. 1. Pic' up one slice and turn it o#er. 5. Place slice being held on top o! other slice. Postcondition: Sand)ich is read to eat.

Step-b -Step Algorithm

Calculating 7od 3ass Inde": Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. %. 3. ... Postcondition: 73I is calculated.

Step-b -Step Algorithm

Calculating 7od 3ass Inde": Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. %. 3. ... Postcondition: 73I is calculated.

Step-b -Step Algorithm

Calculating 7od 3ass Inde": Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. %. .i#ide the )eight b ... 3. ... Postcondition: 73I is calculated.

Step-b -Step Algorithm

Calculating 7od 3ass Inde": Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. Call this height%. %. .i#ide the )eight b height%. Call this 73I. 3. ... Postcondition: 73I is calculated.

Step-b -Step Algorithm

Calculating 7od 3ass Inde": Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. Call this height%. %. .i#ide the )eight b height%. Call this 73I. Postcondition: 73I is calculated.

Writing a Program

Writing a Program
Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. Call this height%. %. .i#ide the )eight b height%. Call this 73I. Postcondition: 73I is calculated.
double BMI() { }

Writing a Program
Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. Call this height%. %. .i#ide the )eight b height%. Call this 73I. Postcondition: 73I is calculated.
double BMI(double height, double weight) { }

Writing a Program
Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. Call this height%. %. .i#ide the )eight b height%. Call this 73I. Postcondition: 73I is calculated.
double BMI(double height, double weight) { double height2 = height * height; }

Writing a Program
Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. Call this height%. %. .i#ide the )eight b height%. Call this 73I. Postcondition: 73I is calculated.
double BMI(double height, double weight) { double height2 = height * height; double bmi = weight/height2; }

Writing a Program
Precondition: 6eight is pro#ided in meters. Weight is pro#ided in 'ilograms. $. S0uare the height. Call this height%. %. .i#ide the )eight b height%. Call this 73I. Postcondition: 73I is calculated.
double BMI(double height, double weight) { double height2 = height * height; double bmi = weight/height2; return bmi; }

-e"t Wee'

&ead section $.%/ pages 8-$5. -e"t topic: 9ariables

You might also like