Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

13: Graphing Functions and Data

Make an empty 600-by-600 Window labeled "Function graphs".

Add an x axis and a y axis each of length 400, labeled "1 == 20 pixels" and with a notch every 20 pixels. The axes should cross at (300,300).

Make both axes red.

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).

Change it to use x scale 20 and y scale 20.

From now on use that range, scale, etc. for all graphs.

Add double slope(double x) { return 0.5*x; } to the window.

Label the slope with a Text "0.5x" at a point just above its bottom left end point.

Add double square(double x) { return x * x; } to the window.

Add a cosine to the window (don't write a new function).

Make the cosine blue.

Write a function sloping_cos() that adds a cosine to slope() (as defined above) and add it to the window.

Define a struct Person containing a string name and an int age.

Define a variable of type Person, initialize it with "Goofy" and 63, and write it to the screen (cout).

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).

Give Person a constructor initializing name and age.

Make the representation of Person private, and provide const member functions name() and age() to read the name and age.

Modify >> and << to work with the redefined Person.

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.

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.

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.

What is a function of one argument?

When would you use a (continuous) line to represent data? When do you use (discrete) points?

What function (mathematical formula) defines a slope?

What is a parabola?

How do you make an x axis? A y axis?

What is a default argument and when would you use one?

How do you add functions together?

How do you color and label a graphed function?

What do we mean when we say that a series approximates a function?

Why would you sketch out the layout of a graph before writing the code to draw it?

How would you scale your graph so that the input will fit?

How would you scale the input without trial and error?

Why would you format your input rather than just having the file contain "the numbers"?

How do you plan the general layout of a graph? How do you reflect that layout in your code?

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 §13.5 is called iterative because 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?

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.

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.

Graph a sine (sin()), a 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.

"Animate" (as in §13.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.

Design and implement a bar graph class. Its basic data is a vector<double> holding N values, and each value should be represented by a "bar" that is a rectangle where the height represents the value.

Elaborate the bar graph class to allow labeling of the graph itself and its individual bars. Allow the use of color.

Here is a collection of heights in centimeters 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.

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 minimize changes when you want to reuse code.

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 labeled points).

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.