Finding Input and there types, Find output and there types.

Find the formulas ( note may be more than one)

What is return type of your function?

What are the arguments/parameters and what are there type?

What are you returning?

What is formulas needed to do computation inside?

// Prototype

//returnType functionName(argumentType argumentName)

int times (int a, int b);

int main(){

int x;

//usage of function

x = times(10,2);

return 0;

}
//Defining function

int times(int a, int b){

return a*b;

}

- Book Case Example
- Rock Paper Scissor Example
- Small Loop Example
- Calulator functions
- array Functions
- Small Function Example

×
## Book Case Example

Code is not complete it is what we got finished in class.

Set inventory for bookstore. Each book size has a

const price for small, large,medium. Each book store

choose how many books of each size it has. Need to keep

track of total inventory cost.

As Long as there is a customer who wants to buy we will

sell or until there are no books left. Each customer will

tell you what type of book they want to buy and how many.

Always ask customer if they want to buy more books. After

done of customer buying books give their total cost. Ask

for payment. Tell the user if they gave correct money, if

so then given them the change. Output the remaining

inventory after each customer.

```
#include < stdio.h >
```

#include < iostream >

#include < iomanip >

using namespace std;

const double PRICE_BIG = 19.99;

const double PRICE_MED = 12.55;

const double PRICE_SMALL = 5.60;

int main(int argc, char **argv)

