//Cyber Battlefield/programs – Input/Output

Input and Output

A program is a sequence of statements. The statements get executed one after another in a single batch by the interpreter. Since the user isn't working with the interpreter, in fact the user no longer even knows there is an interpreter, we need another mechanism to display output. The function alert() pops up a window with a message given by whatever string you put between the ()s. Try entering alert("Hello World!"); in our JavaScript interpreter. Because this doesn't rely on having some kind of interpreter window, alert() is a good way to communicate information to the user in a program. To get values (input) from the user, JavaScript has a function called prompt(). You give it a message indicating to the user what input is supposed to be provided, and prompt( stringLiteral-Variable ) pops up a window with the message and an input box. Whatever string gets entered on that line gets returned as the value of the prompt() function.
Once you start entering several lines of statements, you often want to write little notes to yourself or whomever else will be looking at your code. Notes in programs are called comments. In JavaScript, anything written after a // until the next newline character is ignored by the interpreter, and so we use // to write comments.

Below is a simple program that gets the users height and weight and returns to the user what his weight would be if he were 25 feet tall. Play with it, understand it, and see if you can modify it so that it asks the user for a target height (in feet) rather than always using 25.


Fast food restaurant calculator — variables changing over time

A common feature of many programs is that they have a relatively small set of variables, but these variables change over time, i.e. they continually get new values assigned to them. The following program illustrates this. The program is a "fast food restaurant calculator". The user inputs the number of burgers they want, fries they want, and drinks they want, and the program prints out the total cost. The way the program is written, the variable total keeps a running total of the cost, which gets updated as the user orders a certain number of burgers, then a certain number of fries, then a certain number of drinks.
An interesting twist is to ask for the program to repeat the user's order before giving the total. We can accomplish this by keeping a running list of what gets ordered. The variable total is a number, and is initially zero to indicate that, at the beginning, the user hasn't yet ordered anything. To keep a running list of what's been ordered, we need a variable (we'll call it order) of type string. Initially, since nothing has been ordered, that string will be the empty string, i.e. "".
In this second version of the program, all three variables — total, num. and order — are constantly being assigned new values.


Here are some example problems to try and/or to look at solutions for.
  1. Write a program to do Fahrenheit to Celsius temperature conversion. solution
  2. Write a program to read the x and y coordinates of two points and compute the slope of the line through them. solution
  3. Write a program to read in a dollar amount and annual interest rate and print out how much money you'd have at that rate starting with that amount with after 10 years of annually compounded interest. solution, alternate solution.
  4. Write a program to solve the following problem:
    Consider a horizontal segment AC of length x feet and a point B above it, where the angle CAB is a degrees and ACB is b degrees. Taking x, a, b as input, compute and display the height y (in feet) of point B above segment AC.


Programming languages in the DoD

1974: DoD is using 450 different programming languages, spending $3 billion/year on software maintenance ($14 billion in 2012 dollars). A major part of "software maintenance" is fixing BUGS!. A search begins for an existing language most suited to DoD requirements ...

1977: No existing programming language is suitable. DoD solicits proposals for a language appropriate for embedded computer applications (i.e., command and control, communications, avionics, shipboard, test equipment, software development and maintenance, and support applications). ["Steelman"]

1980: DoD completes specification of the "Ada" language and subsequently mandates its use. Here's Ada code essentially equivalent to     prompt( "Hello, World!" );

  with Text_IO; use Text_IO;
  procedure Hello is
    Put_Line("Hello, World!");
  end Hello;

1996: DoD now using only 37 different programming languages. Much new code is being written in ADA, which is easier (thus less costly) to maintain: Ada was designed to be less prone to the kind of bugs that make software vulnerable to attack.

Today: Ada code is executing on US Navy systems such as the Aegis Weapon System, SSN-21 AN/BSY-2 Submarine Combat Control System, V-22 Osprey, AGM-114 Hellfire missile, CH46 Cockpit Control System, Tomahawk missiles, MK 41 Vertical Launch System.


You are expected to be able to understand simple programs, and modify simple programs as a means of demonstrating that understanding, but we are not going to give you an empty box and ask you to write a program from scratch to solve a problem. That's simply beyond the scope of this course. If you are interested in pursuing something like that, consider taking the intro CS/IT course IC210.

Well 2