arrays eas230

arrays eas230 - if(x>=0&&(x<=4...

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 Document Right 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 >=0) && (x <=4)) { cout << "Item: " << x << endl; cout << "First Name: " + firstName[ x ] << endl; cout << "Last Name: " + lastName[ x ] << endl; cout << "Age: " << age[ x ]; } // call change age function for the user-selected index changeAge( x ); // display the whole table for (int ix=0; ix<=4; ix++) { cout << " First Name: " << firstName[ ix ] ; cout << " Last Name: " << lastName[ ix ] ; cout << " Age: " << age[ ix ] << " for index " << ix << endl; } } // end program // function to change the age of one record void changeAge( int indexToChange ) { int newAge; cout << endl << "Enter new age: "; cin >> newAge; age[ indexToChange ] = newAge; }...
View Full Document

{[ snackBarMessage ]}

Page1 / 2

arrays eas230 - if(x>=0&&(x<=4...

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