CSE2315 001 Java Code Program

Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
Name:
CSE2315-001 Tiernan
UTA ID:
Due: Sunday, 15 Aug 2021, 11:59pm. ** NO LATE WORK ACCEPTED. **
You may turn in homework with less than 100 points on it for credit.
Read all the instructions at the beginning and the end of this document.
Instructions for Lab Homework
1. Lab homework will always require you to submit code files and output. The output format will
be specified in the assignment and may include screenshots, output files created by the
program, or other documentation. If input files are created to run the homework, those input
files must be submitted also.
2. You may write your program in C, C++, or Java. Java programs will be tested in NetBeans. C
and C++ programs will be tested under gcc and/or Visual Studio.
3. You must include the following information in the header comments of every code file you
submit. This includes files with separate functions or classes as well as the main program file.
CSE 2315 Summer 2021 Tiernan
Name:
UTA ID:
Question # and part, if appropriate:
Programming language used for homework:
IDE and compiler used for homework:
Any additional info that would be needed for someone to run the program and grade it:
4. Lab HW code files must be named with your lastname, your first initial, then “Q#File#”. So if
your name is Happy Camper, the Lab Homework Question you are answering is 3.a, and you
are submitting 4 files total for this question (ex. a main program .java file, two screenshot .jpg
files, and an input .txt file) then your file names would be CamperHQ3aFile1.java ,
CamperHQ3aFile2.jpg , CamperHQ3aFile3.jpg , and CamperHQ3aFile4.txt . Be sure that your
input file name in the program matches the input file name for the file you are turning in, i.e. if
you are turning in CamperHQ3aFile4.txt as the input file then your program should not be
reading in “input.txt”, it should be reading in “CamperHQ3aFile4.txt ”
5. Almost every question asks you to submit labeled screenshots of your output. Make one file
named LastnameILabAnswers, ex. CamperHLabAnswers.docx. This should be a .txt, .docx, or
.pdf file. Put ALL the screenshots for all the questions into this one file. This file will be saved
with your other files and submitted for the lab homework.
6. Lab Homework must be submitted on Canvas. There is info at the bottom about creating a
folder for your assignment files to be turned in.
7. If you submit a partial working program, for example, it does some but not all of the required
tasks, you may receive partial credit as long as the program does not crash when it is run.
Likewise, if your program runs but gets incorrect answers, you may receive partial credit if
your code can be reviewed and your error determined.
8. If you have questions, e-mail Dr. T or ask the question on the Canvas discussion board. All
general questions (e-mailed and otherwise) will be answered on the discussion board.
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
REMINDER: Do not submit an entire project folder from your IDE for any question. Submit ONLY
the code file(s) that are specified. If you submit the entire project folder rather than the code
file(s), you will lose ½ of the points for this part of the assignment on top of any other deductions.
9. Submit the required problems and then complete additional selected problems in this lab
homework for credit up to a maximum of 110 points. The required problems are


Problem 1 worth 29 points
Problem 3, 4 OR 5 worth 13 points (3 and 5) or 18 points (4). Only one of these three
problems should be submitted. The first one listed in the answers document will be
graded and any others ignored.
At this point you will have either 42 or 47 points completed. Complete additional problems
to make up to or over 100 total points. Note that you must complete all parts of a problem in
order to get credit. You may reach over 100 points simply through your choices of problems
which add up to more than 100 points.
There is an extra credit 5 point question at the end of Problem 1 and an XC problem at the
end worth 8 points. Note that these two problems will not count toward calculating the first 100
points. This means if you do 97 points of regular problems and 13 points of extra credit, your
maximum score is 107 because you did not do 100 points of regular problems.
You may earn up to a maximum of 110 points worth of credit on this lab assignment
regardless of how much work you turn in including both required, selected and extra credit
problems (so don’t do things that don’t count unless you want to)
Problem values
Problem 1: 29 points
Problem 1e Extra Credit: 5 points
Problem 2: 24 points
Problem 3: 13 points
Problem 4: 18 points
Problem 5: 13 points
Problem 6: 32 points
Problem 7: 25 points
Problem 8: 33 points
Problem 9: 24 points
Problem 10: 28 points
Problem XC1 Extra Credit: 8 points
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Submit the required problems (1 and 2) and then additional selected problems in this lab homework
for credit up to a max of 110 points
Complete problems 1
REQUIRED: 1) Practice recurrence relations and combinations
Parts a – d must be completed to receive credit.
{29 pts total, parts a-d}
1) Write a program to implement the recurrences as described below. Each type of recurrence
will calculate the same value in a different way. For this question you must turn in the program
you write with all methods/functions included and you must include the output from test runs
with the following values:
Test run 1: a = 1, b = 5, n = 6
Test run 2: a = 2, b = 3, n = 4
Test run 3: a = 7, b = -1, n = 3
You may include other test runs if desired. Your test run output should be screenshots of the
output produced by your program into your LastnameILabAnswers file. Make sure the output
includes enough info so that the grader can determine what the output represents.
{output
screenshots and information 4 points}
1.a) Write a main main method/program/routine that will allow the user to enter values for two
integers a and b, and non-negative integer n and then call each of the three functions defined
below (in parts 1.b, c, and d) with those input values.
The main method/program should indicate to the user what range of values they may input for a,
b, and n and should check the input values. Once the user has entered valid inputs, the program
should call the binomial theorem method (or function) (1.b), then save and print the result. Then
the program should call the exponentiation method (1.c) and save and print that result. Finally
the program should call the repeated multiplication method (1.d) and save and print the result.
The main program should then compare each of the three results to determine if they are the same
and should print messages to the user indicating if no results are the same, two results are the
same, or all three results are the same.
After printing the results comparisons, the main program should ask the user if they would like to
enter another set of input values and if so, run again letting the user give new inputs.
The format of the output for each called method/function should look like:
(a+b)^n = B as calculated by the M method
where a, b, and n are the inputs,^ represents exponentiation, B is the value that is calculated and M
is the method name. So if a = 1, b = 2, n = 3, and the output is calculated by the exponentiation
method then the output for that would look like:
(1+2)^3 = 27 as calculated by the exponentiation method
{7 pts for main routine}
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Rubric:
Correctly accept inputs for a, b, and n from user and verify the input value validity {1 pt}
Call the binomial theorem function (part 1b) with a, b, and n as inputs and numeric return value and
then print message as described above {1 pt}
Call the exponentiation function(part 1c) with inputs and return value then print message {1}
Call the repeated multiplication (part 1d) with inputs and return value then print message {1}
Correctly compare results from three function calls and give appropriate output {2 pts}
Allow user to run again and input new values {1 pt}
1.b) Implement the binomial theorem (see section 4.5 for full description of binomial theorem) as
a method (or a function if using C/C++) to compute of (a + b)n with integer input values of a, b,
and non-negative integer input n. Use the specific formalism of the of using the combinations
C(n,k) in the summation with the terms an-kbk.
The method/function should return the value of the computation to the calling routine. The
calling routine should print an output message in the form shown above and stating that the
binomial theorem was used to calculate the result. You must implement the binomial theorem in
this method to get any points.
{8 pts}
𝑛
𝑛−𝑘
𝑘
n
Binomial theorem: (a + b) = ∑𝑘=0 𝐶(𝑛, 𝑘)𝑎
𝑏
or in alternate notation (a + b)^n = summation of k from 0 to n of [C(n,k)*a^(n-k)*b^k]
Rubric:
Correctly implement method with appropriate header, parameters, and return values {2 pts}
Correctly implement the binomial theorem in the method/function {6 pts}
1.c) Create a second method/function, the “exponentiation” method, to compute a + b and raise
this value to the nth power. This method/function should take the integer values of a, b, and nonnegative integer input n as inputs. The method/function should return the value of the
computation to the calling routine. The calling routine should print an output message in the form
shown above and stating that the value of (a + b) was raised to the nth power to calculate the
result. Use a built-in operator or method/function (such as the Java Math.pow() method, the ^
operator in Haskell, or the ** operator in Python) for calculating the power, i.e. doing the
exponentiation. You must implement the “exponentiation method” in this method as described to
get any points.
{6 pts}
Rubric:
Correctly implement method with appropriate header, parameters, and return values {1 pts}
Correctly implement the use of exponentiation by an operator or a method call in this
method/function {5 pts}
1.d) Create a third method/function, the “repeated multiplication” method, to compute a + b and
then multiply this value by itself n times. This method/function should take integer values of a, b,
and non-negative integer input n as inputs. The method/function should return the value of the
computation to the calling routine. The calling routine should print an output message as shown
above stating that the value of (a + b) was multiplied by itself n times to calculate the result. You
must implement the “repeated multiplication method” in this method as described to get any
points.
{8 pts}
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Rubric:
Correctly implement method with appropriate header, parameters, and return values {2 pts}
Correctly implement repeated multiplication to find the value of the power in the function {6 pts}
1.e) OPTIONAL EXTRA CREDIT: Create a program that does all the same things as the previous
programs except that a and b can be floating point numbers rather than integers and n can be any
integer. NOTE: if you are using floating point numbers in your program, you may need to test for
equality within some delta of each other, i.e. that the results are the same within 0.0001, for
example, of each other. The delta value should be small relative to smallest of a, b, or ab.{XC 5 pts}
2) Practice permutation and combination
All parts must be done to receive any credit
{24 pts total for parts a-e}
2.a) Write a main method/program that will have a loop that allows the user to enter two values n
and r during each loop and then call two functions/methods inside the loop as described below.
The program/main method should have loop that will allow the program to be run multiple times
before stopping with different user inputs for each iteration. This will be controlled with a
question to the user at the bottom of the loop asking if they wish to run the program again.
Inside the loop, the program/main method should give the user a message asking for two integers
and should tell the user that the value of n must be greater than or equal to r and the value of r
must be greater than or equal to 0. The main method/program should get input from the user for
two integers n and r. After the user enters the two values, the program should validate the input
to make sure that both inputs are greater than 0 and that n ≥ r.
If the user input is valid, the program should use n and r to call the first function P (question H.b)
passing in n and r as inputs. P will return an integer output to the main routine and this should be
saved and printed in a message on its own line like:
P(n,r) = result
with the actual numeric values filled in. For example, if n = 2 and r = 1, then the result of P will be
2 so the output will look like: P(2,1) = 2 .
Following the call to P, the main method/program should call the second function C (question H.c)
passing in n and r as inputs. C will return an integer output to the main routine and this should be
saved and printed in a message on its own line like:
C(n,r) = result
with the actual numeric values filled in. For example, if n = 2 and r = 1, then the result of C will be
2 so the output will look like: C(2,1) = 2 .
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Once the values of P(n,r) and C(n,r) have been calculated, then print out a message indicating the
difference between P(n,r) and C(n,r), that is, P(n,r) – C(n,r) with the actual numeric values filled in
on its own line.
P(n,r) – C(n,r) = difference
After the difference has been printed, the program should ask the user if they wish to run the
program again.
If the user input is invalid, the program should print a message for the user and then ask the user
if they wish to run the program again.
After asking the user if they wish to run the program again, the main method/program can have
the user respond in any number of ways their answer. The program can ask for a numeric value,
ex. “Enter 1 to run again and 0 to stop”, or letter value, ex. “Type Y to run again or N to stop”, or a
word etc. The program must tell the user how to respond to the question and then read in the
user’s response. If the user’s response to the question is invalid, the program should stop and end
the loop appropriately. Use a simple text interface for this interaction – no pop-up windows or
other graphics should be used. DO NOT USE a BREAK, CONTINUE, EXIT, or GOTO statement in this
program.
As long as the user responds positively, the program should loop to the top and ask for two new
input values. When the user responds negatively, the program should stop (ending the loop
gracefully) and print out a thank you message.
{7 pts}
Rubric:
Correct declaration of n and r and required variables to save results of P and C {1 pt}
Main has a loop to get user input for n and r, call functions P and C, print outputs and difference, and
run again if desired. Bottom of loop correctly allows user to run loop again if desired. {2 pts}
Correctly asks user to input n and r, reads input, validates input as specified, and handles incorrect
input {1 pt}
Correctly calls functions P and C, saves output values, and print outputs {1 pt}
Correctly calculates and prints difference in outputs of P and C {1 pt}
Main correctly implements all requirements as stated {1 pts}
2.b) The P function will calculate the permutation of P(n,r) and return an integer value to the
calling routine. P should accept two integer inputs n and r. The P function should calculate the
permutation by calling a factorial function fact (question 2.d) each time that a factorial value is
needed in the permutation calculation. Once the permutation has been calculated, the value
should be returned as the output return value from function P to the calling routine.
{5 pts}
Rubric:
Function/method P correctly accepts inputs n and r {1 pt}
P correctly implements formula for permutation {1 pt}
P correctly calls factorial function in permutation formula {1 pt}
P correctly returns permutation value {1 pt}
P correctly implements all requirements as stated {1 pt}
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
2.c) The C function will calculate the combination of C(n,r) and return an integer value to the
calling routine. C should accept two integer inputs n and r. The C function should calculate the
combination by calling a factorial function fact (question 2.d) each time that a factorial value is
needed in the combination calculation. Once the combination has been calculated, the value
should be returned as the output return value from function C to the calling routine.
{4 pts}
Rubric:
Function/method C correctly accepts inputs n and r and returns combination value {1 pt}
C correctly implements formula for combination {1 pt}
C correctly calls factorial function in combination formula {1 pt}
C correctly implements all requirements as stated {1 pt}
2.d) The fact function will accept one input integer and will return the factorial value of that
integer. The fact function should be recursive to calculate the factorial value.
{4 pts}
Rubric:
Function/method C correctly accepts integer input and returns factorial value{1 pt}
C correctly implements recursive formula for factorial {2 pt}
C correctly implements all requirements as stated {1 pt}
2.e) For this question you must turn in the program you write with all methods/functions
included and you must include the output from test runs with the following values:
Test run 1 must include all of the following test values
n = 5, r = 2
n = 10, r = 3
n = 10, r = 7
Test run 2 must include all of the following test values
n = 0, r = 1
n = 3, r = 0
n = 8, r = -6
You may include other test runs if desired. Your test run output should be labeled screenshots of
the output produced by your program and stored in your LastnameILabAnswers answer file of
type txt, docx, or pdf. Make sure the output includes enough info so that the grader can determine
what the output represents.
{code file, output screenshots, and contents 4 points}
Complete one of problems 3, 4, or 5. Only one can be submitted for credit
REQUIRED Choose only one of 3, 4, or 5
3. Practice number theory and sequences
{13 points total for parts a and b}
Both parts must be done to receive any credit
An arithmetic progression (arithmetic sequence) is a sequence of terms where there is an initial
term a and each succeeding term is obtained by adding a common difference d to the previous
term. The formula for the sum of the first n (n ≥ 1) terms of an arithmetic sequence is:
a + (a + d) + (a + 2d) +…+ [a + (n − 1)d] = (n/2)[2a + (n − 1)d]
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
3.a) Write a program/main method that takes in three inputs from the user and the calculates the
sum of the first n terms of the arithmetic sequence defined by those inputs and the formula for an
arithmetic sequence. Your program will calculate this sum in two different ways and compare the
results.
Your main method/program should tell the user that it will calculate a sum for an arithmetic
sequence:
a + (a + d) + (a + 2d) +…+ [a + (n − 1)d]
Your program should ask the user for three integers: n the number of terms in the arithmetic
progression, the initial term a, and the common difference d.
Using those three values your program will do two different calculations.
Calculation 1: The first calculation will iterate using the formula for the sequence to find the sum.
For example, if the user inputs n =5, a = 3, and d = 2, then your program should calculate the
following terms in a loop:
a,
HINT: Can you rewrite this calculation in terms of a plus some constant times d?
(a + d),
(a + 2d),
(a + 3d), and
(a + 4d) // because (n-1) = 4
and sum them up, save the value, and then output it with a message that this is the iterative result.
Remember that your program should be general and should work for any values of a and d and
any positive value of n.
Calculation 2: Next, your program will tell the user that it will calculate the sum using the formula:
(n/2)[2a + (n − 1)d]
This second calculation will substitute in the values of n, a, and d to find and save a value. This
value will be output with a message that this is the formula result.
For the final step, your program will compare the iterative result and the formula result and tell
the user if the values are the same, if iterative is greater than formula, or if formula is greater than
iterative. After this is completed, the program ends.
{9 points}
Rubric:
Correct development of program with correct user inputs {2}
Correct iterative Calculation 1 and output {3}
Correct use of formula Calculation 2 and output {2}
Correct comparisons of output for each calculation {2}
3.b) For this question you must turn in the program you write. You are not required to use
functions/methods but if you did then they must be included . You must include the output from
test runs with the following values:
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Test run 1 must include the following test values
n = 5, a = 3, d = 2
Test run 2 must include the following test values
n = 10, a = 0, d = 1
Test run 3 must include the following test values
n = 7, a = 2, d = 4
Test run 4 must include test values that you choose that are different from any of the values above.
n = __, a = __, d = __
You may include other test runs if desired. Your test run output should be labeled screenshots of
the output produced by your program and stored in your LastnameILabAnswers answer file of
type txt, docx, or pdf. Make sure the output includes enough info so that the grader can determine
what the output represents.
{code file, output screenshots, and contents 4 points}
Complete one of problems 3, 4, or 5. Only one can be submitted for credit
REQUIRED Choose only one of 3, 4, or 5
4. Practice number theory and sequences
{18 points total for parts a and b}
Both parts must be done to receive any credit
An arithmetic progression (arithmetic sequence) is a sequence of terms where there is an initial
term a and each succeeding term is obtained by adding a common difference d to the previous
term. The formula for the sum of the first n (n ≥ 1) terms of an arithmetic sequence is:
a + (a + d) + (a + 2d) +…+ [a + (n − 1)d] = (n/2)[2a + (n − 1)d]
4.a) Write a program/main method that takes in one input from the user n and the calculates the
sum of the first n cubes. Your program will calculate this sum in three different ways and compare
the number of operations it takes to calculate each of these results.
Your main method/program should tell the user that they can input a positive integer n and your
program will calculate the sum of the first n cubes. Be sure to check that their input is a positive
integer before running the calculations. So, for example, if they input 3 as n, then your program
should output the sum of 13 + 23 + 33 = 1 + 8 + 27 = 36. However, your program will have to
calculate this sum of cubes in three different ways and you are going to compare the number of
operations it takes to do each of the calculations.
Your program should ask the user for a positive integer input that you will use for n. Using this
value, you will do three different calculations and count the number of additions, multiplications,
and divisions in each type of calculations.
Calculation A: For the first calculation, your program should have a small loop where each
iteration of the loop calculates the next cube value and adds it to the total sum. You will calculate
the cube by doing multiplications only. Do not call the power function. Also in each loop, the
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
number of additions and multiplications are counted. You will need a variable to count the
number of additions (adds) and a variable to count the number of multiplications (mults). Your
adds and mults variables will be set to zero before the loop. The pseudocode looks like:
Loop with i going from 1 to n
Calculate the cube of i by multiplying
Add the cube of i to the total sum // can be combined with previous step
Increment adds by the number of plus (+) signs used
Increment mults by the number of multiplication (*) signs used
After the loop, print the total value, the number of adds and the number of mults
Calculation B: After the calculation above, you are going to calculate the sum of the cubes by using
the formula for an arithmetic sequence. This formula will calculate each cube and then you will
add each cube to the total just like the previous calculation. You will also count the adds and
mults. For Calculation B you also use division so count the divisions (divs) also. For Calculation B
you need the following info:
Formula to calculate a cube of n: (n/2)[2a + (n − 1)d] // replaces the multiplications of i
Value for n is the value input by the user
Value for a = 1
Value for d = 2(n+1)
Note: You may simplify this formula before putting it in your program and this will give a different
number of adds, mults, and divs than an unsimplified version of the formula
You should use a loop from 1 to n as in Calculation A and after the calculation, print the total value,
the number of adds, the number of mults, and the number of divs.
Calculation C: The third calculation is done differently that the two previous but it still requires a
loop, additions, and multiplications. For this calculation, you will do a summation from 1 to n+1
using the loop, including counting the adds, and then after the loop, you will square the summation
and count one multiplication. After the sum is squared, print the total value, the number of adds
and the number of mults.
{14 points}
Rubric:
Correct development of program with correct user input {2}
Correct implementation of Calculation A (iterative multiplication) and output {3}
Correct implementation of Calculation B (arithmetic sequence) and output {3}
Correct implementation of Calculation C (summation and square) and output {3}
Correct outputs of adds, mults, (and divs) for each calculation {3}
4.b) For this question you must turn in the program you write. You are not required to use
functions/methods but if you did then they must be included . You must include the output from
test runs with the following values:
Test run 1 must include the following test value
n=5
Test run 2 must include the following test value
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
n = 10
Test run 3 must include the following test value
n=7
Test run 4 must include a test value that you choose, different from any of the values above.
n = __
You may include other test runs if desired. Your test run output should be labeled screenshots of
the output produced by your program and stored in your LastnameILabAnswers answer file of
type txt, docx, or pdf. Make sure the output includes enough info so that the grader can determine
what the output represents.
{code file, output screenshots, and contents 4 points}
Complete one of problems 3, 4, or 5. Only one can be submitted for credit
REQUIRED Choose only one of 3, 4, or 5
5. Practice number theory and sequences
{13 points total for parts a and b}
Both parts must be done to receive any credit
A geometric progression (geometric sequence) is a sequence of terms where there is an initial
term a and each succeeding term is obtained by multiplying the previous term by a common ratio
r. The formula for the sum of the first n (n ≥ 1) terms of a geometric sequence, where r is not
equal to one (r 1), is:
a+ar+ar2 +…+arn−1 = (a-arn)/(1-r)
or in alternate form: a+ar+ar^2 +…+ar^(n−1) = (a-ar^n)/(1-r)
5.a) Write a program/main method that takes in three inputs from the user and the calculates the
sum of the first n terms of the geometric sequence defined by those inputs and the formula for a
geometric sequence. Your program will calculate this sum in two different ways and compare the
results.
Your main method/program should tell the user that it will calculate a sum for a geometric
sequence:
a+ar+ar^2 +…+ar^(n−1)
Your program should ask the user for three integers: n the number of terms in the geometric
progression, the initial term a, and the common ratio r. Check to make sure the inputs are valid.
Using those three values your program will do two different calculations.
Calculation I: The first calculation will iterate using the formula for the sequence to find the sum.
For example, if the user inputs n =5, a = 3, and r = 2, then your program should calculate the
following terms in a loop:
a,
HINT: Can you rewrite this calculation in terms of a times some constant times r?
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
ar,
ar^2,
ar^3, and
ar^4 // because (n-1) = 4
and sum them up, save the value, and then output it with a message that this is the iterative result.
Remember that your program should be general and should work for any values of a and r and
any positive value of n.
Calculation II: Next, your program will tell the user that it will calculate the sum using the formula:
(a-ar^n)/(1-r)
This second calculation will substitute in the values of n, a, and r to find and save a value. This
value will be output with a message that this is the formula result.
For the final step, your program will compare the iterative result and the formula result and tell
the user if the values are the same, if iterative is greater than formula, or if formula is greater than
iterative. After this is completed, the program ends.
{9 points}
Rubric:
Correct development of program with correct user inputs {2}
Correct iterative Calculation I and output {3}
Correct use of formula Calculation II and output {2}
Correct comparisons of output for each calculation {2}
5.b) For this question you must turn in the program you write. You are not required to use
functions/methods but if you did then they must be included . You must include the output from
test runs with the following values:
Test run 1 must include the following test values
n = 5, a = 3, r = 2
Test run 2 must include the following test values
n = 10, a = 0, r = 1
Test run 3 must include the following test values
n = 7, a = 2, r = 4
Test run 4 must include test values that you choose that are different from any of the values above.
n = __, a = __, r = __
You may include other test runs if desired. Your test run output should be labeled screenshots of
the output produced by your program and stored in your LastnameILabAnswers answer file of
type txt, docx, or pdf. Make sure the output includes enough info so that the grader can determine
what the output represents.
{code file, output screenshots, and contents 4 points}
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Practice relations and set operations
{32 points total}
All parts a – d must be completed for credit
6) Write a program that allows the user to enter the elements of a set and then two binary
relations represented by two groups of ordered pairs. The program will print out the input and
then will determine the union of the two binary relations and the intersection of the two relations.
The pairs in the union will be printed with an appropriate label and the pairs in the intersection
will be printed with an appropriate label
6.a) Write the main method/program and work through the user input and printing steps as
described below. Start the program by asking the user to enter a value that will be the count of
elements in their set S. |S| must be less than 20 and greater than 6 for this program. Once you
have the value for |S|, read in that many values and store them as the members of set S in an array
of 20 values. Print out the given values of set S in a set notation as shown:
Input set S = {a, b, c, d}
where the members of the set would be listed in place of the a, b, c… All the elements given by the
user should be printed.
Next, the program should ask the user to input the number of ordered pairs for the first binary
relation R1. The value of |R1| should be between 3 and 10 inclusive for this program. The
program should then read in that many pairs and store them in a 10 by 2 array, storing only as
many pairs as given by the user. After all the pairs are read in for R1, the same process should be
done for relation R2 and the R2 pairs stored in an second 10 x 2 array. |R1| and |R2| do not have
to be the same number but both must be between 3 and 10.
After all the pairs are read in for R1 and R2, the ordered pairs should be printed for each relation
in the format shown:
Relation R1 = [(a, b), (c, d), (e, f)]
where the ordered pair values would be listed in place of the a, b, c… All the elements given by the
user should be printed with R1 on one line and R2 on the next line. It is recommended, but not
required, that a function be written to print the |R| ordered pairs from the 10 x 2 array. This
function can then be called any time that ordered pairs need to be printed.
{14 pts}
Rubric:
Correct arrays declared for S, R1, and R2 {2 pts}
Meaningful messages to user for both input and output {3 pts}
Correct input of |S| and elements of S {2 pts}
Correct output of S using given format {1 pt}
Correct input of |R1| and |R2| and ordered pairs of R1 and R2 {4 pts}
Correct output of R1 and R2 using given format {2 pt}
6.b) Create a 20 x 2 array called union. Create the union of R1 and R2 (R1 U R2) and store this in
the array union. When complete, the ordered pairs in the union should be printed in the format
shown above for the ordered pairs. Remember that the union should not have duplicates. {6 pts}
Rubric:
Correct array declared for union {1 pt}
Correct algorithm to determine the union of pairs and store final result in union. {4 pts}
Correct output of union using given format {1 pt}
6.c) Create a 20 x 2 array called intersect. Create the intersection of R1 and R2 (R1 ∩ R2) and
store this in the array intersect. When complete, the ordered pairs in the intersect should be
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
printed in the format shown above for the ordered pairs. Remember that the intersection should
not have duplicates.
{6 pts}
Rubric:
Correct array declared for intersect {1 pt}
Correct algorithm to determine the intersection of pairs and store final result in intersect. {4 pts}
Correct output of intersect using given format {1 pt}
6.d) For this question you must turn in the program you write. You are not required to use
functions/methods but if you did then they must be included . You must include the output from
test runs with the following values:
Test run 1 must include the following test values
S = {1, 2, 3, 4, 5, 6, 7, 8}
Relation 1 on S = (1, 2), (2, 4), (3, 6), (4, 8)
Relation 2 on S = (1, 4), (2, 5), (3, 6), (4, 7), (5, 8)
Test run 2 must include the following test values
S = {1, 2, 3, 4, 5}
Relation 1 on S = {1, 2, 3, 4} // R1 is shown in cycle notation (Input to program can be in pairs)
Relation 2 on S = {2, 3, 4, 5} // R2 is shown in cycle notation (Input to program can be in pairs)
Test run 3 must include test values that you choose that are different from any of the values above.
S=
R1 =
R2 =
You may include other test runs if desired. Your test run output should be labeled screenshots of
the output produced by your program and stored in your LastnameILabAnswers answer file of
type txt, docx, or pdf. Make sure the output includes enough info so that the grader can determine
what the output represents.
{code file, output screenshots, and contents 6 points}
Practice matrix operations
{25 pts total}
Both parts a – b must be completed for credit
7.a) Write a program/main method that takes in inputs from the user and uses matrix addition
and multiplication to calculate the results. For this program you must use simple two dimensional
arrays to represent the matrices.
Create three 4 x 4 arrays to use for matrix A, matrix B, and the result matrix C. Ask the user to
enter a value for n that is 2, 3, or 4. Once you have the user input, ask the user for the values in the
first n x n matrix A. The user should give the values in row-major order, i.e. the first value is for
location [0,0], the second value is for location [0,1], etc. This means for a 2 x 2 matrix, the user will
enter 4 values for locations [0,0], [0,1], [0,1],[1,1] in your array A. Your program should store the
inputs in the correct locations. Next ask for the values in matrix B and store in the correct
locations in array B.
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Once the values are stored in the matrices (the two-dimensional arrays), print the two matrices in
rows and columns. Write a small function called matrixPrint that takes in the value of n for rows, a
value c for columns, and a two dimensional array and then prints out the meaningful array values
in rows and columns. This means to only print the values in the current n x n matrix even though
the array is bigger than this.
Print a label for Matrix A and the call matrixPrint with n and A as the input matrix. Then print a
label for Matrix B and print that matrix. Next, your program will do three different operations
using these matrices.
Matrix operation 1: Sum the values in matrix A and matrix B and save the results in matrix C. You
can write a function to do the sums or do them in your main routine. Print a label “Sum of A and
B” and then call matrixPrint with C as the input matrix.
Matrix operation 2: Write a function/method, named matrixMult, to do matrix multiplication. The
function should take in three matrices, multiply the first matrix times the second matrix, and store
the result in the third matrix.
Matrix operation 2A: Call matrixMult with A for the first matrix, B for the second matrix, and C for
the third matrix. After the function call, print a label “Result of A times B” and then call
matrixPrint with C as the input matrix
Matrix operation 2B: Call matrixMult with B for the first matrix, A for the second matrix, and C for
the third matrix. After the function call, print a label “Result of B times A” and then call
matrixPrint with C as the input matrix
{21 pts}
Rubric:
Correct development of program with correct user input {3}
Correct implementation of function matrixPrint {3}
Correct implementation of Matrix operation 1 (matrix addition) and output {4}
Correct implementation of function matrixMult {5}
Correct implementation of Matrix operation 2A (A times B) and output {3}
Correct implementation of Matrix operation 2B (B times A) and output {3}
7.b) For this question you must turn in the program you write including your functions/methods.
You must include the output from test runs with the following values:
Test run 1 must include the following test values
n=2
A = 2, 3, 4, 5
B = 6, 5, 4, 3
Test run 2 must include the following test values
n=3
A = 1, 2, 3, 1, 2, 3, 1, 2, 3
B = 4, 5, 6, 7, 8, 9, 1, 2, 3
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Test run 3 must include the following test values
n=4
A = 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4
B = 1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8
Test run 4 must include test values for a 3 x 3 matrix that you choose that are different from the
values above.
n=3
A=
B=
You may include other test runs if desired. Your test run output should be labeled screenshots of
the output produced by your program and stored in your LastnameILabAnswers answer file of
type txt, docx, or pdf. Make sure the output includes enough info so that the grader can determine
what the output represents.
{code file, output screenshots, and contents 4 points}
Practice relations
{33 pts total}
All parts a – d must be completed for credit
8) The following recursive function g (defined with pseudocode) is initially invoked with an i
value of 1. L is a list (array) of MAX integers. [In this pseudocode, L has an initial index of 1 and a
final index of MAX. Your code can adjust the indices of L as appropriate for your programming
language.] You will need the value of MAX in the function/method g so make sure you can get that
value in the function/method either as an additional parameter or by finding the size of L
g(list L; positive integer i; integer x)
print a blank line
print “Function g is called with i = “+i+” and user input x = “+x +” and MAX = “+MAX
if i > MAX then
print “i is greater than MAX ”+ MAX
print “**Returning -1”
return -1
else
if L[i] = x then
print “The value of L[“+i+”] = ” + (L[i]’s value) + “ and x is ”+ x
print “**Returning [i] ”+ i
return i
else
print 2*i number of blank spaces //(just single spaces not blank lines)
print on the same line“x =“+x+ “ and is not equal to L[“+i+”] = ”+L[i]
return g(L, i + 1, x)
end if
end if
end function g
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
8.a) Write a main method/program that will have a loop that allows the user to enter a value in
each loop for x and then call the function/method g inside the loop. In the main method, the list L
should be declared as something like an array with size of MAX. The list L should be initialized
with random values between 0 and (10 *MAX). Write a method to fill the list L with the random
values. Use a method to print out the values of L in a table of no more than 10 columns across. E.g.
for a list of 50 values you could print 5 rows of 10 columns or 10 rows of 5 columns but not 2 rows
of 25 columns.
The main method/program should then get input from the user for x. The user will have two
choices for giving x a value. 1) The user can choose to enter a value for x where the main
method/program tells the user the valid input range and error checks the input given by the user
or 2) The user can have the program randomly select a value for x from the range of 0 and 10
*MAX.
The main method should also declare two variables to use for recording the results from g – the
integer variable numNots and the integer variable numFounds – which should both be initialized to
0. (Since the program will be looping, make sure to define these variables outside of this loop but
still inside of the main method/program.)
Once the user input x value is valid, the main method/program should call g with the list/array L,
the value of the first index of L (typically 0 or 1) for i, and the user’s input for x. Each recursion of
g will print a message as defined in the algorithm pseudocode above. When the function/method
g is complete it will return a value to the main.
{9 pts}
Rubric:
Declaration of list/array L with size MAX and required variables {1 pt}
Method written to correctly fill L with random values as described {1 pt}
Method written to correctly print L out in a table that meets constraints {1 pt}
Main has a loop to get user input for x, call method g, print table L, and run again if desired {2 pts}
User correctly has choice to give a value for x or can have a random value selected {2 pts}
Main correctly implements all requirements as stated {2 pts}
8.b) Implement the function/method g above.
Rubric:
Correct development of method g with correct parameters and return values {2}
Correct handling of base cases {2}
Correct recursive call {3}
Correct output for each call to the method/function {2}
{9 pts}
8.c) Add the following structure to the main method/program:
{9 points total}
The main method/program should save the returned values from g as follows.
If g returns the value -1, add -1 to numNots and print a message indicating this {2}
If g returns an i value, i.e. ≥0, add +1 to numFounds and print a message indicating this {2}
Ask the user if they wish to run another test of function/method g.
If yes, then ask if they wish to enter a value for x or if they wish to have a value randomly selected
then call function/method g. {1}
If not, then the program is complete so output the value of numNots in a sentence giving the value,
explaining what the value represents, and noting how many results are part of numNots and
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
also output the value of numFounds in a sentence giving the value, explaining what the value
represents, and noting how many results are part of numFounds. {4}
8.d) For this question you must turn in the program you write with all methods/functions
included and you must include the output from eight (8) test runs where six of the test runs must
be run with the following values. Run at least the 4 test runs with MAX of 10 as one execution of
the program looping 4 times (once for each test run). Run at least 2 test runs with MAX of 1000 as
one execution of the program with 2 loops. The remaining 2 required test runs can have a MAX of
10, 1000, or some other value.
Test run 1: MAX = 10, user input x = 12
Test run 2: MAX = 10, user input x = random
Test run 3: MAX = 10, user input x = 5
Test run 4: MAX = 10, user input x = -1
Test run 5: MAX = 1000, user input x = 343
Test run 6: MAX = 1000, user input x = 999
Test run 7
Test run 8
You may include other test runs if desired. Your test run output should be screenshots of the
output produced by your program saved to your LastnameILabAnswers file. Make sure the output
includes enough info so that the grader can determine what the output represents.
{output
screenshots/files and contents 6 points}
Practice graph algorithms
{24 points total}
All parts must be completed for credit
9.a) Write a program/main method that takes in an adjacency matrix from the user and uses the
Euler path algorithm to determine whether the graph represented by the matrix has an Euler path.
For this program you must use a simple two dimensional arrays to represent the matrix.
Create one 10 x 10 array, called graph, to use for the input adjacency matrix. Ask the user to enter
a value for n that is between 5 and 10 inclusive and represents the number of nodes n in the graph.
Once you have the n value, ask the user for the name of a text file that contains the values to put in
graph,the n x n adjacency matrix.
The file should give the values in row-major order, i.e. the first value is for array location [0,0], the
second value is for location [0,1], etc. This means for a 2 x 2 matrix, the file will have 4 values for
locations [0,0], [0,1], [0,1],[1,1] in your array graph. Your program should store the inputs in the
correct locations. An example file for a 3 x 3 matrix could be called graphAdjacency and could
contain undirected arcs (1,1), (1, 3), (2, 3), (3, 1) where node 1 is on row [0] of the matrix:
102
001
210
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Once the values are stored in the matrix (the two-dimensional array), print the adjacency matrix
in rows and columns. If you have not already done so for another question, write a small function
called matrixPrint that takes in the value of n and one two dimensional array and prints out the
meaningful array values in rows and columns. This means to only print the values in the current n
x n matrix even though the array is bigger than this. If you already have matrixPrint then copy it to
this program to use.
Print a label for the graph adjacency matrix and the call matrixPrint with n for rows, n for columns,
and graph as the input matrix. Next, your program will determine if the graph has an Euler path
using the adjacency matrix.
{10 points}
Rubric:
Correct development of program with correct user input for n {2 pts}
Correct adjacency data read from file and stored in graph {3 pts}
Correct implementation of function matrixPrint {3 pts}
Correct output of graph {2 pts}
9.b) Use the Euler path algorithm (given below) and your graph to determine if there is an Euler
path in the original graph. Print a message indicating if there is or is not an Euler path. You can
put this algorithm in the main method/program or put it in a function and call it from the main
method/program.
{8 points}
ALGORITHM EulerPath
EulerPath (n x n matrix A)
//Determines whether an Euler path exists in a connected graph with
//no loops and adjacency matrix A
total = 0
i=1
while total ≤ 2 and i ≤ n do
degree = 0
for j = 1 to n do
degree = degree + A[i, j ] //find degree of node i (*)
end for
if odd(degree) then
total = total + 1 //another odd degree node found
end if
i=i+1
end while
if total > 2 then
write (“No Euler path exists”)
else
write (“Euler path exists”)
end if
end EulerPath
Rubric:
Correct implementation of Euler path algorithm and output {8}
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
9.c) For this question you must turn in the program you write with all methods/functions included
and you must include the output from three test runs as described below. Remember that the
adjacency matrix data must be in a file that will be read by the program. Put the matrix data
below into different test files and submit the test files that you create along with your code
Test run 1: n = 3
1 0 2
0 0 1
2 1 0
Test run 2: n = 5
0
1
1
0
1
0
1
0
1
1
0
1
0
0
1
0
1
0
1
0
1
0
1
0
0
Test run 3: n = 5 or greater
Your data for an adjacency matrix with at least 9 arcs listed in the matrix (Test run2 has 12
arcs listed)
You may include other test runs if desired. Your test run output should be screenshots of the
output produced by your program saved to your LastnameILabAnswers file. Make sure the output
includes enough info so that the grader can determine what the output represents.
{output
screenshots/files and contents 6 points}
Practice recurrence relations
{28 total points}
All parts must be completed for credit
10) In Austin, the Congress St. Bridge colony of bats was counted every 2 months starting in 2016.
The first four counts were 4000, 6000, 9000, and 13500.
10.a) Assuming that this growth rate continues, write a recurrence relation for the number of bats
at count n.
{4 points}
Rubric:
Correct recurrence relation {2 pts}
Work shown of how recurrence relation was derived {2 pts}
10.b) Solve the recurrence relation to find a closed form solution.
Rubric:
Correct closed-form solution {3 pts}
Correct work shown of how closed-form solution was derived {3 pts}
{6 points}
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
10.c) Write a small method/function called bats to implement the recurrence relation closed form
solution from 4.b. The input should be the value for n and the output of the function/method will
be the number of bats at that 2-month count.
{6 points}
Rubric:
Correctly implement method with appropriate header, parameters, and return values {2 pts}
Correctly implement closed form solution to find the value in the function {4 pts}
Incorrectly implementing recurrence relation as a recursive function is worth {2 pts vs. 6}
10.d) Write a main method/program that will have a loop that allows the user to enter a value in
each loop for n and then call the function/method bats . The program should print an output
statement that tells the input n value and the output value from bats in the form of a sentence.
Example outputs could be “For count 3 at 6 months, the bat count is 9000” or “There were 9000
bats at count 3” or “The count at 6 months was 9000”. All of these include the n count info and the
number of bats (“6 months” is a count of 3 times the 2 month interval).
At the end of each loop, the main program should ask the user for another n value or to enter 0 if
they are ready to end the program.
{7 pts}
Rubric:
Correctly accept inputs for n from user and verify the input value validity {1 pt}
Call the bats function (part 4c) with inputs and capture return value {2 pts}
Correctly print all required message data {2 pts}
Allow user to run again and input new values {2 pt}
10.e) Use the program to determine what the 4th, 12th, and 15th counts will be giving the amount of
bats. Your test run output should be screenshots of the output produced by your program aved to
your LastnameILabAnswers file. Make sure the output includes enough info so that the grader can
determine what the output represents.
{4 pts}
Extra Credit
Practice recurrence relations and functions
{8 points extra credit}
XC) Functional programming languages, as opposed to conventional (procedural) programming
languages such as C++, Java, or Python, treat tasks in terms of mathematical functions. A
mathematical function such as f (x) = 2x transforms the argument 5 into the result 10. Think of a
program as a big function to transform input into output. A functional programming language
contains primitive functions as part of the language, and the programmer can define new
functions as well. Functional programming languages support function composition, allowing for
complex combinations of functions. Using the functional programming language Scheme, we can
define the doubling function by
(define (double x) (* 2 x)
The user can then run the program and type
(print (double 5))
which produces an immediate output of 10.
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Practice by writing a Scheme function, called “square”, to square a number.
Using your function and the one above, what is the output from the following user input?
(double (square 3))
XC1) Scheme also supports recursion, plus the usual control structures of procedural languages,
such as conditional and iterative statements.
XC.a Given the Scheme function
(define (mystery n)
(cond ((= n 1) 1)
(else (*n (mystery (- n 1))))))
what is the output of
(print (mystery 4))
{4 pts}
XC1.b) The “mystery” function is better known as _______.
Use the codingground Scheme compiler at TutorialsPoint:
https://www.tutorialspoint.com/execute_scheme_online.php
{4 pts}
If you work this problem, put your answers for XC1.a) and XC1.b) in your LastnameILabAnswers
file. Make sure the output includes enough info so that the grader can determine what the output
represent
——————————————————————————————————-Notes for lab submission
Pay close attention to all requirements on this page, including file names and submission format
where specified. Even in cases where the program works correctly, points will be taken off for not
following the instructions given on this page (such as wrong file names, wrong compression
format for the submitted code, and so on). The reason is that non-compliance with the instructions
makes the grading process significantly (and unnecessarily) more time consuming. Contact the
instructor or TA if you have any questions.
How to prepare your assignment to submit
Create a folder to hold your code files, output files, screenshots, any test data files, and any other
files you will be submitting. Name the folder your lastname, then your first initial, then “Lab”. So
if your name is Happy Camper, then your Lab folder name is CamperHLab. Save your
CamperHLab answers files in this folder. Save all the code files for the assignment in the folder.
Save any other files created for the assignment in the folder. Once you have put all the needed
files in your folder, zip the folder to compress it before submitting it (see info further down with
submission details). Do not make subfolders for the different questions. All files should be in one
folder.
Lab Homework Summer 2021
Due: Sunday, 15 Aug 2021, 11:59pm
CSE2315-001 Tiernan
Only the code files, output, screenshots and input files (if any) should be included in your zipped
folder. Do NOT include the entire compiled project. You will be penalized if you save entire
projects instead of just code files.
How to submit
The assignment should be submitted via Canvas. Submit a ZIPPED directory/folder called
LastnameILab.zip where “LastnameI” should be replaced with your last name followed by your
first initial, ex. my filename would be TiernanCLab.zip . The file must be ZIPped (not RAR). No
other forms of compression accepted. (Contact the instructor or TA if you do not know how to
produce .zip files). The zipped directory should contain all the code, text, output, and any other
files as specified above.
To create a zipped directory called LastnameILab.zip, follow these steps:
1.
Create a folder called LastnameILab.
2.
Copy to that folder all your solutions (your answers file -if you needed this, all your code
files, any output files, screenshots, and any .txt files that were required).
3.
Zip that folder. On some Windows systems, you can zip a folder by right-clicking on the
folder, and then selecting Send to->Compressed (zipped) folder. On Mac, go to the parent
directory of your Lab directory in a Finder window, click on your LastnameILab folder, then select
Compress from the list of file actions (under the gear icon).
4.
Submit your zipped folder through your Canvas account at the lab assignment. You click on
the name of the assignment and then it goes to the screen where you can upload your zipped file.
(Assignments are only accepted through Canvas.)
Submission checklist
• Did you answer the required number of the questions in the lab assignment and did you create
all the required program files and text files, if any?
• Do all your program files run without errors and give correct output?
• Did you create any screenshots that were requested and save them in the folder?
• Did you put all of the files you created into one folder called LastnameILab ?
• Did you zip that folder into a file called LastnameILab.zip?
• Did you upload the zipped file to Canvas before the due date and time?

Calculate your order
275 words
Total price: $0.00

Top-quality papers guaranteed

54

100% original papers

We sell only unique pieces of writing completed according to your demands.

54

Confidential service

We use security encryption to keep your personal data protected.

54

Money-back guarantee

We can give your money back if something goes wrong with your order.

Enjoy the free features we offer to everyone

  1. Title page

    Get a free title page formatted according to the specifics of your particular style.

  2. Custom formatting

    Request us to use APA, MLA, Harvard, Chicago, or any other style for your essay.

  3. Bibliography page

    Don’t pay extra for a list of references that perfectly fits your academic needs.

  4. 24/7 support assistance

    Ask us a question anytime you need to—we don’t charge extra for supporting you!

Calculate how much your essay costs

Type of paper
Academic level
Deadline
550 words

How to place an order

  • Choose the number of pages, your academic level, and deadline
  • Push the orange button
  • Give instructions for your paper
  • Pay with PayPal or a credit card
  • Track the progress of your order
  • Approve and enjoy your custom paper

Ask experts to write you a cheap essay of excellent quality

Place an order