Wednesday, October 16, 2019

Junior 2 ICA -For Loops

The robotic team has built a robot that collects pollutants and takes them to a collector bin.  The bin holds 1000 pieces of trash.  The human worker separates the trash into two categories:  Recycle and Reuse.  Write a program that calculates and states the amount of pieces of pollutants for recycle and the amount for reuse.

Post your solution as a comment.

Tuesday, October 15, 2019

Solution to Algorithm 2 Test #2

'the student council is selling tickets to the dance as follows'
'Junior Dance - $5.00'
'Senior Dance - At door - $15.00  - Advance -$10.00'
'students need an ID for Senior Dance'
'Determine and output the total for a student purchasing ticket(s)'

'This algorithm is written as the solution for algorithm #2 on Test 2'

jd = 5      'jd represents junior dance fee'
sda = 10  'sda represents senior dance fee advanced purchase'
sdd = 15  'sdd represents senior dance fee at the door'

print "Enter 1 for Junior dance ticket purchase"
print "Enter 2 for Senior dance ticekt purchase"
print "Enter 3 for combination purchases"
input num

if (num = 1) then
   print "enter the number of junior tickets needed"
   input jnum
   total = jnum * jd
   print "Your total is $", total
end if

if (num = 2) then
   print "Enter 1 for advanced purchase or 2 for tickets to pay for at the door"
   input ans
   if (ans = 1) then
      print "enter the number of senior tickets needed"
      input snum
      total = snum * sda
      print "Your total is $", total
  else
      if (ans = 2) then
         print "enter the number of senior tickets you want to reserve"
         input snum2
         total = snum2 * sdd
         print "We are reserving your tickets and you may pick up at the door upon payment."
         print "Your total will be $", total
      end if
   end if
end if

if (num = 3 ) then
   print "enter the number of junior tickets needed"
   input jnum
   jtotal = jnum * jd
 
   print "Enter 1 for advanced purchase or 2 for tickets to pay for at the door"
   input ans
   if (ans = 1) then
      print "enter the number of senior tickets needed"
      input snum
      stotal = snum * sda
   else
      if (ans = 2) then
         print "enter the number of senior tickets you want to reserve"
         input snum2
         sdtotal = snum2 * sdd
         print "We are reserving your tickets and you may pick up at the door upon payment."
      end if
   end if
   total = jtotal + stotal + sdtotal
   total2 = jtotal + stotal
   print "Your overalltotal is $", total
   print total2 , " is due now and ", sdtotal,"is due at the door"
end if

Print "Thank you for supporting the Student Council"
input key
 

Monday, October 7, 2019

Test Outline Updated

Junior 1 Test 2 - Theory - Day 9
Junior 2 Test 2 - Theory -  Day 8

Due to an unexpected change in schedule for Day 9, the test will be a Short Test, meaning that it is timed for 40 minutes or less.

The outline is updated as follows:

Part 1 - Multiple Choice (5 points)
5 questions

Part 2 - Structured Questions (45 points)
1 theory question based on control structures (5 points)
2 conditional algorithms (20 points each)



Check this out...


Thursday, October 3, 2019

Conditional Algorithm Practice Problems

1. Write an algorithm that prompts the user to enter two values stored in A and B.  The algorithm should determine the larger value and store it in Max and then output Max.

2. The cost to ride the bus is as follows:
              Ladyville $2.00
              Sandhill $3.00
              Carmelita $4.00
              Orange Walk $5.00
   Write an algorithm that determines and shows the cost for a person to travel to their destination.

3.  The bank changes simple interest rates of 20% on loans valuing more than $10,000.00 and 15% on loans valuing $10,000.00 or less.  Write an algorithm that prompts the user to enter their loan value.  The algorithm should determine and calculate the interest value and the overall loan total inclusive of the interest.  For loans that are $10,000.00 or less, customers must pay the loan in equal monthly payments for 5 years.  For larger loans, customers must pay the loan in equal monthly payments for 8 years.  The algorithm should also state the monthly payment of the customer, based on their loan value.

Thursday, September 19, 2019

Jr Programming Quiz - IT Majors ONLY


Jr Programming
Quiz
Topic:  User defined functions
September 19, 2019

Instructions:  Using the programming language of C++, write a program to solve the given problem.  Ensure to include internal documentation.

Create a salary calculator for a sales company that has ten employees.  Each calculation should be completed as an individual function and called upon in the main function.  Calculations for each employee salary includes:
1.     Basic monthly salary depends on which department the employee is in. 
a.      Management earns $5000.00 per month.  There is only one manager.
b.     Office Administration earns $2,000.00 per month.  There is only one office administrator.
c.      Sales earns $1,500.00 per month plus 5% commission on all monthly sales.

2.     A bonus is paid to all employees if they work on Saturdays.  It is optional, so the bonus is $200.00 per Saturday added to their gross salary.

3.     Employees earning more than $2,500.00 monthly gross salary pays 25% income tax.  Income tax is only on taxable income, meaning the gross income that is more than $2500.00 monthly.

4.     All employees pay social security of 10% of gross salary.

5.     Employees have the option to have one deduction made from salary.

