IDL HW 1

 

1)    Write a main-level script that starts with two variables, A and B, which are defined at the command line, and swaps their values. Make A an integer and B a string. Have the script print a statement about what it did, provide evidence of the swap and print the results. Here’s an example of what I typed at the command line and the resulting output:

 

IDL> a = 10

IDL> b = ‘cat’

IDL> .run swap_em

% Compiled module: $MAIN$.

In the beginning

A =       10 and B = cat

This is what HELP said:

A               INT       =       10

B               STRING    = 'cat'

I swapped the two variables and now

A = cat and B =       10

And now this is what HELP says:

A               STRING    = 'cat'

B               INT       =       10

 

HINT: You’ll need to use the string() function.

 

2)    Write a program called eight_ball.pro that randomly prints one of 6 responses to a question, just like a Magic Eight-ball. The 6 responses are

 

Cannot Predict Now

Concentrate and Ask Again

My Answer is Yes

My Answer is No

Very Doubtful

It is Certain

 

HINT: You can obtain a random number between 0 and 1 using randomu(), e.g.

 

IDL> print,randomu(seed)

          0.704078

IDL> print,randomu(seed)

          0.873373

 

3)    Write a procedure named fib.pro that prints the Fibonacci sequence up to some maximum number, which is specified by the user. For example

 

IDL> fib, 9

       1

       1

       2

       3

       5

       8

 

4)    Modify your Fibonacci procedure to optionally print the sequence as a series of characters. The user can specify what character to use. For example

 

IDL> fib, 9, char='*'

*

*

**

***

*****

********

 

HINT: You my find the strlen() function useful. Here’s an example of strlen() in action:

 

IDL> var = 'johnjohn'

IDL> print,strlen(var)

                          8

IDL> var = 'cat'     

IDL> print,strlen(var)

                       3