Showing posts with label 15. Show all posts
Showing posts with label 15. Show all posts

Monday, 31 August 2020

Chapter 15 // Exercise 11 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 11

Find the average maximum temperatures for each month of the year for two or more locations (e.g., Cambridge, England, and Cambridge, Massachusetts; there are lots of towns called "Cambridge") and graph them together. As ever, be careful with axes, labels, use of color, etc.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2015/Exercise%2011


I should've spend a lot longer on this instead of hacking something together but I really want to move onto the next chapter as graphing data is not my idea of fun...besides excel can do it much nicer and faster and there's got to be a C# library out there somewhere that allows you to call Excel graphs from within your code.


Chapter 15 // Exercise 11 - Principles & Practice Using C++

With that though, I'm finally onto the last graphics chapter.

Sunday, 30 August 2020

Chapter 15 // Exercise 10 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 10

What kind of data is unsuitable for a line graph or a bar graph? Find an example and find a way of displaying it (e.g., as a collection of labelled points).

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Exercise%2010

If you have a graph that is "x amount of people voted yes and y voted no"; this would make a great bar chart. However, charts such as "Age and time spent watching Filthy Frank" would make a better scatter graph. An example from Maths Is Fun; Icecream Sales and Temperature:

Chapter 15 // Exercise 10 - Principles & Practice Using C++

The red line is there due to my laziness with the notch spacing. If the top Y value is more than the length, you end up with teeny tiny gaps between each notch. 

Saturday, 29 August 2020

Chapter 15 // Exercise 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 9

Find another data set of heights (an inch is 2.54cm) and graph them with your program from the previous exercise. For example, search the web for "height distribution" or "height of people in the United States" and ignore a lot of rubbish or ask your friends for their heights. Ideally, you don't have to change anything for the new data set. Calculating the scaling from the data is a key idea. Reading in labels from input also helps minimise changes when you want to reuse code.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/exercise%209


For this one I quickly googled "height data set" on images then used the first image that came up. It contained height and weight pairs. I have no idea what unit they were in, I guessed kg and cm but they seem a bit low for that but whatever.


Chapter 15 // Exercise 9 - Principles & Practice Using C++

Friday, 28 August 2020

Chapter 15 // Exercise 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 8

Here is a collection of heights in centimetres together with the number of people in a group of that height (rounded to the nearest 5cm): (170, 7), (175,9), (180, 23), (185, 17), (190, 6), (195, 1). How would you graph that data? If you can't think of anything better, do a bar graph. Remember to provide axes and labels. Place the data in a file and read it from that file.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2015/Exercise%208


I thought about doing a scatter graph as we have Marks already implemented. Then because it would be as "simple" as replacing the rectangles with marks I decided to create a new class that could hold x and y data but then through inheritance it could be given different functionality.

I also changed how the notches were drawn as originally they were set as ints, causing quite a bit of data lost due to rounding down. CustomGraph though draws axes to fit the window and has modifiable labels. How the data is drawn though is up to the child class.

This isn't the best graph in the world but it reads in from a file or a vector:
Chapter 15 // Exercise 8 - Principles & Practice Using C++


Monday, 24 August 2020

Chapter 15 // Exercise 6, 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 6, 7

6. Design and implement a bar graph class. Its basic data is a vector<double> holding N values, and each value should represented by a "bar" that is a rectangle where the height represents the value.
7. Elaborate the bar graph class to allow labeling of the graph itself and its individual bars. Allow the use of colour.


Exercise 6 - The basic bar graph with just bars set to certain heights:

Chapter 15 // Exercise 6, 7 - Principles & Practice Using C++

This takes in a vector of doubles and resizes itself depending on the window size. The number of notches are also dependent on the size of the graph and how many bars there are.

Exercise 7 - It's not exactly excel but it'll do. The bars are coloured by random ints so occasional bars may be identical. The notch marking on the Y axis also isn't entirely accurate as it's rounded down in the Axis constructor but whatever. The bar labels and fill colours can be turned off by just providing a bool value and you can set the axis labels to anything once you've created the barchart object.

Chapter 15 // Exercise 6, 7 - Principles & Practice Using C++



Thursday, 20 August 2020

Chapter 15 // Exercise 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 5

"Animate" (as in section 15.5) the series 1-1/3+1/5-1/7+1/9-1/11+.... It is known as Leibniz's series and converges to pi/4.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Exercise%205

