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.