CS 1112 Exercise 1

These exercises are intended to give you practice with the course material. If you have any questions, discuss them with your classmates! Asking and answering questions are both effective ways to learn and eventually master concepts. Of course, you can ask the course staff as well; they are there to help you learn.

You have until Sunday, February 14, at 11:00pm EST to complete this exercise and submit your solutions—a file lab01.txt containing the answers to several questions and a Matlab script file convertFah2Cel.m—on CMS. You can resubmit your solutions multiple times before the deadline, but only your last submission will be graded.

Matlab built-in functions… fun with Matlab

Matlab provides numerous built-in variables and functions. For each line below, type the text in the Command Window and press Enter to see what happens. Is the result what you expect? Where you see %____, write the output produced by the code on the left or answer the question in the file lab01.txt.

% This is a comment--no action is executed by the computer

% From this point on, read but do not type the text after the % symbol in a line.

% Variables, constants, and simple calculations:
  a= 100  % Look at the Workspace Pane: a VARIABLE called  a  has been created
  a= 101  % Look at the variable's value in the Workspace Pane
  b= 99   
  format compact
  a/b                        % _____________________________
  ans
  y= ans                     % _____________________________
  format long
  y
  format short
  y
  p=(3*2)^2
  q=(3*2)^2;  % Did you type the semi-colon?  Look at the Workspace Pane:  q is 
              % created but its value is not shown in the Command Window.

  x = 2;  y = x^x;  z = y^y  % _____________________________
  format loose

% Built-in functions:
  sqrt(x)
  pi  % a built-in variable
  cos(pi)                    % _____________________________
  abs(ans)
  abs(cos(pi))               % _____________________________
  exp(ans)
  rem(5,2)  % What does function rem do?  If you're not sure, try a few more
            % examples: rem(9,7), rem(10,6), ... ___________________________
  rand      % Generate one random number in the range of (0,1)
  help rand
  lookfor magic % MATLAB searches its documentation for the keyword 'magic'
                % Wait a few seconds.  If this command takes too long to
                % complete, press <Ctrl><c> to make it stop.

Running and editing a Matlab program

If you haven’t already, set up your file storage location (e.g., flash drive, hard drive, cloud storage) with folders and subfolders to organize all the files that you will write in the course. See the announcement “file storage” on the course website for more info.

From the course website (www.cs.cornell.edu/courses/cs1112), download the file spiral.m from the Exercises page to the folder that you will use for storing your Exercise 1 files, e.g., cs1112/exercise/ex1/. The easiest way is to right-click (Windows) or ctrl-click (Mac) on the file name, select save link as…, and browse to your folder. Make sure that the file name remains spiral.m without any space and parentheses. If your machine has added any extra characters to the file name, e.g., spiral (1).m, after saving the file you must change the name to be without any space and parentheses before you open or run the file in Matlab.

Now in Matlab set the Current Directory to be the directory in which you have stored your file spiral.m. You should now see the file listed in the Current Directory Pane.

To run the program (script) spiral, in the Command Window type the file name without the extension .m. What do you see on the screen?

To read the program, open the program file in the Editor Window: select menu options File→Open and then select or type the file name spiral.m. (Or in the Current Directory Pane double click on the file name.)

Now experiment with the program!

  1. Change the value of turnAngle from 100 to 137. Save the program and run it to see what happens.
  2. Try other values for turnAngle. What does the variable turnAngle represent (or control) in the program?
  3. Now change the value of numEdges and run the program again. Try again with another value. What does variable numEdges represent?
  4. Which variable controls the color of the spiral?
    Now change the color.

Temperature conversion (modified from Insight M1.1.3)

Download the file convertCel2Fah.m from the Exercises page and save it to your folder. Read and run the program. Let’s change the presentation of the result by modifying the last statement in the program:

  1. Change the substitution sequence from %.3f to %.8f. The substitution sequence is also called the format sequence. Save and run the program again and notice that the format of the number printed has changed. What does the substitution sequence %.8f specify?
  2. Say, you want to use 10 character spaces for printing the entire value (including the decimal point) with 2 decimal places shown. You will then use the substitution sequence %10.2f. Again, make this change and observe the print format.

Now save a copy of the file as convertFah2Cel.m. Modify it to prompt the user for a temperature in degrees Fahrenheit and then convert and print the temperature in Celsius. Modify the fprintf() statement to print the result to one decimal place. Test your program a few times using different input values. (You can check the answers using a search engine.)

CS 1112 course web page www.cs.cornell.edu/courses/cs1112

  1. Where are the reading assignments listed?
  2. What does the Academic Integrity policy for CS 1112 projects say about code found on the Internet?
  3. How do you indicate that you work with a partner on a project? (See “CMS” link)

CMS: Course Management System

You will use CMS to submit programming projects and to view your scores and grading comments. Try to log on CMS now. If CS 1112 is not listed as one of your courses, ask your section instructor to add you to CMS. Note that your projects must be submitted through CMS.

If you are using a lab computer, be sure to log off before you leave!