arrays eas230

arrays eas230 - if ((x >=0)...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
#include <iostream> #include <string> using namespace std; void changeAge( int indexToChange ); // A simple 3-array table: lastname, firstname, age // Each array has 5 elements, 0-4 string lastName[ 5 ]; string firstName[ 5 ]; int age[ 5 ]; int x; // generic int variable int main() { // populate 3 arrays with data lastName[ 0 ] = "Truman"; firstName[ 0 ] = "Harry"; age[ 0 ] = 175; lastName[ 1 ] = "Garcia"; firstName[ 1 ] = "Jerry"; age[ 1 ] = 55; lastName[ 2 ] = "Smith"; firstName[ 2 ] = "John"; age[ 2 ] = 24; lastName[ 3 ] = "Mouse"; firstName[ 3 ] = "Mickey"; age[ 3 ] = 75; lastName[ 4 ] = "Buckley"; firstName[ 4 ] = "Mike"; age[ 4 ] = 39; // ask the user which record to display cout << "Enter index to display. .."; cin >> x; // check within range 0-4
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: if ((x &gt;=0) &amp;&amp; (x &lt;=4)) { cout &lt;&lt; &quot;Item: &quot; &lt;&lt; x &lt;&lt; endl; cout &lt;&lt; &quot;First Name: &quot; + firstName[ x ] &lt;&lt; endl; cout &lt;&lt; &quot;Last Name: &quot; + lastName[ x ] &lt;&lt; endl; cout &lt;&lt; &quot;Age: &quot; &lt;&lt; age[ x ]; } // call change age function for the user-selected index changeAge( x ); // display the whole table for (int ix=0; ix&lt;=4; ix++) { cout &lt;&lt; &quot; First Name: &quot; &lt;&lt; firstName[ ix ] ; cout &lt;&lt; &quot; Last Name: &quot; &lt;&lt; lastName[ ix ] ; cout &lt;&lt; &quot; Age: &quot; &lt;&lt; age[ ix ] &lt;&lt; &quot; for index &quot; &lt;&lt; ix &lt;&lt; endl; } } // end program // function to change the age of one record void changeAge( int indexToChange ) { int newAge; cout &lt;&lt; endl &lt;&lt; &quot;Enter new age: &quot;; cin &gt;&gt; newAge; age[ indexToChange ] = newAge; }...
View Full Document

Page1 / 2

arrays eas230 - if ((x &amp;amp;gt;=0)...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online