Advertisement
andrefecto

Working with arrays

Dec 4th, 2013
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. using namespace std;
  4.  
  5. int main() // Declare int main so that the program knows where to start
  6. {
  7.  
  8. // Declarations
  9.  
  10. double hoursWorked[5]; // This is the array for hoursWorked
  11.                     // I have [5] because we have to be inclusize of 4. If you set it to 4, it will do a core dump.
  12.                     // Core dump means we went into memory we aren't allowed to be in
  13.                     // What actually defines an arry is the [] if the [] weren't there it would just be an int called hoursWorked
  14.                     // You can also declare the array as any type of variable seen below
  15. string dayWorked[5]; // This is the array for the dayWorked
  16.                      // Again, [5] to be inclusize of 4
  17. double total;        // We will use this later to total up the hours
  18.  
  19. // Initalizations
  20.  
  21.                       // We also have to initalize these to 0. You have to call each one array that you said there would be above
  22.                       // In this case we said there will be 0 - 4 so we initalize 0 to 4
  23. hoursWorked[0] = 0.0; // Remember to initalize 0
  24. hoursWorked[1] = 0.0; // Initalizing 1
  25. hoursWorked[2] = 0.0; // Initalizing 2
  26. hoursWorked[3] = 0.0; // Initalizing 3
  27. hoursWorked[4] = 0.0; // Initalizing 4
  28.                       // Next lets initalize all of those strings we said there would be
  29. dayWorked[0] = " ";   // Initalize 0
  30. dayWorked[1] = " ";   // Initalize 1
  31. dayWorked[2] = " ";   // Initalize 2
  32. dayWorked[3] = " ";   // Initalize 3
  33. dayWorked[4] = " ";   // Initalize 4
  34.  
  35. total = 0.0;          // Initalize total
  36.  
  37. // Input
  38.  
  39.                     // Next lets do something with all these variables
  40.  
  41.                                                 // Create a for loop that counts up to 5 so we can have all of the days of the week
  42.                     for(int i = 0; i < 5; ++i)  // First declare a variable. int i = 0; means integer i is equal to zero.
  43.                                                 // We want to declare a variable there because TECHNICALLY if i was declare i outside of that line
  44.                                                 // it would be called a global variable. which isn't allowed
  45.                                                 // then we do i < 5
  46.                                                 // then we tell it at each loop to add 1 to i. ++ means add 1 to the variable
  47.                     { // Open the for loop
  48.  
  49.                         cout << "Please enter the day worked: "; // tell the user to enter the day worked
  50.                         cin >> dayWorked[i];                     // This puts the string they enter into dayWorked and puts it in whatever i equals.
  51.                                                                  // This means if i = 1 then we are putting what they enter into dayWorked[1]
  52.                                                                  // This goes for the whole loop from 0 to 4
  53.                                                                  // the i comes from the int i defined in the for loop line on line 39
  54.  
  55.                         cout << endl << "Please enter the amount of hours worked for that day: "; // Now ask the user to enter the hours
  56.                         cin >> hoursWorked[i];                                                    // Again same as above
  57.                                                                                                   // Stores the entered number in hoursWorked[i]
  58.                     } // close the for loop
  59.  
  60. // Process
  61.  
  62.                      // Now lets do some work with this. We are going to have it total up the hours and give us a total
  63.                      total = hoursWorked[0]+hoursWorked[1]+hoursWorked[2]+hoursWorked[3]+hoursWorked[4];
  64.                      // The above adds up all of the hoursWorked that were taken in during our above for loop
  65.  
  66. // Output
  67.  
  68.                      // Now lets output the array use another for loop
  69.  
  70.                      for(int i = 0; i < 5; ++i) // create the for loop refer to line 42
  71.  
  72.                      { //open the for loop
  73.  
  74.                          cout << "Day: " << dayWorked[i] << endl; // It is going to cout the dayWorked from 0 up to 4
  75.                          cout << "Hours for that day: " << hoursWorked[i] << endl; // It is going to cout the hoursWorked from 0 up to 4
  76.  
  77.                      } // close the for loop
  78.                          cout << "total hours worked: " << total; // this is going to cout the total hours worked
  79.                                                                   // We put the total on the outside of the for loop
  80.                                                                   // we do this or else it would output the total everytime the for loop looped
  81. // Process
  82.  
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement