<!DOCTYPE OMNotebook>
<Notebook>
 <GroupCell closed="false" >
  <GroupCell closed="false" >
   <TextCell style="Title" >
    <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Arial; font-size:38pt; font-weight:600; color:#000000;">Exercise 3&lt;/p>&lt;/body>&lt;/html></Text>
   </TextCell>
   <GroupCell closed="false" >
    <TextCell style="Section" >
     <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:18pt; font-weight:600; color:#000000;">while-statements&lt;/p>&lt;/body>&lt;/html></Text>
    </TextCell>
    <TextCell style="Text" >
     <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">Write a class that calculates the sum: 11*1-&lt;span style=" font-size:14pt;"> &lt;/span>12*2&lt;span style=" font-size:14pt;"> + &lt;/span>13*3&lt;span style=" font-size:14pt;"> - &lt;/span>14*4 &lt;span style=" font-size:14pt;">+ &lt;/span>15*5&lt;span style=" font-size:14pt;"> - &lt;/span>16*6&lt;span style=" font-size:14pt;"> ... &lt;/span>&lt;br />Follow the algorithm:&lt;br />        1. Initialize the sum to 0 and the first term to 1.&lt;br />        2. If the absolut value of next term &amp;gt;= 0.000001 do the next two steps:&lt;br />            2.1 Add next term to the sum.&lt;br />            2.2 Calculate a new next term.        &lt;br />            &lt;/p>&lt;/body>&lt;/html></Text>
    </TextCell>
    <InputCell style="Input" closed="true" >
     <Input></Input>
     <Output></Output>
    </InputCell>
    <GroupCell closed="true" >
     <TextCell style="Subsection" >
      <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:14pt; font-weight:600; color:#000000;">Tip&lt;/p>&lt;/body>&lt;/html></Text>
     </TextCell>
     <TextCell style="Text" >
      <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">Every term in the sum is on the form 1/(k*k), where k increaces by one for each step. Set a variable to keep track of if the next term is negative or positive. When the program is run the sum will be 0.82247.    &lt;/p>&lt;/body>&lt;/html></Text>
     </TextCell>
    </GroupCell>
    <GroupCell closed="true" >
     <TextCell style="Subsection" >
      <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:14pt; font-weight:600; color:#000000;">Answer&lt;/p>&lt;/body>&lt;/html></Text>
     </TextCell>
     <GroupCell closed="true" >
      <TextCell style="Subsubsection" >
       <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:12pt; font-weight:600; color:#000000;">SumCalculation&lt;/p>&lt;/body>&lt;/html></Text>
      </TextCell>
      <TextCell style="Text" >
       <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">If the operator &lt;span style=" font-family:Courier New;">abs&lt;/span> is used outside a when clause, state events are triggered. If this is not desired, the &lt;span style=" font-family:Courier New;">noEvent&lt;/span> function can be applied to it.&lt;/p>&lt;/body>&lt;/html></Text>
      </TextCell>
      <InputCell style="Input" closed="true" >
       <Input>class SumCalculation
  constant Real e = 0.000001;
  Real sum(start = 0);
  Real next(start = 1.0);
  Integer k(start = 1);        
  Integer posneg(start = 1);    // if the sign in front of the term is + or -
algorithm
  while noEvent(abs(next)) >= e loop  
    sum := sum + next;        // Add the sum to the next term
    k := k + 1;            // calculate a new next
    posneg := - posneg;
    next := posneg/(k*k);
  end while;
end SumCalculation;</Input>
       <Output></Output>
      </InputCell>
     </GroupCell>
     <GroupCell closed="true" >
      <TextCell style="Subsubsection" >
       <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times; font-size:12pt; font-weight:600; color:#000000;">Simulation of SumCalculation&lt;/p>&lt;/body>&lt;/html></Text>
      </TextCell>
      <InputCell style="Input" closed="true" >
       <Input>simulate( SumCalculation );</Input>
       <Output></Output>
      </InputCell>
      <TextCell style="Output" >
       <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Courier New; font-size:11pt; color:#000000;">&lt;/p>&lt;/body>&lt;/html></Text>
      </TextCell>
      <TextCell style="Text" >
       <Text>&lt;html>&lt;head>&lt;meta name="qrichtext" content="1" />&lt;/head>&lt;body style=" white-space: pre-wrap; font-family:MS Shell Dlg; font-size:8.25pt; font-weight:400; font-style:normal; text-decoration:none;">&lt;p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-family:Times New Roman; font-size:12pt; color:#000000;">When the program is run the sum will be 0.82247 and it will calculate the sum of:     11*1-&lt;span style=" font-size:14pt;"> &lt;/span>12*2&lt;span style=" font-size:14pt;"> + &lt;/span>13*3&lt;span style=" font-size:14pt;"> - &lt;/span>14*4 &lt;span style=" font-size:14pt;">+ &lt;/span>15*5&lt;span style=" font-size:14pt;">  ... - &lt;/span>11001*1001&lt;/p>&lt;/body>&lt;/html></Text>
      </TextCell>
      <InputCell style="Input" closed="true" >
       <Input>{sum[1]
, next[1]
, posneg[1]
, k[1]

}
</Input>
       <Output></Output>
      </InputCell>
     </GroupCell>
    </GroupCell>
   </GroupCell>
  </GroupCell>
 </GroupCell>
</Notebook>