Your program is to calculate and show the monthly net salary for the employees.

Thursday, September 12, 2019

Jr IT Majors ONLY

Functions in C++

Functions are used to provide modularity to a program. Creating an application using function makes it easier to understand, edit, and check errors.

Basic Syntax for using Functions in C++

Here is how you define a function in C++,
return-type function-name(parameter1, parameter2, ...)
{
    // function-body
}
  • return-type: suggests what the function will return. It can be int, char, some pointer or even a class object. There can be functions which does not return anything, they are mentioned with void.
  • Function Name: is the name of the function, using the function name it is called.
  • Parameters: are variables to hold values of arguments passed while function is called. A function may or may not contain parameter list.
    // function for adding two values
    void sum(int x, int y)
    {
        int z;
        z = x + y;
        cout << z;
    }
    
    int main()
    {
        int a = 10;
        int b = 20;
        // calling the function with name 'sum'
        sum (a, b);
    }
    Here, a and b are two variables which are sent as arguments to the function sum, and x and y are parameters which will hold values of a and b to perform the required operation inside the function.
  • Function body: is the part where the code statements are written.

Declaring, Defining and Calling a Function

Function declaration, is done to tell the compiler about the existence of the function. Function's return type, its name & parameter list is mentioned. Function body is written in its definition. Lets understand this with help of an example.
#include < iostream>
using namespace std;

//declaring the function
int sum (int x, int y);

int main()
{
    int a = 10;
    int b = 20;
    int c = sum (a, b);    //calling the function
    cout << c;
}

//defining the function
int sum (int x, int y)
{
    return (x + y);
}
Here, initially the function is declared, without body. Then inside main() function it is called, as the function returns sumation of two values, and variable c is there to store the result. Then, at last, function is defined, where the body of function is specified. We can also, declare & define the function together, but then it should be done before it is called.

Calling a Function

Functions are called by their names. If the function is without argument, it can be called directly using its name. But for functions with arguments, we have two ways to call them,
  1. Call by Value
  2. Call by Reference

Call by Value

In this calling technique we pass the values of arguments which are stored or copied into the formal parameters of functions. Hence, the original values are unchanged only the parameters inside function changes.
void calc(int x);

int main()
{
    int x = 10;
    calc(x);
    printf("%d", x);
}

void calc(int x)
{
    x = x + 10 ;
}
10
In this case the actual variable x is not changed, because we pass argument by value, hence a copy of x is passed, which is changed, and that copied value is destroyed as the function ends(goes out of scope). So the variable x inside main() still has a value 10.
But we can change this program to modify the original x, by making the function calc() return a value, and storing that value in x.
int calc(int x);

int main()
{
    int x = 10;
    x = calc(x);
    printf("%d", x);
}

int calc(int x)
{
    x = x + 10 ;
    return x;
}
20

Call by Reference

In this we pass the address of the variable as arguments. In this case the formal parameter can be taken as a reference or a pointer, in both the case they will change the values of the original variable.
void calc(int *p);

int main()
{
    int x = 10;
    calc(&x);     // passing address of x as argument
    printf("%d", x);
}

void calc(int *p)
{
    *p = *p + 10;
}
20

Homework:  Create a program (using user-defined functions) that accepts two values, performs the additions, subtraction, multiplication and division of the values, and outputs the results.
DUE DAY 10

Friday, September 6, 2019

Assessment Notification Cycle 2

Junior 1
Day 7 - No Classes - National Holiday
Day 9 - Graded ICA - Simple Algorithm
Day 10 - Quiz 2 - Simple Algorithm (Practical)
Cycle 3 Day 2 - Test #1- Introduction to Programming; Simple Algorithm
*practice-problems listed below


Junior 2
Day 5 - HW2 collected; ICA - Simple Algorithm 
Day 8 - Quiz #2 - Simple Algorithm (Practical)
Day 9 - Test #1 - Introduction to Programming; Simple Algorithm
*practice-problems listed below


Jr. Networking
Day 6 - Graded Lab - Basic LAN and VLAN using 2960 switches
Day 7 - No Classes - National Holiday 


Jr. Programming
Day 9 - graded ICA - User define functions 
Day 10 - Test #1 - Program in C++ (user define functions)


PRACTICE-PROBLEMS

1. Bobby was given his meal's plus allowance for the week and is planning to spend equal amounts each day at the cafeteria.  Write an algorithm to help Bobby calculate and output his daily allowance.

2. Your school is having a school dance and the student council is selling tickets: Advance purchase is $10.00 for females and $12. 00 for males.  At the door purchase is $15.00 for females and $18.00 for males.  Write an algorithm that will tally and output the total income collected from the dance.  (simple algorithm)

3. Write an algorithm that calculates the net income of a husband and wife.  The husband is an engineer and makes a total of $4,500.00 per month as gross salary (salary before any deductions).  The wife works as a consultant and charges $100.00 per session.  Each of them pays 12% monthly to social security.  The wife pays a monthly loan of $350.00 and the husband has a mandatory deduction from his check 10% of his gross salary that is paid to the company's pension program.  Your algorithm should display the combined monthly net salary.  Net salary is the take home amount after all deductions.







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 xy, 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!