<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5 plus MathML//EN" "http://cnx.rice.edu/cnxml/0.5/DTD/cnxml_mathml.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:m="http://www.w3.org/1998/Math/MathML" xmlns:bib="http://bibtexml.sf.net/" id="id37465911">
  <name>Arrays</name>
  <metadata>
  <md:version>1.1</md:version>
  <md:created>2008/05/12 08:25:04.139 GMT-5</md:created>
  <md:revised>2008/05/12 08:27:01.139 GMT-5</md:revised>
  <md:authorlist>
      <md:author id="dtanh">
      <md:firstname>Anh</md:firstname>
      <md:othername>Tuan</md:othername>
      <md:surname>Duong</md:surname>
      <md:email>tam.vohoang@gmail.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist>
    <md:maintainer id="dtanh">
      <md:firstname>Anh</md:firstname>
      <md:othername>Tuan</md:othername>
      <md:surname>Duong</md:surname>
      <md:email>tam.vohoang@gmail.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  

  <md:abstract/>
</metadata>
  <content>
    <section id="id-445502358615">
      <name>1. OBJECTIVE </name>
      <para id="id34581781">The objective of Lab session 4 is to get familiar with arrays, one-dimensional and two-dimensional arrays. </para>
    </section>
    <section id="id-548134244471">
      <name>2. EXPERIMENT</name>
      <para id="id37260840">2.1 Test the following program. </para>
      <para id="id38477417">
        <code>#include &lt;iostream.h&gt;</code>
      </para>
      <para id="id38506799">
        <code>const int NUM = 8;</code>
      </para>
      <para id="id36008019">
        <code>void main()</code>
      </para>
      <para id="id34157435">
        <code>{</code>
      </para>
      <para id="id23220948">
        <code>int nums[NUM];</code>
      </para>
      <para id="id22777166">
        <code>int total = 0; // Holds total of user’s eight numbers.</code>
      </para>
      <para id="id21558309">
        <code>int ctr;</code>
      </para>
      <para id="id34838253">
        <code>for (ctr=0; ctr&lt;NUM; ctr++)</code>
      </para>
      <para id="id21871208">
        <code>{ </code>
      </para>
      <para id="id38471479">
        <code>cout &lt;&lt; “Please enter the next number...”;</code>
      </para>
      <para id="id21199732">
        <code>cin &gt;&gt; nums[ctr];</code>
      </para>
      <para id="id22502470">
        <code>total += nums[ctr]; </code>
      </para>
      <para id="id38510081">
        <code>}</code>
      </para>
      <para id="id5372311">
        <code>cout &lt;&lt; “The total of the numbers is “ &lt;&lt; total &lt;&lt; “\n”;</code>
      </para>
      <para id="id22162038">
        <code>return;</code>
      </para>
      <para id="id37474372">
        <code>}</code>
      </para>
      <para id="id20949982">2.2 If the array weights is declared as in the following statement, then what the value of weights[5] is ?</para>
      <para id="id19753863">int weights[10] = {5, 2, 4};</para>
      <para id="id22688928">2.3 Given the statement:</para>
      <para id="id37417751">
        <code>char teams[] = {‘E’,’a’,’g’,’l’,’e’,’s’,’\0', ’R’, ‘a’,’m’,’s’,’\0'};</code>
      </para>
      <para id="id34073184">which of the following statement is valid?</para>
      <para id="id35227521">a. cout &lt;&lt; teams;</para>
      <para id="id33975632">b. cout &lt;&lt; teams+7;</para>
      <para id="id38522205">c. cout &lt;&lt; (teams+3);</para>
      <para id="id23780227">d. cout &lt;&lt; teams[0];</para>
      <para id="id36381425">e. cout &lt;&lt; (teams+0)[0];</para>
      <para id="id22502876">f. cout &lt;&lt; (teams+5);</para>
      <para id="id21146833">2.4 Given the array declaration:</para>
      <para id="id23357110">
        <code>int grades[3][5] = {80,90,96,73,65,67,90,68,92,84,70, 55,95,78,100};</code>
      </para>
      <para id="id36959095">Determine the value of the following subscripted variables:</para>
      <para id="id22201328">a. grades[2][3]</para>
      <para id="id38221557">b. grades[2][4]</para>
      <para id="id4345377">c. grades[0][1]</para>
      <para id="id39029867">2.5 Write a C++ program that inputs an array consisting of n single-precision floating point numbers and finds the smallest element in the array. (n is an integer that is entered by the user).</para>
      <para id="id23832056">2.6 Write a C++ program that inputs an integer array and finds the last element in the array.</para>
      <para id="id3583960">2.7 Write a C++ program that inputs an integer array and then inserts the integer value X in the first position in the array.</para>
      <para id="id35641528">2.8 Write a C++ program that inputs an integer array and checks if all the elements in the array are unique (i.e. we can not find any pair of elements that are equal to each other).</para>
      <para id="id35808868">2.9 Write a C++ program that inputs a matrix and then transposes it and displays the transposed matrix. Transposing a square matrix is to swap:</para>
      <para id="id20226104">a(i,j) &lt;==&gt; a(j,i) for all i, j</para>
      <para id="id37467420">For example, given the matrix</para>
      <figure id="id22867924">
        <media type="image/png" src="graphics1.png">
          <param name="height" value="75"/>
          <param name="width" value="71"/>
        </media>
      </figure>
      <para id="id22883171">After transposing it, it becomes as follows:</para>
      <figure id="id20914110">
        <media type="image/png" src="graphics2.png">
          <param name="height" value="77"/>
          <param name="width" value="73"/>
        </media>
      </figure>
      <para id="id4321878">2.10 Write a C++ program that inputs an integer n and two square matrices with order of n. Then the program calculates the multiplication of the two matrices and displays the resultant matrix.</para>
    </section>
  </content>
</document>
