CODE_C++7Gaddis - Starting Out with C From Control Structures through Objects 7th edition By Tony Gaddis Source Code Chapter 7 Program 7-1 1 This

CODE_C++7Gaddis - Starting Out with C From Control...

This preview shows page 1 - 11 out of 74 pages.

Starting Out with C++: From Control Structures through Objects 7 th edition By Tony Gaddis Source Code Chapter 7
Image of page 1
Program 7-1 1 // This program asks for the number of hours worked 2 // by six employees. It stores the values in an array . 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 const int NUM_EMPLOYEES = 6 ; 9 int hours [ NUM_EMPLOYEES ]; 10 11 // Get the hours worked by each employee. 12 cout << "Enter the hours worked by " 13 << NUM_EMPLOYEES << " employees: "; 14 cin >> hours [ 0 ]; 15 cin >> hours [ 1 ]; (continued…)
Image of page 2
16 cin >> hours [ 2 ]; 17 cin >> hours [ 3 ]; 18 cin >> hours [ 4 ]; 19 cin >> hours [ 5 ] ; 20 21 // Display the values in the array. 22 cout << "The hours you entered are:"; 23 cout << " " << hours [ 0 ]; 24 cout << " " << hours [ 1 ]; 25 cout << " " << hours [ 2 ]; 26 cout << " " << hours [ 3 ]; 27 cout << " " << hours [ 4 ]; 28 cout << " " << hours [ 5 ] << endl; 29 return 0; 30 }
Image of page 3
Program 7-2 1 // This program asks for the number of hours worked 2 // by six employees. It stores the values in an array. 3 #include <iostream> 4 using namespace std; 5 6 int main() 7 { 8 const int NUM_EMPLOYEES = 6; 9 int hours [ NUM_EMPLOYEES ]; 10 int count; 11 12 // Input the hours worked. (continued…)
Image of page 4
13 for (count=0; count < NUM_EMPLOYEES; count++) 14 { 15 cout << "Enter the hours worked by employee " 16 << (count + 1) << ": "; 17 cin >> hours [ count ]; 18 } 19 20 // Display the contents of the array. 21 cout << "The hours you entered are:"; 22 for (count = 0; count < NUM_EMPLOYEES; count++) 23 cout << " " << hours [ count ]; 24 cout << endl; 25 return 0; 26 }
Image of page 5
Program 7-3 1 // This program reads data from a file into an array. 2 #include <iostream> 3 #include <fstream> 4 using namespace std; 5 6 int main() 7 { 8 const int ARRAY_SIZE = 10; 9 int numbers [ARRAY_SIZE]; 10 int count = 0; 11 ifstream inputFile; 12 13 // Open the file. 14 inputFile .open ("TenNumbers.txt"); (continued…)
Image of page 6
15 16 // Read the numbers from the file into the array. 17 while ( count < ARRAY_SIZE && inputFile >> numbers[ count ] ) 18 count++; 19 20 // Close the file. 21 inputFile.close(); 22 23 // Display the numbers read: 24 cout << "The numbers are: "; 25 for (count = 0; count < ARRAY_SIZE; count++) 26 cout << numbers[ count ] << " "; 27 cout << endl; 28 return 0; 29 }
Image of page 7
Program 7-4 1 // This program writes the contents of an array to a file. 2 #include <iostream> 3 #include <fstream> 4 using namespace std; 5 6 int main() 7 { 8 const int ARRAY_SIZE = 10; 9 int numbers [ ARRAY_SIZE ]; 10 int count; 11 ofstream outputFile; 12 14 for ( count = 0; count < ARRAY_SIZE; count++ ) 15 numbers [ count ] = count;
Image of page 8
16 17 // Open a file for output. 18 outputFile .open(" SavedNumbers.txt "); 19 20 // Write the array contents to the file. 21 for ( count = 0; count < ARRAY_SIZE; count++ ) 22 outputFile << numbers [ count ] << endl; 23 24 // Close the file. 25 outputFile .close(); 26 27 // That's it! 28 cout << "The numbers were saved to the file.\n "; 29 return 0; 30 }
Image of page 9
Program 7-5 1 // This program unsafely accesses an area of memory by writing 2 // values beyond an array's boundary. 3 // WARNING : If you compile and run this program, it could crash. 4 #include <iostream> 5 using namespace std; 6 7 int main() 8 { 9 const int SIZE = 3; 10 int values [SIZE]; 11 int count ; 12 13 // Attempt to store five numbers in the three-element array .
Image of page 10
Image of page 11

You've reached the end of your free preview.

Want to read all 74 pages?

  • Spring '11
  • HURLEY
  • Array, cout

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes