Final Exam Practice Answers Computers

pdf

School

Rutgers University *

*We aren’t endorsed by this school

Course

440:127

Subject

Computer Science

Date

Jan 9, 2024

Type

pdf

Pages

12

Uploaded by BailiffRockWolf28

Report
Printed Name__________________________ NetID__________________________ Final Exam Practice Your final exam will be EIGHT short answer questions. I have provided a few more example questions for you to practice with. Note that while some question formats may change to fit the online environment, the material will be similar. I will post an answer key on Thursday, 12/10. Exam 2
Printed Name__________________________ NetID__________________________ 1. Assume that we had the following lines of code: square.circle.number = 5; square.pentagon.number = 2; Thinking back to how structures work, please draw a visual representation of how this data is organized where data that is contained within other data is represented by shapes that are inside of other shapes. Please use the names of the shapes above (i.e. draw a circle to represent the variable circle , square to represent the variable square , etc) to determine which shapes to draw, and the values of the numbers to represent any numerical variables. a. Exam 2 5 2
Printed Name__________________________ NetID__________________________ 2. Each section of this problem builds upon the previous section. Please keep this in mind as you are answering. If you do not answer one section correctly, you can begin the next section assuming you have the correct answer. a. Write code that will create the variables, x and y. x and y are arrays. x should be an array with the numbers 0 to 100, with 200 elements. y should be the numbers -10 to 10, counting by 0.1. x = linspace(0,100,200); y = -10:0.1:10; b. Write code that will a matrix z. It should be length(y) by length(x) in size. z should implement the following multivariable mathematical function: for i = 1:length(x) for j = 1:length(y) z(i,j) = x(i)*2 + y(j); end end c. Write code that will display z as an image. imagesc(z) z = 2 x + y Exam 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Printed Name__________________________ NetID__________________________ 3. a. Given the following code, fill in the resulting plot. No labels needed, though you may want to differentiate between multiple lines so that graders can clearly read the plot: x = [-10:0.1:10]; y = ones(1,length(x))*-2; plot(x,y) hold on z = ones(1,length(x))*-1; plot(x,z) a = x-2; plot(x,a) b. Assuming we wanted to add a title, axes labels, and a legend to this plot, what lines of code would we write? title(‘plot stuff’) xlabel(‘x values’) ylabel(‘y values’) legend(‘line1’,’line2’,’line3’) Exam 2
Printed Name__________________________ NetID__________________________ 4. Write a function that takes two inputs, arr1 and arr2, and returns two outputs, arr12 and arr21. All inputs and outputs are arrays. arr12 should be an array with all of the numbers in arr1 followed by all of the numbers in arr2. arr21 should be all of the numbers in arr2 followed by all of the numbers in arr1. You function should work in the case that arr1 and arr2 have different orientations (i.e., one is vertical, the other is horizontal). This means you should use loops to accomplish this task. Do not use built in MATLAB functions that combine arrays. function [arr12 arr21] = myFunction(arr1,arr2) L1 = length(arr1); L2 = length(arr2); for i = 1:L1+L2 if i < L1 arr12(i) = arr1(i); else arr12(i) = arr2(i-L1); end if i < L2 arr(21) = arr2(i); else arr21(i) = arr1(i-L2); end end Exam 2
Printed Name__________________________ NetID__________________________ 5. For the following problem, refer to this program that modifies a particular kind of structure: function [widgetOut] = widgetAdd(widgetIn) widgetOut = widgetIn; widgetOut.weight = widgetOut.weight+widgetOut.componentWeight; widgetOut.componentNumber = widgetOut.componentNumber + 1; function [widgetOut] = widgetSub(widgetIn) widgetOut = widgetIn; widgetOut.weight = widgetOut.weight-widgetOut.componentWeight; widgetOut.componentNumber = widgetOut.componentNumber - 1; a. If we wrote the following code: widget.weight = 400; widget.componentWeight = 15; widget.componentNumber = 6; widget = widgetSub(widget); What value do all of the properties of widget have after this code? weight is 385, componentWeight is 15, componentNumber is 5. b. If we wrote the following code to call widgetConveyor widget.weight = 150; widget.componentWeight = 2; widget.componentNumber = 50; while widget.componentNumber < 55 widget = widgetAdd(widget); end What value do all of the properties of widget have after this code? weight is 160, componentWeight is 2, componentNumber is 55 Exam 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Printed Name__________________________ NetID__________________________ 6. Write a function that takes a row array as an input and produces a matrix as an output. If the input array is n in length, the output matrix should be n x n . The first row of the matrix should be the original array. Every subsequent row should be the row above modified in the following way: - The first column number is the same. - All other numbers are the result of the number from the previous row in the same column minus the number from the previous row and previous column. For example, the input [1 3 2 5 20] would produce the matrix: In the above matrix, for each set of bolded numbers, the number on the bottom is the result of the number directly above it minus the number above it to the left. function out = prob6(row1) n = length(row1); out = zeros(n,n); out(1,;) = row1; out(:,1) = row1(1); for i = 2:n for j = 2:n out(i,j) = out(i-1,j)-out(i-1,j-1); end end 1 3 2 5 20 1 2 -1 3 15 1 1 -3 4 12 1 0 -4 7 8 1 -1 -4 11 1 Exam 2
Printed Name__________________________ NetID__________________________ 7. Take the following code: function mysteryFunction(fileLocation) data = csvRead(fileLocation); [ydim, ~] = size(data); xAxis = data(1,:); for i = 2:ydim yAxis = data(i,:); plot(xAxis,yAxis) %Title goes here pause(0.25) end A. Fill out the following table B. What would be an appropriate line of code to replace the comment that says “Title goes here” ? title(‘Problem 7’) C. What would be a descriptive header comment for this function? Remember, a header comment should describe the purpose of the program. %This program reads data from an external csv file, and plots the first row of data vs the subsequent rows of data in order. This will cause a moving image to appear. Variable Name Data Type Brief Description of Variable’s Purpose fileLocation string Name of the file to read data Double/matrix Data from the file ydim double Number of rows in the file i Double/counting variable Counts through all of the rows xAxis Double/array Data from the first row yAxis Double/array Data from subsequent rows Exam 2
Printed Name__________________________ NetID__________________________ 8. Write a function called order that has two inputs, in1 and in2 , and two outputs, out1 and out2 . out1 should be the lower value of in1 and in2 . out2 should be the higher value of in1 and in2 . Note that if both inputs are the same, both outputs are the same. You can accomplish this task any way you see fit. Once you have written this function , write the proper syntax for calling this function elsewhere in MATLAB. Use appropriate inputs (i.e. actual values) when writing this syntax. function [out1, out2] = prob8(in1,in2) if in1>in2 out1 = in2; out2 = in1; else out1 = in1; out2 = in2; end ________________________________________________________________- [o1,o2] = prob8(5,10); Exam 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help
Printed Name__________________________ NetID__________________________ 9. Write a function called derivative1 . It should have two inputs, xValue and exponent , and two outputs, original and deriv . They should be assigned in the following way: function [original, deriv] = derivative1(xValue,exponent) original = xValue^exponent; deriv = exponent*xValue^(exponent-1); original = xValue exponent deriv = exponent*xValue exponent 1 Exam 2
Printed Name__________________________ NetID__________________________ 10. A data scientist is writing a program to sort through a data set on temperature. She has long arrays of temperature data, and corresponding arrays of the time the temperature data was collected. These arrays are of equal length. The data scientist only cares about the times that the temperature drops below freezing, and wants to create a simplified representation of that data in a new array. Put yourself in the data scientist’s shoes. Write a function that takes two inputs, an array of temperatures, and an array of times. Your function should return a new array of all of the times that the temperature drops below freezing. Assume that temperature is in degrees Celsius, and that the times are stored in a numeric format (i.e., the time array is an array of numbers). For example, with the arrays: Temp = [20 14 8 -1 -4 1 0 -5 8]; Time = [1000 1100 1200 100 200 300 400 500 600]; Your function would return: Output = [100 200 500] function out = prob10(temp,time) ct = 1; for i = 1:length(temp) if temp(i) < 0 out(ct) = time(i); ct = ct + 1; end end Exam 2
Printed Name__________________________ NetID__________________________ 11. Assuming there was an RGB image in the file sameOldDoge.jpg, answer the following questions: a. If x = imread(‘sameOldDoge.jpg’); and the file is an RGB image, how many dimensions does x have? In 10 words or less, why? 3 dimensions, rows, columns, and colors b. If x(:,:,1) = 0; and x(:,:,3) = 0; , what color(s) would we see when we displayed imshow(x) ? In 10 words or less, why? Green and black. This code sets red and blue to 0 (no intensity) c. If we took y = x/2; , would there ever be a non-integer number stored in any of the elements of y ? In 10 words or less, why? No. The data type is uint8, from above. uint8 is limited to integers between and including 0 and 255. Exam 2
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help