Wednesday, September 4, 2019

Junior 2 Homework

Class:  Information Technology
Date Administered: September 4, 2019
Date Due:  September 6, 2019
Submission Format:  Hardcopy of source code (printed or folder sheet)
To be graded.  10 points each

Instructions:  Write structured algorithms for the following problems:  (simple algorithms)

1.  Doctor Bob works for the local clinic part-time and gets paid $75.00 for each patient that he does a basic physical on, except for NHI patients.  For NHI patients, he gets paid $50.00 per patient.  Write an algorithm that calculates and shows the total that Doctor Bob should get paid at the end of a day at the clinic.

2. Write an algorithm that prompts the user to enter two values that are stored in variables A and B.  The algorithm should find and display the product and mean of the values.  

3. The city bus charges $1.00 for adults and $0.50 for children.  Write a program that calculates and outputs the total income made for a day.  Assume that the same amount of money is made daily and calculate and output the total collected for the week.

4. You and three of your friends are planning a trip to Xunantunich.  You have all agreed that you will share the cost evenly, including bus fare and food.  Write an algorithm that calculates and outputs the cost each of you will have to pay.  

Junior Majors ONLY

Functions





Functions allow to structure programs in segments of code to perform individual tasks.

In C++, a function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:

type name ( parameter1, parameter2, ...) { statements }
Where:
- type is the type of the value returned by the function.
- name is the identifier by which the function can be called.
- parameters (as many as needed): Each parameter consists of a type followed by an identifier, with each parameter being separated from the next by a comma. Each parameter looks very much like a regular variable declaration (for example: int x), and in fact acts within the function as a regular variable which is local to the function. The purpose of parameters is to allow passing arguments to the function from the location where it is called from.
- statements is the function's body. It is a block of statements surrounded by braces { } that specify what the function actually does.

Arguments passed by value and by reference

In the functions seen in earlier sessions, arguments have always been passed by value. This means that, when calling a function, what is passed to the function are the values of these arguments on the moment of the call, which are copied into the variables represented by the function parameters. For example, take:

1
2
int x=5, y=3, z;
z = addition ( x, y );


In this case, function addition is passed 5 and 3, which are copies of the values of x and y, respectively. These values (5 and 3) are used to initialize the variables set as parameters in the function's definition, but any modification of these variables within the function has no effect on the values of the variables x and y outside it, because x and y were themselves not passed to the function on the call, but only copies of their values at that moment.


In certain cases, though, it may be useful to access an external variable from within a function. To do that, arguments can be passed by reference, instead of by value. 

To gain access to its arguments, the function declares its parameters as references. In C++, references are indicated with an ampersand (&) following the parameter type, as in the parameters taken by duplicate in the example above.

When a variable is passed by reference, what is passed is no longer a copy, but the variable itself, the variable identified by the function parameter, becomes somehow associated with the argument passed to the function, and any modification on their corresponding local variables within the function are reflected in the variables passed as arguments in the call.


If instead of defining duplicate as:

 
void duplicate (int& a, int& b, int& c) 


Was it to be defined without the ampersand signs as:

 
void duplicate (int a, int b, int c)


The variables would not be passed by reference, but by value, creating instead copies of their values. In this case, the output of the program would have been the values of x, y, and z without being modified (i.e., 1, 3, and 7).


Efficiency considerations and const references

Calling a function with parameters taken by value causes copies of the values to be made. This is a relatively inexpensive operation for fundamental types such as int, but if the parameter is of a large compound type, it may result on certain overhead. For example, consider the following function:

1
2
3
4
string concatenate (string a, string b)
{
  return a+b;
}


This function takes two strings as parameters (by value), and returns the result of concatenating them. By passing the arguments by value, the function forces a and b to be copies of the arguments passed to the function when it is called. And if these are long strings, it may mean copying large quantities of data just for the function call.

But this copy can be avoided altogether if both parameters are made references:

1
2
3
4
string concatenate (string& a, string& b)
{
  return a+b;
}


Arguments by reference do not require a copy. The function operates directly on (aliases of) the strings passed as arguments, and, at most, it might mean the transfer of certain pointers to the function. In this regard, the version of concatenate taking references is more efficient than the version taking values, since it does not need to copy expensive-to-copy strings.

On the flip side, functions with reference parameters are generally perceived as functions that modify the arguments passed, because that is why reference parameters are actually for.

The solution is for the function to guarantee that its reference parameters are not going to be modified by this function. This can be done by qualifying the parameters as constant:

1
2
3
4
string concatenate (const string& a, const string& b)
{
  return a+b;
}


By qualifying them as const, the function is forbidden to modify the values of neither a nor b, but can actually access their values as references (aliases of the arguments), without having to make actual copies of the strings.

Therefore, const references provide functionality similar to passing arguments by value, but with an increased efficiency for parameters of large types. That is why they are extremely popular in C++ for arguments of compound types. Note though, that for most fundamental types, there is no noticeable difference in efficiency, and in some cases, const references may even be less efficient!

Tuesday, September 3, 2019

J1 Homework Solutions

Junior 1 Homework Solution:
Note:  Your task today was to place your source code in the compiler to identify your errors.  Hopefully you were able to apply correct syntax to test your algorithm.
Below are my solutions for the four assigned problems.  As mentioned in class, we will go over them in our next session after your testing in the compiler is attempted.  We will focus heavily on problem number 3 to learn about symbols used in algorithms.
Remember, there is no one solution for a problem, but syntax must be adhered to.

Problem #1:  The cost to go to Altun Ha are as follows:  $5.00 for Belizean and $10.00 for non-Belizeans.  Write an algorithm that calculates and shows the total for a group going to Altun Ha.

Solution:

'This algorithm is written by M. Young on September 3, 2019 for Junior 1'
belizean = 5.00
non_bze = 10.00
print "Enter the number of Belizeans in the group"
input num1
print "Enter the number of Non-Belizeans in the group"
input num2
total = (num1 * belizean) + (num2 * non_bze)
print "The total cost for the group is", total
input key


Problem #2: Write an algorithm that asks the user to enter the diameter of a circle.  The algorithm should calculate and show the circumference of a circle using the formula circumference = 2 Pi R

Solution:
'This algorithm is written by M. Young on 9/3/19 for Junior 1'
pi = 3.14
print "Enter the diameter of a circle"
input diameter
R = diameter/2
C = 2*pi*r
print "The circumference of the circle is", C
input key


Problem #3: Write an algorithm that prompts the user to enter the name and age of three friends.  The algorithm should find and show the average age of the friends.

Solution:
'This algorithm is written by M. Young on 9/3/19 for Junior 1'
print "enter the name and age of the first friend"
input nam1
input age1
print "enter the name and age of the second friend"
input nam2
input age2
print "enter the name and age of the third friend"
input nam3
input age3
avg = (age1 + age2 + age3) /3
print "The average age of the three friends is", avg
input key

Problem #4:  Write an algorithm that asks the user to enter a number and store in X.  The algorithm should find and show the square and cube of the number.

Solution:
'This algorithm is written by M. Young on 9/3/19 for Junior 1'
print "enter a number"
input X
sq = X * X
cu = X * X * X
print "The square of the number is", sq
print "The cube of the number is", cu
input key