[Skip Navigation] [CSUSB] / [CNS] / [CSE] / [R J Botting] >> [CSE201] >> [Lab01] >> index
[Index] [Schedule] [Syllabi] [Glossary] [Labs] [Projects] [Resources] [Grading] [Search ]
Notes: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20]
Labs: [01] [02] [03] [04] [05] [06] [07] [08] [09] [10]
Tue Mar 15 15:12:14 PDT 2011

Contents


    cs201/lab10 -- Vectors

      Previous [ ../lab09/ ]

      Deliverables

      By the end of the lab you should have worked with several programs that use vectors.

      Process

      1. Set up a cs201/lab09 directory to work in.
      2. Download [ vectors.cpp ] the example program for the handout. Verify that it works as described in the comment.
      3. Add a function to vectors.cpp that adds up all the numbers in a given vector and returns the sum:
      4. sum(v) = Σ[i](v[i]).
         	int sum( const vector <int> & v ) { /*your code here */ }
        Note -- this function outputs nothing. It silently returns the sum to the main program that does this
         		cout << sum(v) << "\n";
      5. Add instructions to the main program vectors.cpp that test your int sum(vector <int>&) function. Test it...
      6. Now add and test a function
         	double mean(const vector <int> & v) { /*Your code here*/ }
        Hint: mean(...) should call sum(...) and use code like this:
         	return sum(v)/static_cast<double>(v.size());
        Note: the algorithm is similar but simpler than the one you used in lab07 because you know the size of the vector. Again -- the "cout" should be outside the function and the mean is returned.
      7. Now add and test a function that returns the maximum or biggest item:
         	int max(const vector <int> & v) { /*Your code here*/ }
      8. Next return the smallest or minimum value.
         	int min(const vector <int> & v) { /*Your code here*/ }
      9. Next -- range should be easy, reusing max(v) and min(v).
         	int range(const vector <int> & v) { /*Your code here*/ }
        Return max(v)-min(v).
      10. Next -- the sum of the squares of the values:
      11. sum_of_squares(v) = Σ[i](v[i]*v[i]),
      12. Next -- the population variance can be found as
      13. variance(v) = (sum_of_squares(v) - mean(v)*sum(v))/(v.size()-1),
         	double variance( const vector <int> & v ) { /*your code here */ }
      14. Next -- standard deviation -- using sqrt in cmath:
      15. sd(v) = √(variance(v)),
         	double sd( const vector <int> & v ) { /*your code here */ }
        Note -- Formulas for standard deviation are on page 154 of Horstmann & Budd.

      . . . . . . . . . ( end of section Process) <<Contents | End>>

      Grading

      The more functions you complete the better the grade. Getting as far as standard deviation should be worth an A.

      Work on projects or something else

      This is just for grins and giggles.... you can declare a vector of strings
       	vector <string> grid;
      and use it to plot functions. It should be like the experiments you did in [ ../lab08/ ] but you can display the graphs the right way up: x horizontally and y = f(x) vertically. Note: you can borrow some code from [ lab08/ ] .... but I figure you've got about 60 minutes work to do for this project.

    Abreviations

  1. Gnu::="Gnu's Not Unix", a long running open source project that supplies a very popular C++ compiler.
  2. TBA::="To Be Announced", something I have to do.
  3. TBD::="To Be Done", something you have to do.

End