IDL Exercise 7
The purpose of this exercise is to gain an understanding of the operation
and use of the control statements
FOR, WHILE, IF and CASE.
-
Write a program that uses the FOR loop to compute the cumulative total
over an array of values. Create a file in your account with this program
and test it by plotting the cumulative sum of the numbers up to 100.
FUNCTION CUM_TOTAL,x
;+
; y=CUM_TOTAL(x) is an array of the same size as x in which
; each element is the sum of itself and all lower-index elements.
;
; EXAMPLE
; x=FINDGEN(10)
; y=CUM_TOTAL(x)
; PLOT,x,y
;
;-
y=x*0B ; Create an array of zeros the same size
as x.
N=N_ELEMENTS(x)
y[0]=x[0]
FOR i=1,N-1 DO y[i]=y[i-1]+x[i]
RETURN,y
END
-
Use the CUM_TOTAL to find and plot the sum of the squares of all of the
numbers up to 100.
-
Write a program to simulate a gambling game. This program uses RANDOMN
to generate random numbers. Use this program to generate plots of gambling
results for a variety of values of D and b.
FUNCTION Gamble,D,b
;+
; r=Gamble(D,b) returns the amount of winnings after each repetition
; of the gambling game. The program stops when D dollars are either
; won or lost. The game is assumed to have a bias b in favor of the
; house. A negative value of b is a bias in favor of the player. The
; program uses a WHILE loop and has a limit of 10,000 plays.
;
;EXAMPLE
; r=gamble(10,0.1)
; plot,r
;-
s=0
r=FLTARR(10001)
n=0L
WHILE ABS(s) LE D AND n LT 10000 DO BEGIN
s=s+RANDOMN(seed)-b
n=n+1
r[n]=s
ENDWHILE
RETURN,r[0:n]
END
-
Write a program to determine the type of a variable. Test it with a variety
of variable types that you create. An example of such a program is contained
in the notes on IF.
-
Write a program that you could use as a directory of information about
people. Populate the program with data for at least six people and test
it for examples of people in the directory and those not in the directory.
-
EXERCISE FOR SUBMISSION
Write a program for a function IMSIZE that will return the number of bits
in an image of a given size. The operation of the function is described
by the header information given below. You have to create the function
that does this. The results should be submitted to
the instructor.
;+
; s=IMSIZE(M,N,G) returns the number of bits required for an image
; of size M columns by N rows having G gray levels. The function returns
; s=-1 if G is not a positive integer.
;-