{

int num_big, num_med, num_small,how_many;

double inventory_cost=0.0,customer_cost=0.0;

char customers,sell,book_type;

cout << 'Welcome to book store:' << endl;

cout << 'How many small books are in the inventory: ' << endl;

cin >> num_small;

cout << 'How many medioum books are in the inventory: ' << endl;

cin >> num_med;

cout << 'How many big books are in the inventory: ' << endl;

cin >> num_big;

inventory_cost = PRICE_BIG*num_big + PRICE_MED*num_med + PRICE_SMALL*num_small;

cout<< 'Any customers in store '<< endl;

cin >> customers;

while ( (customers=='y') ||(customers =='Y'){

do {

cout << 'Do you want to sell' << endl;

cin >> sell;

if( sell =='y'|| sell=='Y'){

cout<< 'what type';

cin>> book_type;

cout << ' how many of that type' << endl;

cin >> how_many;

switch(book_type){

case 'l':

case 'b':

case 'B':

case 'L':

if( how_many > num_big){

cout << 'we only have : ' << num_big;

cout << 'Please choose another amount';

cin >> how_many;

}

if( how_many <= num_big){

num_big = num_big -how_many;

customer_cost += how_many*PRICE_BIG;

}

break;

case 'm':

case 'M':

if( how_many > num_med){

cout << 'we only have : ' << num_med;

cout << 'Please choose another amount';

cin >> how_many;

}

if( how_many <= num_med){

num_med = num_med -how_many;

customer_cost += how_many*PRICE_MED;

}

break;

case 's':

case 'S':

if( how_many > num_small){

cout << 'we only have : ' << num_small;

cout << 'Please choose another amount';

cin >> how_many;

}

if( how_many <= num_small){

num_med = num_small-how_many;

customer_cost += how_many*PRICE_SMALL;

}

break;

default:

cout << 'we dont sell that type of book';

break;

}

}while( sell =='y' || sell = 'Y');

if( (num_big ==0) && (num_small==0) && (num_med==0){

cout << 'sorry no more books left';

break;

}

}

return 0;

}

×
## Rock Paper Scissor

You have two players who have 3 choices rock, paper or scissor.

Rock bet scissor, scissor bet paper, and paper bets rock. This will be best of three games.

If the players tie then that will not count in the best of three. Will ask at the end if

the users want to play another round

×
## Small Loop Examples

We want to print out the sum of all even numbers from user.
The user will enter a number from 1-100.

Output the factorial of a number given by a user

A user choose a size of fabric in square ft. the user will also choose the shape the fabric is to cover.

The shapes are square, triangle, circle, and rectangle. The users will also choose the size of sides.

If the size of the fabric is less than the square ft of the shape with the users give dimesions. The users

will recieve a message that it can not make that shape with the given fabric. If the fabric is less than

or equal to the square ft of the shape with the users given dimesions. The user will recieve a message

that it can make the shape with the fabric and how much is left over.

` int num,i; `

int sum=0;

cout << "enter a number in from 1-100" <
cin >> num;

for(i= 0 ; i< num; i+=2){

sum+=i;

}

cout<< " Sum of all even numbers" <

```
int num,i;
```

int fac=1;

cout << "enter a number in from 1-100" <
cin >> num;

for(i= 1 ; i< num; i++){

fact*=i;

}

cout<< " Sum of all even numbers" <

The shapes are square, triangle, circle, and rectangle. The users will also choose the size of sides.

If the size of the fabric is less than the square ft of the shape with the users give dimesions. The users

will recieve a message that it can not make that shape with the given fabric. If the fabric is less than

or equal to the square ft of the shape with the users given dimesions. The user will recieve a message

that it can make the shape with the fabric and how much is left over.

```
double sqft, sizelength, sizeWidth,base,height,radius,formulaOutput;
```

string shape;

cout << "square foot of fabric"<
cin>> sqft;

cout << "what shape(square, triangle, cirle or rectangle"<
cin>> shape;

if(shape =="square"){

cout << " what is length of sizes"<
cin >> sizelength;

formulaOutput = sizeLength*sizelength;

}

elseif(shape =="rectangle"){

cout << " what is length of size"<
cin >> sizelength;

cout << " what is width of size"<
cin >> sizeWidth;

formulaOutput = sizeLength*sizeWidth;

}

elseif(shape =="triangle"){

cout << " what is height of size"<
cin >> height;

cout << " what is base of size"<
cin>> base;

formulaOutput = .5*base*height;

}

elseif(shape =="circle"){

cout << " what is radius of size"<
cin>> radius;

formulaOutput = 3.14159 * radius*radius;

}

if( sqft < formulaOutput){

cout << " cant make shape with that fabric" <
}

else{

cout << " can make "<< shape << " With that square foot have this much fabric left over" << sqft-formulaOutput<
}

×
## Calculator function

Addition: Takes in 2 arguments of double and returns the sum of the two
multiplcation: Takes in 2 arguments of double and returns the times of the two
divison: Takes in 2 arguments of double and returns the division of the two
subtraction: Takes in 2 arguments of double and returns the sum of the two
calc: takes in argument of type string that is the type of calcuation, if its

not valid do nothing, if it valid, call the function for that cacluation string with

getting the needed arguments from users, tell user the output of that function and return

nothing to the user(void)

not valid do nothing, if it valid, call the function for that cacluation string with

getting the needed arguments from users, tell user the output of that function and return

nothing to the user(void)

×
## Arrays and array Functions

fill an array of size 20 will animals(strings) from users
create a function that takes in an array of size 15 and fill it with random values from 0-100
create a function that finds the average of a array of type double and returns it to the user
create a function that finds the sum of an array of type ints and returns it to the user

×
## Small Function

Given two int values, a and b, return True if either one is 6. Or if their sum or difference is 6
You are driving a little too fast, and a police officer stops you. Write code to compute the result,

encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0.

If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless

it is your birthday -- on that day, your speed can be 5 higher in all cases.

caught_speeding(60, False) → 0

caught_speeding(65, False) → 1

caught_speeding(65, True) → 0

Given an array of ints length 3, return an array with the elements "rotated left" so {1, 2, 3} yields {2, 3, 1}.
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each).

Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't

be done. the parameters are number of big bars, number of small bars and the goal of chocolate of type int

encoded as an int value: 0=no ticket, 1=small ticket, 2=big ticket. If speed is 60 or less, the result is 0.

If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless

it is your birthday -- on that day, your speed can be 5 higher in all cases.

caught_speeding(60, False) → 0

caught_speeding(65, False) → 1

caught_speeding(65, True) → 0

Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't

be done. the parameters are number of big bars, number of small bars and the goal of chocolate of type int