Skip to content
cesardv edited this page Sep 11, 2012 · 1 revision

Csc4330 - Assignment 1

  1. Total lexemes = 55.

    4    public class FtoC {
    11       public static void main ( String [ ] args) {
    5             double fahrenheit = 98.6;
    15            double celsius = ( fahrenheit - 32.0 ) * (5.0 / 9.0 );
    9             System.out.print("Celsius equivalent: ");
    9             System.out.println(celsius);
    1        }
    1    }
  1. The C statement: printf("i = %4d x = %7.3f", i, x); in Fortran would be:

Ln1)    PRINT 2 i, x 

Ln2)    FORMAT (i = ,I5,4H x =, F7.3)

3-

A) The CONTINUE statement is sort of a dummy keyword that was used most often to control transfer in loops. It does not create object program instructions, it's most often found as the last statement in DO-loop.

B) The FREQUENCY is a non-executing, optionally used statement that helped the compiler optimize the object code, when a Transfer was used arising out of a IF branch.

C) PAUSE statement was used to stop the program at a specific instruction or value, and display the address of such variable. The 704 machine's console would HALT and would resume once the START button was hit. This was sort of an old-fashioned breakpoint, since debuggers hadn't been born.

4- ((A B) ((C) D))

    
   
    

    

5- For this lisp program:


    ; LISP Example function
    ; The following code defines a LISP predicate function
    ; that takes two lists as arguments and returns True
    ; if the two lists are equal, and NIL (false) otherwise
    (DEFUN equal_lists (lis1 lis2)
	(COND
		((ATOM lis1) (EQ lis1 lis2))
		((ATOM lis2) NIL)
		((equal_lists (CAR lis1) (CAR lis2))
			(equal_lists (CDR lis1) (CDR lis2)))
		(T NIL)
    	)
    )


(A) Return value will be NIL

(B) equal_lists function will be called 7 times

6- Apart from index bounds checking, Integer and Stack overflow checks are also done at runtime when using Ada. This would prevent for example infinite loops. Also, runtime check for calls to subprograms before they are defined are also possible.

7-
(A) The program prints out a calendar on an html (page) table for any given month/year (accounting for leap-years too), and marks the given day on that calendar by shading the background on that cell.

(B) First the program starts by defining an array of months, in which the first month of the year (January) is at index 0, and so on. Similarly, an array called daysInMonth is defined to store the number of day that each month normally has, starting with January as well at index 0. The exception to the defined number of months in that array is February, during leap years, but that is dealt with in the code with IF statement when the desired month calendar is February. The last variable is the last day of the given month we're creating the calendar for, and it is also the upper bound of the for-loops that contruct the calendars body seven columns at a time. The program then using the built-in functions document.open(), document.clear(), document.write(), and document.close() creates strings of html/css code necesary to display the calendar on the browser.

(C) Javascript uses var as the type identifier in declaration because it is a dynamic scripting language. It uses the "context" of the declaration and type inference to determine the proper data type at interpretation/runtime.

(D) We have a few undeclared variables, like last which obtains the integer number of days of that particular month from the daysInMonth int array. This is possible due to javascript's dynamic nature, thus it can tell the type since it knows we already stored ints in that array. Also, note the parameters of the display() function. These are not given a type in the function's definition, due to the similar reasons. Specifically in this case though, we get away with not having to declare the parameters' types since we call display() using the returned integers that result from calls to getMonth(), getFullYear(), and getDate().

(E) Basically === or !=== are most commonly used when we know our two operands to be of the same type. Using === is most efficient, since it compares without doing any "dynamic" type conversions. Since javascript is a dynamic language it can "coerse" a comparison between two different types, when one uses == and !=. However, the rules of this implicit conversion-plus-comparison are convoluted and this is not for the faint of heart. For this program we could change the ===/!== to ==/!= but we would not gain anything by it. According to some runnable tests at jsperf.com using == to compare two (equal) string literals is 34% slower than just using ===.

Clone this wiki locally