Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- using namespace std;
- int main() // Declare int main so that the program knows where to start
- {
- // Declarations
- double hoursWorked[5]; // This is the array for hoursWorked
- // I have [5] because we have to be inclusize of 4. If you set it to 4, it will do a core dump.
- // Core dump means we went into memory we aren't allowed to be in
- // What actually defines an arry is the [] if the [] weren't there it would just be an int called hoursWorked
- // You can also declare the array as any type of variable seen below
- string dayWorked[5]; // This is the array for the dayWorked
- // Again, [5] to be inclusize of 4
- double total; // We will use this later to total up the hours
- // Initalizations
- // We also have to initalize these to 0. You have to call each one array that you said there would be above
- // In this case we said there will be 0 - 4 so we initalize 0 to 4
- hoursWorked[0] = 0.0; // Remember to initalize 0
- hoursWorked[1] = 0.0; // Initalizing 1
- hoursWorked[2] = 0.0; // Initalizing 2
- hoursWorked[3] = 0.0; // Initalizing 3
- hoursWorked[4] = 0.0; // Initalizing 4
- // Next lets initalize all of those strings we said there would be
- dayWorked[0] = " "; // Initalize 0
- dayWorked[1] = " "; // Initalize 1
- dayWorked[2] = " "; // Initalize 2
- dayWorked[3] = " "; // Initalize 3
- dayWorked[4] = " "; // Initalize 4
- total = 0.0; // Initalize total
- // Input
- // Next lets do something with all these variables
- // Create a for loop that counts up to 5 so we can have all of the days of the week
- for(int i = 0; i < 5; ++i) // First declare a variable. int i = 0; means integer i is equal to zero.
- // We want to declare a variable there because TECHNICALLY if i was declare i outside of that line
- // it would be called a global variable. which isn't allowed
- // then we do i < 5
- // then we tell it at each loop to add 1 to i. ++ means add 1 to the variable
- { // Open the for loop
- cout << "Please enter the day worked: "; // tell the user to enter the day worked
- cin >> dayWorked[i]; // This puts the string they enter into dayWorked and puts it in whatever i equals.
- // This means if i = 1 then we are putting what they enter into dayWorked[1]
- // This goes for the whole loop from 0 to 4
- // the i comes from the int i defined in the for loop line on line 39
- cout << endl << "Please enter the amount of hours worked for that day: "; // Now ask the user to enter the hours
- cin >> hoursWorked[i]; // Again same as above
- // Stores the entered number in hoursWorked[i]
- } // close the for loop
- // Process
- // Now lets do some work with this. We are going to have it total up the hours and give us a total
- total = hoursWorked[0]+hoursWorked[1]+hoursWorked[2]+hoursWorked[3]+hoursWorked[4];
- // The above adds up all of the hoursWorked that were taken in during our above for loop
- // Output
- // Now lets output the array use another for loop
- for(int i = 0; i < 5; ++i) // create the for loop refer to line 42
- { //open the for loop
- cout << "Day: " << dayWorked[i] << endl; // It is going to cout the dayWorked from 0 up to 4
- cout << "Hours for that day: " << hoursWorked[i] << endl; // It is going to cout the hoursWorked from 0 up to 4
- } // close the for loop
- cout << "total hours worked: " << total; // this is going to cout the total hours worked
- // We put the total on the outside of the for loop
- // we do this or else it would output the total everytime the for loop looped
- // Process
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement