/*======== Fall 2024 - CS 111 Week 14 Lab Exercise date: 2024-12-06 ========*/ /*--- USING pair-programming * COPY and PASTE the contents of this file into a lab14.cpp file within the CS50 IDE * ADD the parts asked for below (one student saying what to type, the other student typing it into the CS50 IDE) * each time you want to compile: in a CS50 terminal that is open to the folder CONTAINING this .cpp file, type: g++ lab14.cpp -o lab14 * IF it compiles with no errors: to run: in that same CS50 terminal that is open to the folder CONTAINING this .cpp file, type: ./lab14 * When you are satisfied with its output, create an example output file by typing: ./lab14 > lab14-out.txt * Download copies of your resulting lab14.cpp and lab14-out.cpp ******** and ALSO the file your program creates, array-contents.txt ******** ...by right-clicking on their names in the file explorer on the left of the CS50 IDE, and use Gmail to MAIL a copy of ALL THREE of these files to BOTH of you. * And, EACH of you should SUBMIT these ***THREE*** files lab14.cpp, lab14-out.txt, and array-contents.txt to Canvas ---*/ /*--- by: PUT BOTH of YOUR NAMES HERE last modified: 2024-12-06 ---*/ /*--- WEEK 14 LAB EXERCISE - PROBLEM 1 ---*/ /*--- Below, add the #include needed for stream-based file input/output in C++ ---*/ #include <cstdlib> #include <iostream> #include <string> #include <cmath> using namespace std; /*--- WEEK 14 LAB EXERCISE - PROBLEM 2 --- */ /*--- Purpose: to practice writing another function with an array parameter Use the design recipe to design a function indexed_display, that expects an array of string values and its size, has the side effect of printing to the screen the strings in that array, one array element per line, BUT each PRECEDED by its array index, a colon, and a blank**, and returns the number of lines printed. ** ALTERNATE OPTION 1: you can use a dash, or a blank, or a set of square brackets, or some combination, to tastefully set apart the array index and the string at that index For example, if you have: string pets[3] = {"Snoopy", "Ginger", "Caesar"}; then: indexed_display(pets, 3) == 3 ...and has the side-effect of printing to the screen: 0: Snoopy 1: Ginger 2: Caesar (BE SURE to write at least TWO tests for this, using at least two DIFFERENT arrays of DIFFERENT sizes.) ---*/ /*--- signature: purpose: tests: ---*/ /*--- this week - this main function will give you a chance to: * test the function above, * practice a bit more with arrays, * practice writing to a file, and * practice reading from a file. ----*/ int main() { cout << boolalpha; cout << "*** Testing: indexed_display ***" << endl; /*--- WEEK 14 LAB EXERCISE - PROBLEM 3 ---*/ // Declare an array of at least 10 string elements of your choice // * give it an appropriate descriptive name // * initialize it as you would like /*--- WEEK 14 LAB EXERCISE - PROBLEM 4 ---*/ cout << endl; // Call your indexed_display function with your array from // Problem 3 and its size as its arguments. // * NOTE: we don't need what indexed_display returns in this // particular call -- so write this function call expression // like a statement, by itself and followed by a semicolon! /*--- WEEK 14 LAB EXERCISE - PROBLEM 5 ---*/ // Declare an OUTPUT file stream with an appropriate name of // your choice // Open this output file stream so that it will be able to // write to a file named array-contents.txt // Now write a loop to write the contents of your array // from Problem 3 to the file array-contents.txt, // one array element per line. // Close your output file stream /*--- WEEK 14 LAB EXERCISE - PROBLEM 6 ---*/ // Declare an INPUT file stream with an appropriate name of // your choice // Open this input file stream so that it will be able to // read from the file array-contents.txt // Now, YOU GET TO CHOOSE: // OPTION 1: read at least three things from the file // array_contents.txt, printing to the screen what // you read in a readable, attractive fashion. // OPTION 2: use a while loop to read ALL of the things // from the file array_contents.txt, printing to the screen what // you read in a readable, attractive fashion. // ...and for either option, you also get to CHOOSE whether // to use getline or >> or some combination! // // (but *if* you do >> FOLLOWED BY getline, // put an EXTRA getline before your actual intended getline, // to read "past" the previous entry's newline -- // see the lab instructor if you are trying this // and have questions!) // Close your input file stream // REMEMBER: SUBMIT your resulting array-contents.txt along with // your lab14.cpp and lab14-out.txt files return EXIT_SUCCESS; }