CS106B Assessment 1

The URS

  1. Setup compiler and get working.  Began 25/08/2017.  Finished
  2. Get a program running that generates a hash code. Finished 25/08/2017
  3. Write and program to generate “perfect numbers”. Finished 28/08/2017
  4. Write a program that uses random/io libraries to calculate election validity.  Finished 29/08/2017
  5. Write a graphics program exploring the “Chaos Game”. Finished 08/09/2017
  6. Write a program that utilizes the Soundex algorithm that classifies surnames based on how they sound.  Finished 11/09/2017 (same day started).
  7. Write a program that displays a histogram from a file of exam scores.  Finished 14/09/2017

Refer to the Standford document for details – H09-Assign1SimpleC++.

  • 9 days to program
  • Began 25/08/2107
    Finished: 14/09/2017
  • Other things intruded into life.

Program Setup

Basic compiler setup

Easy

CS106B - Working Compiler

CS106B – Working Compiler

Find the two bugs in the source code and produce a hash.  Bug one was obvious without reading the hints in the assessment pdf.  Running through the deugger, ‘GetLIne’ was not declared in this scope.  That needed to be fixed.  The solution was obvious after a while.

CS106B Asses1 Hash Output

CS106B Asses1 Hash Output

Perfect number determination

This one was a little tricky.  The return on my tracker int was in the wrong place so 24 was returned as perfect .  The return needed to be outside of the loop where I’d placed it.  The code then worked correctly.  However, at that point I was using main to do my calculations, not a predicate function.

CS106B Asses1 Perfect Number

CS106B Asses1 Perfect Number not using a function

Turns out I was calling the function wrong – saying “bool IsPerfect (int n)” instead of “IsPerfect (n)”.  I also ditched the console for something different.

CS106B Asses1 Perfect Number using a predicate function

CS106B Asses1 Perfect Number using a predicate function

Here’s the code without the fancy pants maths…

int main() {

for (int n = MIN_RANGE; n < MAX_RANGE; n++) {
IsPerfect (n) ;
if (result == true) cout << n << ” is a perfect number!\n”;
}

return 0;
}

Simulation and Use of Random/io Libraries

The program setup was pretty easy and I had everything excluding the random/invalid election result formula in place.  Before finalising the program, I needed to work out how to take, voterNumbers, electionSpread and voterError and then get some validity figure out the other side.  I built the code up little by little until I got something out the other end that looked correct (beta testing showed it was).

The basic program setup is as follows:

int main() {
ProgramPurpose();
CollectData();
SimulateElections(iNumberVoters, dPercentageSpread, dVoterError);
CalculateValidity(dInvalidElections);
PrintResult();

return 0;
}

CS106B Asses1 Random

CS106B Asses1 Random

Running the “completed” code for the first time took an age. I’d included error checking cout’s and after 10 mins or so, the program started getting sluggish.  Ditching these sped things up.   The code could do with some cleaning up, but it works as far as I can tell, though the invalid election rate seems high at the input values (inputting stuff all error does create 0 to not many invalid results) and looking at some sample code on github after I finished my coding shows the same logic I’ve used.  Must be correct then though it looks like I’m out by a factor of 10.

Graphics

Here the instructions in the PDF were outdated so I needed to figure out which of Standford’s C++ libraries were needed to get the job done.  Things got tricky here.  If I initialized a graphics window before main, calling it would result in a compiler error.

Calling it in a function would throw a “not declared in scope” error.  Additionally, trying to set up mouse listening was a pain as though the code seemed to be correct “nothing” was happening.  Lots of back and forth between the information contained here: https://stanford.edu/~stepp/cppdoc to figure out things.  The first round of code spelt everything out rather than used something like a vector to capture and store my click references so the plan was to fancifiy the code. Whacking things into vector really reduced the code length.

Some strange errors cropped up.  I was pushing the mouse click coordinates into a vector that should have been readable from any function and the second loop created to build the polygon was getting crazy data out of the vector.  That was due to a “{” being in the wrong place.  Next I nearly went crazy trying to figure out why the polygon was a single (long) line.  My “x” and “y” cords were the same in the code.  I replaced an “x” with a “y” and the polygon drew correctly after that.

Another bug was using randomVertex when I should have been using newRandomVertex.

Lots of code crimping for this one so I could figure out what did what.  I also tested this on shapes with >3 vertices.  Things did not turn out so pretty – just random dots.

The finished chaos game

The finished chaos game

While looking at other’s code for this, the line Randomize() appeared.  This did not affect my code and I did not use it.  Getting on to Lecture 6, this bit of code ensures the random function starts off with a different number than the previous run.

Strings via “Soundex”

With having coded the remove character function earlier, this section was made a little easier as I could reuse/modify code I’s used myself.  On challenge I came across was the RemoveRepeats function.  I needed to get it to run over and over until there were no repeats (removing a repeat could result in a repeat and multiple repeats would not shrink down to one.).  Amazingly, the first bit of test code for this worked.

One bit of code I was not happy with was the search and replace function.  Lot of repeated code.  A switch statement would have been a more elegant solution (my hangman code would be a good place to go to grab the code for that).

Overall things went ok here, but I got the really wacky errors seen when creating the remove character function.  As far as I can tell the code is correct (obviously it is not).  Argh!  Additionally, typing strangebehaviorfollows, hangs the program…as do certain other words.  The program works with variations on my name.  It does not if I was following the test script (output of both shown).  Program code here for posterity.

File Processing & Vectors

A lot of faffing around before my brain locked into gear.  At one point, I had the scores.txt file being read and the lines being put into a vector arrScores.  Cleaning up the error check comments broke the code.  It looked like eof was not being parsed (or it was saying end of file before reading anything).  Another 30 mins of trying to debug had me on Stanford website where I saw their libraries had either been updated with snazzy new abilities since 2007, or they had features the lectures did not enlighten me about.    This meant that about 40 lines (including comments) of code could be condensed into two.  Now I had no need to write my own GetFilename and OpenFile functions.  This probably defeats the purpose of getting students to work out their own file open and error check code, but hey, in the real world, efficiency is everything and I’d learned the basics of using push_back to put data into an array and how to write open an read file code from scratch any way, so it was short cut time.

Version 1.0 wonky code.

Version 2.0 final code.  I don’t use Stanford’s vector.h class so I may have done things “the hard way”.

Throughout this code, I read and manipulated the data as strings.  I could have manipulated an int – that would have negated the need to convert a tracker into to a string for comparison, but the program works and I learned stuff. so all is good.

CS106B Assessment 1 - Exam scores

CS106B Assessment 1 – Exam scores

Creating the Program Including Beta Testing

Interestingly, the need to include genlib.h has been mothballed.  The instructions are now in iostream.  Any discussion on program creation is above.

The Shipped Programs

As above.

Program Modifications / Extensions

None required.

SummarySave

Save

Save

Summary

Easy going until I got to the graphics section, then a fair bit of frustration.  One thing I have noticed is the debugger’s fairly informative and my favorite errors appear to be using “,” or “:” when I should be using
“;”.

Lessons Learned

  • How to work with booleans properly (that was hurting my brain all through CS106A).
  • How to use Typedef to simplify the use of some data types.
  • How to convert an integer to a string.