Lab (4)
Functions, More of if .. else if ... else, while loop
Scope of variables

The goal is to:
Learn how functions work
=================================================
Preparations
Before we begin this lab, please change your current directory to cs1440 or 1440 (whichever you have), and in that directory, create the lab4 directory and change to that directory.  You will do all your work in the lab4 directory. (% cd cs1440 ... then %mkdir lab4,  .... then %cd lab4)

Part 1. Predefined functions
C++ comes with libraries of defined functions.  You can use these functions in your program by properly making a call to them.  A good example of these type of functions is the sqrt function that we used to solve quadratic equation.  For illustration, we look at Program (4.1) in which we use a new predefined function called pow:

// **************************************************************
// Program (4.1)
// This following C++ code illustrates the predefined functions;
// pow(x,n), sqrt(x)
#include<iostream.h>
#include<math.h>
#include<stdlib.h>
int main()
{

double x, z;
cout << "enter a value for x, and I will compute \n";
cout << "Its square root in two different ways, and I bet \n";
cout << "I get it right either way ... \n";

cin >> x;

//Here is the first method
z = pow(x,0.5);
cout << "Square root of  " << x;
cout << " computed using pow function is " << z << "\n";

//Here is the second method
z = sqrt(x);
cout << "Square root of  " << x;
cout << " computed using sqrt function is " << z << "\n";

return 0;
}
// ************************************************************

`In the above program you included the math.h library:`
`#include <math.h>`
This library allows you to use the following functions:
 Name Description Type of arguments Type of Value Returned Example Value sqrt square root double double sqrt(4.0) 2.0 pow powers double double pow(2.0,3.0) 8.0 abs  fabs absolute value for integer  absolute value for double int  double int  double abs(-7), abs(7)  fabs(-7.5) 7  7.5 ceil ceiling (round up) double double ceil(3.2) 4.0 floor floor (round down) double double floor(3.8) 3.0

In addition to the above functions, you can also use, sin(x), cos(x), tan(x), asin(x), acos(x), atan(y,x), atan2(x), sinh(x), cosh(x), tanh(x), exp(x), log(x),log10(x). There might be some more functions that I haven't listed.

Please note that type of the parameters and function itself is very important.  Without proper type definition, your program will generate errors or wrong answers.  Also note that all angles for trigonometric functions are expressed in radians.

In the above code, we used the pow and sqrt predefined functions.  As you can see in the above table, these two are of type double and for that is reason we have defined x and z as double.

