EECE 1080C / Programming for ECE
Summer 2022
Laboratory 4: Global Functions Practice
qq1703105484
Plagiarism will not be tolerated:
Topics covered:
- function creation and call statements (emphasis on global functions)
Objective:
- To practice program development by using programming operators. The student will create several short programming assignments to develop a solid understanding of how to use global functions and call statements in C++
Highlights:
- Please access the laboratory assignment via the Canvas link. The descriptions for each problem are contained within this document.
- Each program should start with the following template
#include //array utilities
#include
#include
#include
#include
#include
#include
using namespace std;
int main() {
return 0;
}
- Submit on Canvas using the assignment submission links
Grading
- Each part should be worked on separately. You will need a separate file for each part of this assignment when working within your IDE.
- While each program can be completed without functions, the objective is to practice using global functions
- Using a separate .hpp file is encouraged, but not required for these applications.
Rubric: 60 points
- Part A = 15 points
- Part B = 15 points
- Part C = 15 points
- Part D = 15 points
Tasks:
- Electric Field - Pass By Value Exercise
Background
Gauss's Law states that an electric field exists whenever there is a net electric charge(Q) around an conducting sphere with conductivity (ke). At any distance (r) for a sphere of radius (a), the electric field is
Er=keQra3;rkeQ1r2;r≥a
Let's assume that
Q=3.5 coulombs, ke=15.8 Newtons meters2coulombs2
Write a program that computes the electric field for a user specified value of radius (a) and distance (r). While the program can be written entirely without functions, use the guidelines below to create the required implementation.
Your program should include the following
Global Variables
- define const double variables for the charge and conductivity using the values above
Prototype Declaration
- double askValue(string)
- double eFieldFunction(double, double)
- void printAll(double, double, double)
int Main Scope
- declare two double variables, named distance and radius, initialize by setting each equal to aksValue function, with correct input
- declare one double variable, named eField, initialize by setting equal to eFieldFunction function, with correct input
- call printAll, with the correct inputs, to display the results
Function Definitions
- askValue – create a function to ask the user for the value of local double variable; return that variable. The user should know which parameter to enter based on the input string
- eFieldFunction – create a function to complete the calculation above and store in a local double variable; return that variable. The function should use distance and radius from the input list and use charge and conductivity from the global variables
- printAll – create a function to provide a standard output for this application, the output should look similar to the output solution below. The function should use distance, radius, and electric field from the input list and use charge and conductivity from the global variables
Output Solution
Enter distance: 4.5
Enter radius: 0.3
Gauss's Law for a Conducting Sphere
Charge (Q): 3.500
Conductivity (ke): 15.800
Radius (a): 0.300
Distance (r): 4.500
Electric Field (E): 2.731
- Numerical Extrapolation - Pass-By-Value Exercise
Background
In advanced calculus we may see a derivative that looks like
dydx=1+2xy, y0=1.0, 0≤x≤5
A numerical solution can be developed that will complete the calculus using simple algebra! The technique is called Runge-Kutta Extrapolation; we start at the coordinate (x=0, y=1) and advance until we reach x=5. The result is a table of xy pairs that make a line.
Don't worry! You do not have to know calculus to solve this problem.
Instead, I will take you through the steps, one piece at a time.
Write a program that generates the xy pairs for the derivative equation and initial condition for 0≤x≤5 using Runge-Kutta Extrapolation.
Your program should include the following:
Prototype Declaration
|
double step(double, double, int)
- double *ones(int, double = 1)
- double *linspace(double, double, int)
- double dydx(double, double)
- void RungeKutta(double *, double *, int)
- void printXY(double *, double *, int)
int Main Scope
- declare a constant integer to store the number of elements of an array, use nElem = 11
- declare a double pointer to store the x coordinate, initiailize using the linspace function with inputs, 0, 5, nElem
- declare a double array to store the y coordinate, declare as y[nElem] = {1.0}
(note this only sets y[0] = 1, all other values are undefined! This is okay. It is what we want: y0=1.0. The rest will be calculated later.) - call RungeKutta with inputs x, y, nElem
- call printXY with inputs x, y, nElem
Function Definitions
- step – create a function with input arguments: double L, double H, int N; return the calculation:
(H-L)(N-1) - ones – create a function with input arguments: int N, double V; return an array of N elements equal to V
– initialize the local variable as a pointer double *A = new double[N] (matching the output type)
– use a for loop to assign each element of the array as V A[i] = V
– return A - linspace – create a function with input arguments: double L, double H, int N; return an array of N elements ranging between L and H, equally spaced
– create a local variable and initialize using the step function double s = step(L,H,N)
– create a local variable as a pointer and initialize using ones function double *A = ones(N,L)
– use a for loop to increment each element of the array A[i] += s*i
– return A - dydx – create a function with input arguments: double x, double y; return the calculation;
1+2xy - RungeKutta – create a function with inputs: double *x, double *y, int N to complete numerical technique, as follows
– define a local variable and initialize double h = x[1] - x[0]
– define four local double variables double k1, k2, k3, k4
– start a for loop to advance from 0 to to N-1, then insert five equations… you will need to write the index using [] notation, xi→xi, yi→yi, yi+1→y[i+1]
k1=dydxxi, yi
k2=dydxxi+0.5h, yi+0.5k1h
k3=dydxxi+0.5h, yi+0.5k2h
k4=dydxxi+h, yi+k3h
yi+1=yi+h6k1+2k2+2k3+k4
– close the loop
– (note this is a void function acting on input arrays, changes will automatically apply to the int main scope!) - printXY – create a function with inputs: double *x, double *y, int N; print a table in the terminal of the results, format and values should match the output solulution
Output Solution
X Y
0.00 1.00
0.50 1.89
1.00 4.00
1.50 8.26
2.00 15.99
2.50 28.88
3.00 48.98
3.50 78.74
4.00 120.96
4.50 178.84
5.00 255.94
- Employee Pay Check - Pass-By-Reference Exercise
Background
Workers in the retail space can receive a weekly pay check based on a number of factors:
– salary workers get paid a fixed amount Salary÷52
– commission workers get paid a variable amount based on the number of products sold Fee×Units Sold
– hourly workers get paid a fixed rate based on the number of hours they work Rate×Hour Worked
Write a program that prints out the weekly pay summary, based on user input.
Your program should include the following:
|
Prototype Declaration
- void askValue(string, char&)
- void askValue(string, string&)
- void askValue(string, int&)
- void askValue(string, double&)
- void workerOptions(char&)
- void salaryPay(double&)
- void commissionPay(double&)
- void hourlyPay(double&)
- void printPayCheck(string&, double&)
int Main Scope
- declare four variables
- an integer to store the number of workers
- a string to store the worker's name
- a character to store the worker's type
- a double to store the worker's pay
- call askValue to set number example askValue("number of workers", number);
- begin a for loop…(int i = 0; i < number; i++)
- call askValue to set name example askValue("worker name", name);
- call workerOptions to set type
- make a switch-case
- salary (S) type –call salaryPay to set pay
- commission (C) type – call commissionPay to set pay
- hourly (H) type – call hourlyPay to set pay
- default – set pay to zero
- call printPayCheck, with correct input
Function Definitions
- askValue (four overloads)– create a function to ask the user for the value the & variable. The user should know which parameter to enter based on string variable; hint: make the first one, copy-paste, and change the type of the second input
- workerOptions - create a function to display the following to the terminal, then calls askValue to set input variable based on user selection
Worker Types
(S) Salary Worker
(C) Commission Worker
(H) Hourly Worker
- salaryPay – create a function that uses a local double variable to store the salary (call askValue to set salary), defines double& input variable as salary÷52
- commissionPay – create a function that uses a local double variable to store the fee (call askValue to set fee), uses a local integer variable to store the units (call askValue to set units), defines the input variable as fee×units sold
- hourlyPay – create a function that uses a local double variable to store the rate (call askValue to set rate), uses a local double variable to store the hours worked (call askValue to set hours worked), defines the input variable as rate×hours worked
- printPayCheck – create a function to print the pay check for the worker, see samples below for standard output; the string& input should represent the worker's name and the double& input should represent the worker's pay
Output Solution
Enter number of workers: 3
Enter worker name: Samuel
Worker Types
(S) Salary Worker
(C) Commission Worker
(H) Hourly Worker
Enter worker type: S
Enter annual salary [RMB]: 85000
.---------------------------------------.
| C++ Programming Network
| Pay to the order of Samuel
| 1634 RMB and 6 Jiao
.---------------------------------------.
Enter worker name: Caroline
Worker Types
(S) Salary Worker
(C) Commission Worker
(H) Hourly Worker
Enter worker type: C
Enter commission fee [RMB]: 23.8
Enter units sold: 38
.---------------------------------------.
| C++ Programming Network
| Pay to the order of Caroline
| 904 RMB and 3 Jiao
.---------------------------------------.
Enter worker name: Henry
Worker Types
(S) Salary Worker
(C) Commission Worker
(H) Hourly Worker
Enter worker type: H
Enter hourly rate [RMB]: 15.8
Enter hours worked: 36
.---------------------------------------.
| C++ Programming Network
| Pay to the order of Henry
| 568 RMB and 8 Jiao
.---------------------------------------.
EECE 1080C / Programming for ECE
Summer 2022