This took me a while to wrap my head around and I'm still not entirely convinced at how the lambda expression works but whatever.

So pi/4 == 0.7853989 (not precisely but you have to stop somewhere)

The problem with this exercise is that each frame looks identical as it gets closer to pi/4. Probably not the best function to graph. Either that or I've completely miscalculated.

Wednesday, 19 August 2020

Chapter 15 // Exercise 4 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 4

Graph a sine(sin()),  cosine (cos()), the sum of those (sin(x)+cos(x)), and the sum of the squares of those (sin(x)*sin(x)+cos(x)*cos(x)) on a single graph. Do provide axes and labels.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Exercise%204

.

Chapter 15 // Exercise 4 - Principles & Practice Using C++

Tuesday, 18 August 2020

Chapter 15 // Exercise 3 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 3

Modify Fct from the previous exercise to take an extra argument to control precision or whatever. make the type of that argument a template parameter for extra flexibility.

Github: Not done.

This was very confusing. I'm not good with templates; I don't particularly like them. By 'control precision' I thought he meant change doubles to ints or something but at the end of it all I have to convert the points into ints anyway as my version of Point only uses int.

And what does he want by it being a templated type? Would the precision mean "to this decimal place?" If it's templated, whats to stop someone from passing a string in? I'm so confused.

I've decided to just leave it as I don't understand what he wants me to do.

Chapter 15 // Exercise 2 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and modified versions of the graphics files used throughout the chapters. You can find those versions through the link below.

Chapter 15 // Exercise 2

Define a class Fct that is just like Function except that it stores its constructor arguments. Provide Fct with "reset" operations, so that you can use it repeatedly for different ranges, different functions, etc.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2015/Exercise%202


In this one took me a while as I pondered over how to re-write the points that had already been added to a shape once it's drawn. I thought about deleting the shape and creating a new one but the easiest way was to simply provide a function in Shape that clears the points vector. That way no new shapes are made and StoredFct can just call Function.

Sunday, 16 August 2020

Chapter 15 // Exercise 1 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017.

Chapter 15 // Exercise 1

Here is another way of defining a factorial function:

int fac(int n) { return n > 1 ? n *fac(n - 1) : 1; }  // factorial n!

It will do fac(4) by first deciding that since 4 > 1 it must be 4 * fac(3), and that's obviously 4 * 3 * fac(2), which again is 4 * 3 *2 * fac(1), which is 4 * 3 * 2 * 1. Try to see that it works. A function that calls itself is said to be recursive. The alternative implementation in section 15.5 is called iterative bcause it iterates through the values (using while). Verify that the recursive fac() works and gives the same results as the iterative fac() by calculating the factorial of 0, 1, 2, 3, 4, up until and including 20. Which implementation of fac() do you prefer, and why?

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Exercise%201


When using int, at 13 this will begin to overflow and return the wrong numbers. 

I also did a quick benchmark test between the two functions and there isn't really any difference. I'd say half the time, the recursive function was faster by 0.002 nanoseconds. Sometimes iterative was a bit slower or recursive was slower. They seem to be pretty balanced speed-wise. 

I looked at the disassembly and realised why they perform the same; the compiler produces the exact same assembly for both functions. I've kept this code in so you can run it yourself a few times.

So it comes down to preference. I prefer the recursive as it's less code and at a glance; easier to reason about what's happening. A lot of people don't like the ternary operator though so I can understand why the iterative version would be chosen instead.

Saturday, 15 August 2020

Chapter 15 // Class Definition Drills 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the header file std_lib_facilities found here:
https://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 15 // Class Definition Drills

9. Change the representation of Person to have first_name and second_name instead of name. Make it an error not to supply both a first and a second name. Be sure to fix >> and << also. Test.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Class%20Definition%20Drills/Drill%209

.

Friday, 14 August 2020

Chapter 15 // Class Definition Drills 8 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the header file std_lib_facilities found here:
https://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 15 // Class Definition Drills

8. Read a sequence of Persons from input (cin) into a vector<Person>; write them out again to the screen (cout). Test with correct and erroneous input.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Class%20Definition%20Drills/Drill%208

.

Thursday, 13 August 2020

Chapter 15 // Class Definition Drills 1, 2, 3, 4, 5, 6, 7 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the header file std_lib_facilities found here:
https://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h

Chapter 15 // Class Definition Drills

