Thursday, June 26, 2014

The First Lab... and I Only Cried Twice!

The first true obstacle was the first lab presented to the class. The task at hand was taking a string of pseudo-random numbers in a file and creating a program that calculated the minimum, maximum, sum, and average. This was a task that at first glance seemed unachievable, but like the philosopher Ringo Starr once said, "I get by with a little help from my friends." This couldn't have been more true. When login errors made any function on my computer worthless, I was lucky to have David B. at the adjacent computer willing to give assistance when needed.

























I began the program by using <iostream>, <string>, and <fstream>, which allowed me to input and output not only words and variables but also files. The program did not use a string, but in order to remember to include it when needed, I placed in in anyways. "Using namespace std;" allowed us to avoid using "std::" throughout the program whenever working with data.  I streamed in the file and proceeded to define my variables. I established all of them as "float" as opposed to "double" because "float" allows for real numbers with less space taken up. Since the values were not as large size wise, using "double" for the particular set of data would not have been as efficient space wise (but for larger numbers "double" could also be used). The large numbers were defined as 0 and the "min" was defined as 99999 so that the numbers could get larger and smaller, respectively. However, the while loop was the star of the show. It's function let the data be run and adjust the min and max. The if and else if statements allowed the program to change the answers when presented with a criteria meeting number. If the following value exceeded the previous, it became the new max; on the other hand, if the value was less than the previous it became the new min. The sum was established by adding the previous term with the one following. Additionally, the variable "moo" added 1 to itself each time a new number came by (moo++). This made finding the average from the sum simple to get. The program goes to state all the values obtained whenever run: the min, the max, the sum, and the average. Equally important, to conclude the program, the data was exported to a file of its own which contained the answers as well. This was done by using ofstream and creating a new text file.

The program did not always run smoothly. After trying to run it the first time, the program shot out error messages across the program. I naively placed semi-colons where there was no need to. Thankfully, the program shot out errors and indicated the lines where the issue was. This helped to remedy the problem quicker. Near completion, the monitor also displayed each answer as it was modifying what the answer was. To further clarify, each time the program tested whether a number was larger or smaller than the previous (to find the min and max), the program spit out each answer as it went down the list of numbers. Upon further investigation, it became apparent that the key error was placing the "cout" inside of the loop; it therefore spit out the answer each time it checked a new number. The solution here was to put all the "cout"'s outside the loop.

With each corrected mistake I take an additional step forward in becoming a programming master.

The epic journey begins!

Our story starts out with a small town boy with some big town dreams. The goal: to make some programs and chronicle the adventure that will surely follow. Hold on to your keyboards as the drama presents itself.