Part 2. User Defined Functions
In this part, we will define our own function.  We define a function to make our program more efficient, better readable, and reusable.  The description of a  function has two parts, function prototype (function declaration) and function definition.  The following code solve a physics problem.  The problem is to find the distance that a car has traveled after some t seconds.  The motion is a non uniform motion, i.e.; the car moves with an acceleration, a. As you may remember, following is the equation for the distance for this type of motion:
x = (1/2) at2 + (v0)t + x0
where, t is time in second, a is acceleration in (m/sec2), and x0 is the initial distance (just in case the car didn't start from the 0).

// ********************************************************
// Program (4.2)
// This program computes the distance traveled by an object in a non uniform
// motion based on:
//                     x = (1/2)a.t^2 + (v0)t + x0
// WITHOUT USING FUNCTION
#include<iostream.h>
#include<math.h>

int main()
{
double t, x0, v0,a, x;

cout << "Enter time of travel, t,  in (sec) \n";
cin >> t;
cout << "Enter acceleration, a, in (m/sec^2) \n";
cin >> a;
cout << "Enter initial distance, x0, in (m) \n";
cin >> x0;
cout << "Enter initial velocity, v0, in (m/s) \n";
cin >> v0;

x = (0.5) * a * pow(t,2.0) + (v0)*t + x0;

cout << "With initial distance " << x0 << " (m) \n";
cout << "and initial velocity " << v0 << " (m/s) \n";
cout << "and acceleration " << a << " (m/s^2) \n";
cout << "The object will travel " << x;
cout << " (m) after " << t << " (sec) \n";

return 0;
}
// ****************************************************

Now let's write a function to compute the distance:
// ****************************************************
// Program (4.3)
// This program computes the distance traveled by a car in a non uniform
// motion based on:                    x = (1/2)a.t^2 + (v0)t + x0
// USING A FUNCTION
#include<iostream.h>
#include<math.h>

double Distance(double t, double a, double x0, double v0)
// This function computes the distance traveled in a non uniform motion.
// t is traveled time, a is the acceleration, and x0 is the initial
// distance and v0 is the initial velocity
{
double x;
x = (0.5) * a * pow(t,2.0) + (v0)*t + x0;
return x;
}

int main()
{
double t, x0, v0, a, x;

cout << "Enter time of travel, t,  in (sec) \n";
cin >> t;
cout << "Enter acceleration, a, in (m/sec^2) \n";
cin >> a;
cout << "Enter initial distance, x0, in (m) \n";
cin >> x0;
cout << "Enter initial velocity, v0, in (m/s) \n";
cin >> v0;

x = Distance(t, a, x0,v0);

cout << "With initial distance " << x0 << " (m), \n";
cout << "initial velocity of " << v0 << " (m/s), \n";
cout << "and acceleration " << a << " (m/s^2) \n";
cout << "The object will travel " << x;
cout << " (m) after " << t << " (sec) \n";

return 0;
}
// ***************************************************************************

In the second program, we used a function, Distance, to compute the distance for us.  Note that Distance is a function of type: double.  How do we know that? The function prototype (declaration)

double Distance(double t, double a, double x0, double v0)

will tell us that.  This function is taking t, a, x0, and v0 as its arguments.  Notice that these variables are defined as double in the main program.  Also, they must be received by the function as double, as you can see that is true in the (double t, double a, double x0, double v0) part of the function prototype.

IT IS CRITICALLY IMPORTANT THAT WE CORRECTLY ASSIGN THE FUNCTION TYPE AND IT ARGUMENTS.

As you can see in the main program knows what the Distance(t,a,x0,v0) is when it gets to the line:
x = Distance(t, a, x0,v0);

How does it know it?  We defined the whole function right before the main function.  There is another was of telling the main function what to expect when it sees the distance function.  Here is how:

// ******************************************************
// Program (4.4)
// This program computes the distance traveled by a car in a non uniform
// motion based on:                   x = (1/2)a.t^2 + (v0)t + x0
// USING A FUNCTION, Function is placed after the main
#include<iostream.h>
#include<math.h>

// Tell the main what to to expect when it sees the following function(s)
double Distance(double t, double a, double x0, double v0);

int main()
{
double t, x0, v0, a, x;

cout << "Enter time of travel, t,  in (sec) \n";
cin >> t;
cout << "Enter acceleration, a, in (m/sec^2) \n";
cin >> a;
cout << "Enter initial distance, x0, in (m) \n";
cin >> x0;
cout << "Enter initial velocity, v0, in (m/s) \n";
cin >> v0;

x = Distance(t, a, x0,v0);

cout << "With initial distance " << x0 << " (m), \n";
cout << "initial velocity of " << v0 << " (m/s), \n";
cout << "and acceleration " << a << " (m/s^2) \n";
cout << "The object will travel " << x;
cout << "(m) after " << t << " (sec) \n";

return 0;
}

double Distance(double t, double a, double x0, double v0)
// This function computes the distance traveled in a non uniform motion.
// t is traveled time, a is the acceleration, x0 is the initial
// distance, and v0 is the initial velocity
{
double x;
x = (0.5) * a * pow(t,2.0) + (v0)*t + x0;
return x;
}
// ****************************************************************

Part 3. All Pieces Together
In the following program, we use while loop, if .. else if ... else, and function.

// ****************************************************************
// Program (4.5)
// The following program will ask you to enter 5 numbers.  Then, it computes the sum
// of those numbers and will compute the average and also the square root of sum or
// display a message when it cant do it

#include<iostream.h>
#include<math.h>

int Sum(int sum_x, int x)
// This function adds x to the previous sum of x values and return
// the result
{
return sum_x += x;
}

int main()
{

int x, i;
int N = 5;  // number of values to receive from the keyboard
int sum_x = 0;  // variable to hold the sum of all x values,
//Note: initialized to 0
double z;

i = 0;
while(i < N){
cout << "Input a integer \n";
cin >> x;
sum_x = Sum(sum_x, x);
cout << "The sum so far is " << sum_x << " \n";
i++;
}

if( (sum_x >= 0) && (sum_x <10) ){
cout << "Sum of input values is between 0 to 10 \n";
cout << "I will multiply it by 10";
cout << "and will compute the square root \n";
z = sqrt(double(sum_x));
cout << "The sum is " << sum_x;
cout << " and the square root is " << z << "\n";
}
else if(sum_x >= 10){
cout << "Sum of input values is larger than or equal to 10 \n";
z = sqrt(double(sum_x));
cout << "The sum is " << sum_x;
cout << " and the square root is " << z << "\n";
}
else{
cout << "Sum is a negative value \n";
cout << "I can't compute a square root \n";
cout << "The sum is " << sum_x << "\n";
}
return 0;
}
// ******************************************************************
IN LAB HOMEWORK
You will finish this part in the lab to receive the lab grade.
Modify program 4.4 (call your new code p46.C) to use the following equation to find the distance:
x = (v^2 - v0^2)/2a
where
v : is the final speed (m/s)
v0: is the initial speed (m/s)
a : is the accelaration (m/s^2)
Test your code for the following three cases:
1) v = 40 (m/s), v0 = 120 (m/s), and a = -2 (m/s^2)
2) v = 160 (m/s), v0 = 40 (m/s), and a = 2 (m/s^2)
3) v = 160 (m/s), v0 = 40 (m/s), and a = 0 (m/s^2)