18 while count ARRAYSIZE inputFile numbers count 19 Close the file

18 while count arraysize inputfile numbers count 19

This preview shows page 24 - 34 out of 74 pages.

// the number of values that were stored in the array. 18 while ( count < ARRAY_SIZE && inputFile >> numbers [ count ] ) 19 count++; 20 21 // Close the file. 22 inputFile.close(); 23 25 cout << "The numbers are: "; 26 for (int index = 0; index < count ; index++) 27 cout << numbers [index] << " "; 28 cout << endl; 29 return 0; 30 }
Program 7-12 1 // This program uses two parallel arrays : one for hours 2 // worked and one for pay rate. 3 #include <iostream> 4 #include <iomanip> 5 using namespace std; 6 7 int main() 8 { 9 const int NUM_EMPLOYEES = 5 ; 10 int hours [ NUM_EMPLOYEES ]; 11 double payRate [ NUM_EMPLOYEES ]; 12 13 // Input the hours worked and the hourly pay rate. 14 cout << "Enter the hours worked by " << NUM_EMPLOYEES
15 << " employees and their \n" 16 << "hourly pay rates. \n"; 17for (int index = 0; index < NUM_EMPLOYEES ; index++ ) 18 { 19 cout << "Hours worked by employee #" << (index+1) << ": "; 20 cin >> hours [ index ]; 21 cout << "Hourly pay rate for employee #" << (index+1) << ": "; 22 cin >> payRate [ index ]; 23 } 24 25 // Display each employee's gross pay. 26 cout << "Here is the gross pay for each employee:\n"; 27 cout << fixed << showpoint << setprecision(2); (continued…)
28 for( index =0: index < NUM_EMPLOYEES ; index++ ) 29 { 30 double grossPay = hours [ index ] * payRate [ index ]; 31 cout << "Employee #" << (index + 1); 32 cout << ": \$" << grossPay << endl; 33 } 34 return 0; 35 }
Program 7-13 1 // This program demonstrates that an array element is passed 2 // to a function like any other variable. 3 #include <iostream> 4 using namespace std; 5 6 void showValue ( int ); 7 8 int main() 9 { 10 const int size = 8; 11 int numbers [ SIZE ] = {5, 10, 15, 20, 25, 30, 35, 40}; 12 13 for (int index = 0; index < SIZE ; index++ ) 14 showValue ( numbers [ index ]); (continued…)
15 return 0; 16 } // End main 17 18 //**************************************************** 19 // Definition of function showValue Definition of function showValue . 20 // This function accepts an integer argument. 21 // The value of the argument is displayed. 22 //**************************************************** 23 24 void showValue (int num ) 25 { 26 cout << num << " "; 27 }
Program 7-14 1 // This program demonstrates an array being passed to a function. 2 #include <iostream> 3 using namespace std; 4 5 void showValues ( int [ ] , int ); 6 7 int main() 8 { 9 const int ARRAY_SIZE = 8; 10 int numbers [ ARRAY_SIZE ] = {5, 10, 15, 20, 25, 30, 35, 40}; 11 12 showValues ( numbers , ARRAY_SIZE ); 13 return 0; 14 } (continued…)
15 16 //******************************************************** 17 // Definition of function showValue. 18 // This function accepts an array of integers and 19 // the array's size as its arguments. The contents 20 // of the array are displayed. 21 //******************************************************** 22 23 void showValues (int nums [ ] , int size ) 24 { 25 for (int index = 0; index < size ; index++ ) 26 cout << nums [ index ] << " "; 27 cout << endl; 28 }
Program 7-15 1 // This program demonstrates the showValues function being 2 // used to display the contents of two arrays . 3 #include <iostream> 4 using namespace std; 5 6 void showValues ( int [ ] , int ); 7 8 int main() 9 { 10 const int SIZE1 = 8; 11 const int SIZE2 = 5; 12 int set1 [ SIZE1 ] = {5, 10, 15, 20, 25, 30, 35, 40}; 13 int set2 [ SIZE2 ] = {2, 4, 6, 8, 10}; 14 (continued…)
15 16 showValues ( set1 , SIZE1 ); 17 18 19 showValues ( set2 , SIZE2 ); 20 return 0; 21 } 22 23 //******************************************************* 24 // Definition of function showValues Definition of function showValues . 25 // This function accepts an array of integers and 26 // the array's size as its arguments. The contents 27 // of the array are displayed.

You've reached the end of your free preview.

Want to read all 74 pages?

• Spring '11
• HURLEY
• Array
Stuck? We have tutors online 24/7 who can help you get unstuck.
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes