You are on page 1of 1

Fall 2014, Dr.

Frank Shih

CS 101 Computer Programming & Problem Solving


Homework #4
DUE: 11:00pm, Nov. 14, 2014
NAME:
STUDENT ID#:
NOTES: Submit the homework solution in Microsoft Word format to http://njit2.mrooms.net before
the above deadline. Absolutely, NO LATE SUBMISSION IS ACCEPTED. Write the answers in
your own words individually. Any plagiarism will post a ZERO score or cause a FAIL grade.
4.1 (20 points) Use for-end loops to create a 47 matrix in which the value of each element follows
the three cases: (a) If the row number is less than the column number, the value is the sum of its
squared indices (the row number and column number of the element); (b) If the row number is the
same as the column number, the value is the sum of its indices; (c) If the row number is greater than
the column number, the value is the sum of its cubed indices. For example, the value of element
A(2,5) is 29; A(2,2) is 4; A(4,2) is 72. Show the script file and the result of the matrix A.
4.2 (20 points) Fibonacci numbers are the numbers in a sequence in which the first two elements
are 0 and 1, and the value of each subsequent element is the sum of the previous two elements:
0, 1, 1, 2, 3, 5, 8, 13, ...
Write a MATLAB program in a script file using a for-loop that determines and displays the first 20
Fibonacci numbers. Hint: Initialize the first and second elements to be 0 and 1, respectively, and
calculate those elements from the third element on.
4.3 (20 points) Write a program in a script file that finds the smallest odd integer that is divisible by
13 and whose square root is greater than 120. Use a for-loop in the program. The loop should start
from 1 and stop when the number is found. The program prints the message The required number
is: and then prints the number. Hint: Use rem(n,2)~=0 for finding odd integers, and rem(n,13)==0
for finding the number to be divisible by 13.
4.4 (20 points) Write an M- file to evaluate the equation y ( x) x 2 6 x 5 for all values of x
between 1 and 3, in steps of 0.1. (a) Use a for-loop to define x and y and plot the resulting function
using a 3-point-thick dashed red line. (b) Use vectorization to define x and y and plot the resulting
function using a 5-point-thick solid green line. Hint: In the for-loop, i is from 1 to 41, and
x(i)=(i-11)/10.
4.5 (20 points) Write a MATLAB function, called breaktime.m, that calculates and returns three
output arguments. The function will receive one input argument representing a total number of
seconds, and returns the number of hours, minutes, and remaining seconds that it represents. For
example, 7515 total seconds is 2 hours, 5 minutes, and 15 seconds because 7515 = 3600 * 2 + 60 *
5 + 15. Show your function file and the above testing result.

You might also like