1. Define a struct Person containing a string name and an int age.
2. Define a variable of type Person , initialise it with "Goofy" and 63, and write it to the screen (cout).
3. Define an input (>>) and an output (<<) operator for Person; read in a Person from the keyboard (cin) and write it out to the screen (cout).
4. Give Person a constructor initialising name and age.
5. Make the representation of Person private, and provide const member functions name() and age() to read the name and age.
6. Modify >> and << to work with the redefined Person.
7. Modify the constructor to check that age is [0:150] and that name doesn't contain any of the characters ; : " ' [] * & ^ % $ # @ ! . Use error()  in case of error. Test.

Github: https://github.com/l-paz91/principles-practice/blob/master/Chapter%2015/Class%20Definition%20Drills/Drills1-7

Wednesday, 12 August 2020

Chapter 15 // Shape Function Graphing Drills 1, 2, 3, 4, 5, 6, 7, 8, 9 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 15 // Function Graphing Drills

1. Graph the function  double one(double x) {return 1;} in the range [-10,11] with (0,0) at (300,300) using 400 points and no scaling (in the window).
2. Change it to use x scale 20 and y scale 20.
3. From now on use that range, scale, etc. for all graphs.
4. Add double slope(double x) {return x/2;} to the window.
5. Label the slope with a Text "x/2" at a point just above its bottom left end point.
6. Add double square(double x) {return x*x;} to the window.
7. Add a cosine to the window (don't write a new function).
8. Make the cosine blue.
9. Write a function sloping_cos() that adds a cosine to slope() (as defined above) and add it to the window.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2015/Function%20Graphing%20Drills%202


All of these functions can be found from section 15.2 to 15.3.2. If you have already followed along with the chapter you will have done them.

Drill 2-1:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-2:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-3, 2-4:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-5:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-6:
It annoys me that this one doesn't scale
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-7, 2-8:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Drill 2-9:
Chapter 15 //  Shape Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

Friday, 7 August 2020

Chapter 15 // Function Graphing Drills 1, 2, 3, 4, 5 - Principles & Practice Using C++

In this exercise I am using Visual Studio 2017 and the graphics files found here:
https://github.com/l-paz91/principles-practice/tree/master/Graphics%20Files

Chapter 15 // Function Graphing Drills

1. Make an empty 600-by-600 Window labeled "Function Graphs".
2. Note that you'll need to make a project with the properties specified in the "installation of FLTK" note from the course website.
3. You'll need to move Graph.cpp and Window.cpp into your project.
4. Add an x axis and a y axis each of length 400, labeled "1 == 20 pixels" and with a notch ever 20 pixels. The axes should cross at (300,300).
5. Make both axes red.

Github: https://github.com/l-paz91/principles-practice/tree/master/Chapter%2015/Function%20Graphing%20Drills


I am lazy and I've been using the same project since the start of Chapter 12.




Wednesday, 3 August 2016

Chapter 4 // Exercise 11, 12, 13, 14, 15 - Principles & Practice Using C++

In all these exercises I am using Visual Studio Community 2015 and the header file "std_lib_facilities.h" which can be found here:


http://www.stroustrup.com/Programming/PPP2code/std_lib_facilities.h


My version is spelt differently so adjust the code accordingly if copying and pasting.


Chapter 4 Exercise // 4.11

Create a program to find all the prime numbers between 1 and 100. One way to do this is to write a function that will check if a number is a prime (i.e., see if the number can be divided by a prime smaller than itself) using a vector of primes in order (so that if the vector is called primes, primes[0]==2, primes[1]==3, primes[2]==5, etc). Then write a loop that goes from 1 to 100, checks each number to see if it is a prime, and stores each prime found in a vector. Write another loop that lists the primes you found. You might check your result by comparing your vector of prime number with primes. Consider 2 the first prime.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

vector<int> user_primes; // vector to put found primes into
vector<int> primes{ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,}; // to compare against

bool isItPrime(int n) 
{
for (int p = 0; p < user_primes.size(); ++p)
{
if (n % user_primes[p] == 0)
return false;
}
return true;

}

int main()
{
user_primes.push_back(2);

for (int i = 3; i <= 100; ++i)
{
if (isItPrime(i))
user_primes.push_back(i);
}

for (int i = 0; i < primes.size(); ++i)
{
cout << primes[i] << '\t' << user_primes[i] << '\n';
}

keep_window_open();

return 0;
}

I spent so many hours trying to solve this one, so many hours scouring the internet but they all solved it using methods that hadn't been taught yet. I then even found Bjarne himself answer this question using methods he had fucking taught yet (talk about not even reading your own book). The way the question is written it sounds like he is implicitly asking you to check all numbers from 1 to 100 are prime by dividing them by a smaller number from within another vector called primes containing primes numbers from 1 - 100. Talk about misleading. He actually wants you to just write a function that finds a prime and then returns that number into a vector. 

So basically I had to end up using a bool function which I still don't fully understand. In chapters 1 - 4 all he has said is that a bool is true or false. I also had to start at 3 otherwise it would never work (even though he says to start at 1 in the book, his own website solves this starting at 3. I was rage quitting for hours). So it checks to see if the number is divisible by any primes already pushed back (so obviously, primes smaller than itself).

For a better understanding of bool values, look here. This is the website I use when Bjarne makes no fucking sense. The author Alex, makes things much easier to understand. 

This was a terribly written exercise.

Chapter 4 Exercise // 4.12

Modify the program described in the previous exercise to take an input value max and then find all the prime numbers from 1 to max.

#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

vector<int> user_primes; // vector to put found primes into
vector<int> primes{ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,}; // to compare against

bool isItPrime(int n)
{
for (int p = 0; p < user_primes.size(); ++p)
{
if (n % user_primes[p] == 0)
return false; // n divided
}
return true; // n couldn't be divided

}

int main()
{
user_primes.push_back(2);

cout << "Please give a maximum number the computer should stop finding primes at:\n";
int max;
cin >> max;

for (int i = 3; i <= max; ++i)
{
if (isItPrime(i))
user_primes.push_back(i);
}

for (int i = 0; i < user_primes.size(); ++i)
{
cout << '\n' << user_primes[i];
}

cout << '\n';
keep_window_open();

return 0;
}



Chapter 4 Exercise // 4.13

Create a program to find all the prime numbers between 1 and 100. there is a classic method for doing this, called the "Sieve of Eratosthenes." Write your program using this method.

At this point in my programming knowledge, I honestly don't know how to do this using only methods he has shown us so far in the book. I've looked everywhere and I just don't understand whats going on. If someone could perhaps explain it to me in dunce terms that would be greatly appreciated but until I'm a more proficient programmer the answer to this one (and below) will have to wait.

EDIT 17/09/2019 - 3 years later I have now completed exercises 13 & 14. You can find the code for them at my git repository: https://github.com/l-paz91/principles-practice/tree/master/Chapter%204

I followed the pseudocode given on Wikipedia here: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes#Algorithmic_complexity

This time is took me around 30 minutes to solve and I'm quite happy to see just how much my ability to read other code (pseudo or not) has progressed. It also is very possible to do this exercise using methods only shown up to chapter 4 but my problem solving skills were not as developed. I still have a long way to go but I've improved. 

Basically though, this method is quite a contrived way of doing it but I understand why he made us do it. I don't fully understand how it works, but the pseudocode is simple enough to follow to get it working.

Chapter 4 Exercise // 4.14

Modify the program described in the previous exercise to take an input value max and then find all the prime numbers from 1 to max.



Chapter 4 Exercise // 4.15

Write a program that takes an input value n and then finds the first n primes.


#include "stdafx.h"
#include "std_lib_facilities_new_version.h"
using namespace std;

vector<int> user_primes; // vector to put found primes into
vector<int> primes{ 2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,}; // to compare against

bool isItPrime(int n)
{
for (int p = 0; p < user_primes.size(); ++p)
{
if (n % user_primes[p] == 0)
return false; // n divided
}
return true; // n couldn't be divided

}

int main()
{
user_primes.push_back(2);

cout << "What number do you want to find primes from?: \n";
int countFrom;
cin >> countFrom;

double maxPrime = countFrom * 100;

for (int i = 3; i <= maxPrime; ++i)
{
if (isItPrime(i))
user_primes.push_back(i);
}

int countTo = countFrom*2;

for (int i = countFrom-1; i <= countTo-2; ++i) //this starts at n and continues n times
{
cout << '\n' << user_primes[i];
}

cout << '\n';
keep_window_open();

return 0;
}

I was a little confused on this one for a while as I thought he meant enter a number (n) and then find that many primes starting from the beginning. But then I realised that was exactly the same as 4.12, so he actually meant start at 'n' and then find the next 'n' numbers from 'n'. 

After some messing around it wasn't too hard, the function to find primes doesn't actually need to be tinkered all that much, you just need to make sure that it finds enough primes to print out.