C++
Reading and Writing Files Skills Required Read and write Files Work with arrays and vectors, Create Functions, Include Headers and other files, Loops (while, for), Conditional (if, switch), Datatypes, assignment, etc Basic git commands . Follow the link that was given on Canvas for this Assignment to create your repository. Use the directions and skills from Week 1 to clone the repository into the directory of your choosing. Open the starter solution provided Assignment 1. 2. 3. 4. You may have learned the distance formula in math or physics class. it lets you know the distance between any two points in space using x and y-coordinates in a two-dimensional space. x, y, and z coordinates: The same formula can be modified to work in three-dimensional space if you know both This lab will read in several sets of point coordinates from a file calculate their distances, then write those calculated distances to a file. Introduction to Reading and Writing to Files Using ifstream input file streams) and ofstream (output file streams) variables, you can load in data from one file and out to another. First, you need to create two variables called fin and fout which will correspond to your input and output files ifstream fin("input.txt"); ofstream fout("output.txt"); You can stream in data to or from your variables from input or output files in the same way you use cin and cout to get data to and from the console (keyboard/screen): fin >x; / Reads in data from input.txt to the variable x fout x /1 If you don't know how many data elements you are reading in while (fin.goodc) 11 while the file stil1 has elements to read in fin >> xj For the case of this lab, Writing the Distance Function You need to write a function called cal correspond to the two x, two y, and return that calculation to the main() function as a double. cDistance(). It should take in 6 parameters (all doubles which and two z coordinates), calculate the distance between the two points, Obviously there's no way to type in a Vi into your program, but Cor has a built-in function called sqrt() that looks like this: double sqrt (double x); It takes in a double and returns the square root back There is a function to do exponents as well. You can use the pow) function like so: double pow (double x, double y); 1/ Corresponds to x There is another way to square numbers. Just multiply the number twice (like x x). Breaking down the formula, you should be able to figure out how to take several values and build a function out of it. Storing in Vectors Store the information in vectors. Remember you can resize a vector or use push back to add a new value to a vector. This keeps your program easy to maintain if the number of values in the input file changes. The input file that the grader uses may have a different number of input lines to work with Sorting the Values (Stretch Goal Write out to a new file called output sorted.txt all the distances sorted from lowest to highest. You can use any of the searching algorithms found in your textbook