\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>typedef<\/span> struct<\/span>\n<\/tt>{\n<\/tt> double<\/span> time;\n<\/tt> double<\/span> y;\n<\/tt>} point;\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n2. Create a main program that has an array of 40 points (or structures), keeps track of the number of values in the array and has variables for each of the values needed to calculate above.<\/b><\/p>\n In the main program we will need an array of points p<\/i><\/b>, an integer with the total number of points values<\/i><\/b>, and 4 double variables, one for each of the functions (I used vert_shift<\/i><\/b>, amp<\/i><\/b>, freq<\/i><\/b>, and phase_shift<\/i><\/b>).<\/p>\n Then you will need to call each function and for testing I printed each value out to make sure everything was coming in right. It is short, simple, and to the point like all programming should be. Your main should look something like this:<\/p>\n \n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt>24\n<\/tt>25<\/strong>\n<\/tt>26\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>int<\/span> main()\n<\/tt>{\n<\/tt> point p[40<\/span>]; \/\/ array of points<\/span>\n<\/tt> int<\/span> values = 0<\/span>; \/\/ total number of values in the points array<\/span>\n<\/tt> double<\/span> vert_shift=0<\/span>; \/\/ vertical shift<\/span>\n<\/tt> double<\/span> amp=0<\/span>; \/\/ amplitude<\/span>\n<\/tt> double<\/span> freq=0<\/span>; \/\/ frequency<\/span>\n<\/tt> double<\/span> phase_shift=0<\/span>; \/\/ phase shift<\/span>\n<\/tt>\n<\/tt> values = read_data(p);\n<\/tt> cout << "<\/span>Values: <\/span>"<\/span><\/span> << endl;\n<\/tt> for<\/span>(lcv=0<\/span>; lcv < values; lcv++)\n<\/tt> cout << "<\/span> <\/span>"<\/span><\/span> << p[lcv].time << "<\/span> <\/span>"<\/span><\/span> << p[lcv].y << endl;\n<\/tt> cout << "<\/span>n<\/span>Total Number of values = <\/span>"<\/span><\/span> << values << endl;\n<\/tt> vert_shift = vertical(p, values);\n<\/tt> cout << "<\/span>n<\/span>Vertical shift = <\/span>"<\/span><\/span> << vert_shift << endl;\n<\/tt> amp = amplitude(p, values);\n<\/tt> cout << "<\/span>n<\/span>Amplitude = <\/span>"<\/span><\/span> << amp << endl;\n<\/tt> freq = frequency(p, values);\n<\/tt> cout << "<\/span>n<\/span>Frequency = <\/span>"<\/span><\/span> << freq << endl;\n<\/tt> phase_shift = pshift(p, values);\n<\/tt> cout << "<\/span>n<\/span>Phase Shift = <\/span>"<\/span><\/span> << phase_shift << endl;\n<\/tt> return<\/span> 0<\/span>;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n3. The main program should then call the read_data function. This function will have one argument (the array of points - or structures - which will be passed as a pointer to the function) and will return the number of values in the array. It should read the data from the data file using a pointer and the arrow (->) operator.<\/b><\/p>\n First, we are going to be reading from a file in this function so we are going to need to include the fstream<\/b> library.<\/p>\n\n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>#inlcude<\/span> <fstream> \/\/ reading from file<\/span>\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nNext, we need to create the prototype for this function called read_data<\/b>. This will go above the main. The read_data<\/b> function will need the array of points and will return the total number of points as an integer. So it should look like this:<\/p>\n\n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>int<\/span> read_data(point *);\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nNow below the main we can create our function and lets call the array of points a<\/i><\/b>. In this function we will need 2 more variables, an integer (total<\/i><\/b>) for counting the number of points that we read from the file and an ifstream variable (f<\/i><\/b>) for opening and closing the file.<\/p>\n First, we need to try to open the file using f.open(\"sp09prog1.txt\")<\/b>. Next, we need to test to make sure the file was opened and that we can read from it and if not then print out error message and exit. If the file was opened then we need to read in the values into the point array and count the number of points using the total<\/i><\/b> integer variable. We do this until we read the end of the file and we find out if we are at the end of the file with f.eof()<\/b>. Finally, we will want to return the total<\/i><\/b>. It should look like this:<\/p>\n\n\n\n1\n<\/tt>2\n<\/tt>3\n<\/tt>4\n<\/tt>5<\/strong>\n<\/tt>6\n<\/tt>7\n<\/tt>8\n<\/tt>9\n<\/tt>10<\/strong>\n<\/tt>11\n<\/tt>12\n<\/tt>13\n<\/tt>14\n<\/tt>15<\/strong>\n<\/tt>16\n<\/tt>17\n<\/tt>18\n<\/tt>19\n<\/tt>20<\/strong>\n<\/tt>21\n<\/tt>22\n<\/tt>23\n<\/tt><\/pre>\n<\/td>\n\n\n<\/tt>int<\/span> read_data(point *a)\n<\/tt>{\n<\/tt> ifstream f; \/\/ file streaming variable<\/span>\n<\/tt> int<\/span> total=0<\/span>; \/\/ counts the total number of points in file<\/span>\n<\/tt>\n<\/tt> f.open("<\/span>sp09prog1.txt<\/span>"<\/span><\/span>);\n<\/tt>\n<\/tt> if<\/span>(f.fail())\n<\/tt> {\n<\/tt> cout << "<\/span>ERROR: File could not be opened<\/span>"<\/span><\/span> << endl;\n<\/tt> exit(1<\/span>);\n<\/tt> }\n<\/tt>\n<\/tt> while<\/span>(!f.eof())\n<\/tt> {\n<\/tt> f >> a->time >> a->y;\n<\/tt> total++;\n<\/tt> a++;\n<\/tt> }\n<\/tt> return<\/span> total - 1<\/span>;\n<\/tt>}\n<\/tt>\n<\/tt><\/pre>\n<\/td>\n<\/tr>\n<\/table>\nNotice that we return total - 1<\/b>. This is because we increment total<\/i><\/b> and then we find out it is the end of the file. So we incremented one too many times so we need to subtract 1 before returning it.<\/p>\n 4. Main should then call the vertical function to calculate the vertical shift. This function has two arguments (the array of points - passed as a pointer - and the number of values in the array). It will calculate the vertical shift by finding the average of the maximum and minimum y-values. You will need to search through the array of points to find these max and min values.<\/b> \n5. Next, main will call the amplitude function to calculate the amplitude. This function has two arguments (the array of points - passed as a pointer - and the number of values in the array; you must also use the arrow operator in this function) and will return the amplitude. To find the amplitude, you need to calculate half the difference between the maximum and minimum values of the y-values (you will need to search through the array for the maximum and minimum values again).<\/b><\/p>\n The functions are both almost identical. The only difference is the equation at the end so we will do them at the same time.<\/p>\n First, we will create the prototypes. Both functions take the array of points and the integer total number of points and they both return a double. So the prototypes should look like this:<\/p>\n | | | | | | | | | |