// Lab 8 Lecture // comment the examples and run individually or several at once // by commenting out the indicated sections of code. // (Eclipse: ctrl+/ to comment and uncomment) // (Code Blocks: ctrl+shift+C to comment and ctrl+shift+x to uncomment) #include #include using namespace std; int main() { // ------------------------------- Arrays ------------------------------------- // Variables thus far have only been able to store a single value // at a time. However arrays allow you to store multiple values of // the same data type. // // Declaring an empty array of integers with 10 elements: // // int array_name [10]; // // ** Note - your array will be filled with garbage until initialized, // just like a regular variable. ** // Like a regular variable we can declare and initialize // an array at the same time. The syntax is a little different // because we will be assigning multiple values to it. int array_name[10] = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; // This is how our array would look in memory: // ____________________________________________________ // |_10_|_20_|_30_|_40_|_50_|_60_|_70_|_80_|_90_|_100_| << array elements // 0 1 2 3 4 5 6 7 8 9 << index of the element // ** Note - the index starts at 0 and not 1. ** // Once declared, you can access/assign an element individually using the index. // here are a few examples: array_name[0] = 5; cout << array_name[0] << endl; array_name[0] = array_name[1] + array_name[2]; cout << array_name[0] << endl; // --------------- Iterating Through an Array Using a Loop -------------------- // Loops are an array's best friend and are necessary to traverse them. // Here we allow the user to fill an array using a FOR loop char array[5]; cout << endl << "Enter 5 characters" << endl; for(int i = 0; i < 5; i++){ cin >> array[i]; } // Now we print the array back to the screen using another FOR loop for(int i = 0; i < 5; i++){ cout << array[i] << " "; } cout << endl; // -------------------- Going Out of Bounds of an Array ----------------------- // Watch out. It is easy to crash your program with a segmentation fault // by attempting to access data outside of your array, or even // worse it may allow you to access a memory location not allocated to // your array. // What error do you get when you run the following code? double array_2 [10]; cout << "Testing Segmentation fault. Creating an array with 10 elements." << endl; // notice that this will likely run even though it is outside of your array array_2[10] = 100; cout << "array element 11 " << array_2[10] << endl; // This will almost certainly cause an access violation array_2[1000000] = 100; cout << "array element 1000001 " << array_2[1000000] << endl; // ------------------ Understanding the Index of an Array --------------------- // The index of an array is simply an integer. Therefore we can save the // index in a variable so we can reference or modify that element later. // for example we have an array of strings containing the months: const string MONTHS[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; // If the user indicated that their birthday was in June, instead of saving // the string "June" we can instead save the index to June which is 5. int birthdayIndex = 5; // now when we want to print out their birthday month, we can use the following: cout << "My birthday is in " << MONTHS[birthdayIndex] << endl; // Notice how the index is the variable "birthdayIndex". // ------------------ Array Example 1 - Searching an Array -------------------- // Here is a simple algorithm that searches an array for the number 10. int array_4[5] = {4, 9, 10, 32, 44}; // array that will be searched int searchValue = 10; // the value we are looking for int foundIndex = -1; // the index to where it was found. -1 signals "not found". // search array for value for(int i = 0; i < 5; i++) { // if the number we are looking for is found we assign the index // to that element into "foundIndex" so that we can do stuff to it // later. if(searchValue == array_4[i]) { foundIndex = i; } } // print result of search if (foundIndex != -1) { cout << "Found value at index " << foundIndex << endl; } else { cout << "Item not found" << endl; } // ---------------- Array Example 2 - File I/O Fixed Size --------------------- // here we fill an array using input from a file and send // it to an output file. // *IMPORTANT* // Note that we use ARRAY_SIZE constant to specify // the size of our array. This is so that if we decide // to change the size of our array, we only have to change // that variable and not multiple places. Always try // to avoid hard coding literals into your program as it // makes your code more difficult to read and maintain. const int ARRAY_SIZE = 5; int array_5 [ARRAY_SIZE]; ifstream fin; fin.open("input.txt"); if(!fin){ cout << "Error - file not found" << endl; return 1; } // Pull ARRAY_SIZE number of data entries into your // array. Note that if there are fewer entries in your // input file this code will not work properly. for (int i = 0; i < ARRAY_SIZE; i++){ fin >> array_5[i]; } fin.close(); cout << "Your data has been written to output file - output.txt\n"; // Send our data to an output file ofstream fout; fout.open("output.txt"); for(int j = 0; j < ARRAY_SIZE; j++){ fout << array_5[j] << " "; } fout.close(); // --------------- Array Example 2 - File I/O Uncertain Size ------------------ // This also pulls data from a file into an array, but this time we are // uncertain of how many elements are inside the file. Therefore, we use // a variable 'count' to keep track of how many items we pulled in. const int ARRAY_SIZE_2 = 100; int array_3 [ARRAY_SIZE_2]; int count; ifstream fin2; fin2.open("input.txt"); if(!fin2){ cout << "Error - file not found" << endl; return 1; } count = 0; // while the array is not full and the file is not empty, // pull data from file and put it into the next array element. while(count < ARRAY_SIZE_2 && fin >> array_3[count]){ count++; } fin2.close(); // print array to screen for(int k = 0; k < count; k++){ cout << array_3[k] << " "; } cout << endl; // Now lets send our data to an output file ofstream fout2; fout2.open("output.txt"); for(int j = 0; j < count; j++){ fout2 << array_3[j] << " "; } fout.close(); return 0; }