<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" xmlns:m="http://www.w3.org/1998/Math/MathML" id="new">
  <name>Programming in LabVIEW MathScript-Simple For Loop Exercises</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2006/08/02 12:33:43.933 GMT-5</md:created>
  <md:revised>2006/08/03 00:46:08.346 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="aantonac">
      <md:firstname>Anthony</md:firstname>
      <md:othername>Gene</md:othername>
      <md:surname>Antonacci</md:surname>
      <md:email>aantonac@utk.edu</md:email>
    </md:author>
      <md:author id="morrell">
      <md:firstname>Darryl </md:firstname>
      
      <md:surname>Morrell</md:surname>
      <md:email>morrell@asu.edu</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="aantonac">
      <md:firstname>Anthony</md:firstname>
      <md:othername>Gene</md:othername>
      <md:surname>Antonacci</md:surname>
      <md:email>aantonac@utk.edu</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist>
    <md:keyword>Exercises</md:keyword>
    <md:keyword>for loop</md:keyword>
    <md:keyword>LabVIEW</md:keyword>
    <md:keyword>MathScript</md:keyword>
  </md:keywordlist>

  <md:abstract>This module provides several simple exercises designed to test and increase your understanding of for loops in LabVIEW MathScript.</md:abstract>
</metadata>
  <content>
<section id="SecReview">
	  <name>Some For Loop Exercises</name>

<exercise id="ExAa">
<problem id="ProbAa">
<name>Loop Indices</name>
<para id="ParaAa">
How many times will this program print "Hello World"?
<code type="block">
for a=0:50
    disp('Hello World')
end
</code>
</para>
</problem>
<solution>
<para id="SolAa"> The code <code>0:50</code> creates a vector of integers starting at 0 and going to 50; this vector has 51 elements. "Hello World" will be printed once for each element in the vector (51 times).
</para>
</solution>
</exercise>

<exercise id="ExAb">
<problem id="ProbAb">
<name>Loop Indices II</name>
<para id="ParaAb">
How many times will this program print "Guten Tag Welt"?
<code type="block">
for a=-1:-1:-50
    disp('Guten Tag Welt')
end
</code>
</para>
</problem>
<solution>
<para id="SolAb">The code <code>-1:-1:-50</code> creates a vector of integers starting at -1 and going backward to -50; this vector has 50 elements. "Guten Tag Welt" will be printed once for each element in the vector (50 times).</para>
</solution>
</exercise>

<exercise id="ExAc">
<problem id="ProbAc">
<name>Loop Indices III</name>
<para id="ParaAc">
How many times will this program print "Bonjour Monde"?
<code type="block">
for a=-1:1:-50
    disp('Bonjour Monde')
end
</code>
</para>
</problem>
<solution>
<para id="SolAc">The code <code>-1:1:-50</code> creates an  empty vector  with no elements. Since this is an empty vector the following error occurs: "Error in function range at line 1.  You cannot specify a step size of zero for a range."</para>
</solution>
</exercise>

<exercise id="ExA">
<problem id="ProbA">
<name>Nested Loops</name>
<para id="ParaA">
How many times will this program print "Hola Mundo"?
<code type="block">
for a=10:10:50
    for b=0:0.1:1
        disp('Hola Mundo')
    end
end
</code>
</para>
</problem>
<solution>
<para id="SolA"> The outer loop (the loop with <code>a</code>) will be executed five times. Each time the outer loop is executed, the inner loop (the loop with <code>b</code>) will be executed eleven times, since  <code>0:0.1:1</code> creates a vector with 11 elements. "Hola Mundo" will be printed 55 times.</para>
</solution>
</exercise>

<exercise id="ExTwo">
<problem id="ProbTwo">
<name>A tricky loop</name>
<para id="ParaTwo">
What sequence of numbers will the following for loop print?
<code type="block">
n = 10;
for j = 1:n
    n = n-1;
    j
