CSc 4630/6630 Programming Assignment #1

Documentation
The first thing in your program should be documentation, such as the following. This should appear at the very top of your program.
  % program1.m
  %
  % Author: (Your Name)
  % Account: (your account name, or student e-mail)
  % CSc 4630/6630 Program #1
  %
  % Due date: (put the due date here)
  %
  % Description:
  % (Give a brief description of what your program does.)
  %
  % Input:
  % (State what the program inputs are.)
  %
  % Output:
  % (State what the program outputs are.)
  %
  % Usage:
  % (Give an example of how to use your program.)
  % (For example: out = myabs(in); )
  %
Verify that the command help program1 shows this information.

Objectives

  1. Practice designing a program.
  2. Practice implementing a program design in MATLAB.
  3. Practice testing a program.
  4. To get familiar with the programming environment.

Introduction
Your program will prompt the user for a few pieces of information, then print the information back to the command window.

This first assignment is focused on following the directions and getting used to the environment. Most of these directions will be repeated on all future assignments.

To solve your problem, write a program that reads the necessary information to compute and output the indicated values, as efficiently as possible. Design your program by specifying its behavior, identifying the variables and operations it needs to solve the problem, and then organizing the variables and operations into an algorithm. For our purposes, include functions as operations. Then code your design in MATLAB using step-wise translation. Finally, test your program thoroughly.

Assignment
Your program should show a menu of 3 options to the user. The first option is to convert from miles to kilometers. The second option is to convert from kilometers to miles. The third option is to quit your program. Choosing the first or second option means that your program should repeat the menu after the conversion. This way, the user can convert as many values as he/she wants without having to start your program more than once.

Make sure to prompt the user for information, such as "please enter your choice: ". For the first two options, you will need to follow up with a prompt for the value to convert.

Once the user has entered this information, print the following to the screen:
        X miles converts to Y kilometers.
or,
        X kilometers converts to Y miles.
or,
        quitting
The values for X and Y will change. X is the value that the user enters, and Y is the calculated value.

Use comments within the program to say how you handle any unexpected cases.

Your program should work for all possible inputs. Make sure that you test it with several different cases. For example, what if the input is given as a floating-point number, like 21.4? What if the user enters 0, -10, or 1000? How should your program handle the case where 1+2j is given as the input number? What should happen if the user enters a choice besides the three options?


Turn In Your Work

  1. your source code 
  2. the output from at least three different executions in which you test the correctness of your program (using a script*). Use more if the testing needs it. 

Make sure to use plain text files (.txt or .m).


* A script is short for typescript, where everything typed is saved. This keeps a log of all the input and output. MATLAB provides this capability with the diary command. For example, diary out records all inputs and outputs in a file named ``out''. Do NOT call your script file ``program1.m'', or it may overwrite your homework! Make sure to keep a backup of your work. Type help diary at the MATLAB prompt for more information.

The Mathworks refers to MATLAB programs as scripts. In computing in general, the term "script" can mean a program (i.e. what you make in the MATLAB editor) or it can mean a log (record) of activity (i.e. what is produced by the "diary" command). These are not exactly the same. Consider the simple case of a program with a single line. We can save the one-line program below in a file called simpleExample.m.

  a = 3 + 4

Now suppose we open MATLAB and type the following in the command window.

  diary simpleEx_log.txt
  simpleExample
  diary off

The first command creates a new file called simpleEx_log.txt, and starts recording the MATLAB session. The second line above runs our simpleExample.m program, then the last one closes the diary file, and stops recording. When done, the file simpleEx_log.txt looks like this.

  simpleExample
 
  a =
 
  7
 
  diary off

Thus we have two files, simpleExample.m which is our program (source code, what you turn in to the TA), and simpleEx_log.txt which is a log of our MATLAB session (the output from one execution of our program, what you also turn in as a hard copy).