end
</code>
Explain why this code does what it does.
</para>
</problem>
<solution>
<para id="SolTwo">In the first line, the value of <code>n</code> is set to 10.  The code <code>1:n</code> creates a vector of integers from 1 to 10.  Each iteration through the loop sets <code>j</code> to the next element of this vector,  so <code>j</code> will be sent to each value 1 through 10 in succession, and this sequence of values will be printed.  Note that each time through the loop, the value of <code>n</code> is decreased by 1; the final value of <code>n</code> will be 0. Even though the value of <code>n</code> is changed in the loop, the number of iterations through the loop is not affected, because the vector of integers is computed once before the loop is executed and does not depend on subsequent values of <code>n</code>.
</para>
</solution>
</exercise>

<exercise id="ExThree">
<problem id="ProbThree">
<name>Nested Loops II</name>
<para id="ParaThree">
What value will the following program print?
<code type="block">
count = 0;
for d = 1:7
   for h = 1:24
      for m = 1:60
         for s = 1:60
            count = count + 1;
         end
      end
   end
end
count
</code>
What is a simpler way to achieve the same results?
</para>
</problem>
<solution>
<para id="SolThree">The  <code>d</code> loop will be executed seven times. In each iteration of the <code>d</code> loop, the <code>h</code> loop will be executed 24 times. In each iteration of the <code>h</code> loop, the <code>m</code> loop will be executed 60 times. In each iteration of the <code>m</code> loop, the <code>s</code> loop will be executed 60 times. So the variable <code>count</code> will be incremented
<m:math>
  <m:apply>
    <m:eq/>
      <m:apply>
        <m:times/>
          <m:cn> 7 </m:cn>
          <m:cn> 24 </m:cn>
          <m:cn> 60 </m:cn>
          <m:cn> 60 </m:cn>
      </m:apply>
    <m:cn> 604800 </m:cn>
  </m:apply>
</m:math>
times.</para>
<para id="SolThreea">
A simpler way to achieve the same results is the command
<code type="block">
7*24*60*60
</code>
</para>
</solution>
</exercise>

<exercise id="ExOne">
<problem id="ProbOne">
<name>Multiple Hypotenuses</name>
<para id="ParaOne">
<figure id="RTriag">
  <media type="application/postscript" src="RT.eps">
    <media type="image/png" src="RT.png"/>
  </media>
  <caption>
    Sides of a right triangle.
  </caption>
</figure>
Consider the right triangle shown in <cnxn target="RTriag"/>. Suppose you wish to find the length of the hypotenuse <m:math> <m:ci> c </m:ci> </m:math> of this triangle for several combinations of side lengths <m:math> <m:ci> a </m:ci> </m:math> and <m:math> <m:ci> b </m:ci> </m:math>; the specific combinations of <m:math> <m:ci> a </m:ci> </m:math> and <m:math> <m:ci> b </m:ci> </m:math> are given in <cnxn target="SideLen"/>. Write a LABVIEW MATHSCRIPT's program to do this.
<table id="SideLen" frame="all">
  <name>Side Lengths</name>
  <tgroup cols="2" colsep="1" rowsep="1">
    <thead>
      <row>
        <entry><m:math> <m:ci> a </m:ci> </m:math></entry>
        <entry><m:math> <m:ci> b </m:ci> </m:math></entry>
      </row>
    </thead>
    <tbody>
      <row>
        <entry align="center">1</entry>
        <entry align="center">1</entry>
      </row>
      <row>
        <entry align="center">1</entry>
        <entry align="center">2</entry>
      </row> 
      <row>
        <entry align="center">2</entry>
        <entry align="center">3</entry>
      </row>
      <row>
        <entry align="center">4</entry>
        <entry align="center">1</entry>
      </row>
      <row>
        <entry align="center">2</entry>
        <entry align="center">2</entry>
      </row>
    </tbody>
  </tgroup>
</table>

</para>
</problem>
<solution>
<para id="SolOne">This solution was created by Heidi Zipperian:
<code type="block">
a=[1 1 2 4 2]
b=[1 2 3 1 2]
for j=1:5
    c=sqrt(a(j)^2+b(j)^2)
end
</code>
A solution that does not use a for loop was also created by Heidi:
<code type="block">
a=[1 1 2 4 2]
b=[1 2 3 1 2]
c=sqrt(a.^2+b.^2)
</code></para>
</solution>
</exercise>

</section>
  </content>
  
</document>
