Homework #4#
Completing this assignment
For the each of the problems below, write a separate C++ program
named in the problem problemN.cpp, where N is the
problem number.
Important
Make sure your that g++ can compile your code. For some of
the problems here, you will need to use -std=c++20.
Upload your C++ source files (not the executables produced by g++) to Brightspace.
Important
All work must be your own.
You may not use generative AI / large-language models for any part of this assignment.
In our vector example, Averaging, we computed the average of a vector of data.
Starting with this example, extend the code to compute the standard deviation of the vector, defined as:
\[\sigma = \sqrt{ \frac{1}{N} \sum_{i=1}^N (x_i - \mu)^2 }\]where \(\mu\) is the average.
Have your code output the average and standard deviation.
solution
#include <cmath> #include <format> #include <iostream> #include <vector> int main() { std::vector<double> vec{1.0, 2.2, 10.5, 21.3, 25.4, 6.6, 4.2}; double sum{}; for (auto e : vec) { sum += e; } double avg = sum / vec.size(); double sigma{}; for (auto e : vec) { sigma += std::pow(e - avg, 2.0); } sigma = std::sqrt(sigma / vec.size()); std::cout << std::format("The average / std dev is: {:.3f} +/- {:.3f}\n", avg, sigma); }
Consider a
structthat defines a point in the 2D plane:struct Point { double x; double y; };
You want to compute the distance from the origin for a collection of
Pointobjects. For this problem, we will work with the following points \((x, y)\):\[(1.5, 2.3), (3.2, 1.6), (5.3, 0.1), (8.6, 2.5)\]Your task:
Create a
std::vectorthat holdsPointdata and initialize it with the data given above.Loop over the points in your vector and compute the distance from the origin, \(d = \sqrt{x^2 + y^2}\), for each point
Within the loop body, output the distance to the screen using
std::formatto give a line for each point of the form:Point ( 1.50, 2.30) is 2.746 from the origin.
solution
#include <cmath> #include <format> #include <iostream> #include <vector> struct Point { double x; double y; }; int main() { std::vector<Point> points{Point{.x=1.5, .y=2.3}, Point{.x=3.2, .y=1.6}, Point{.x=5.3, .y=0.1}, Point{.x=8.6, .y=2.5}}; for (auto p : points) { double dist = std::sqrt(std::pow(p.x, 2.0) + std::pow(p.y, 2.0)); std::cout << std::format("Point ({:5.2f}, {:5.2f}) is {:6.3f} from the origin.\n", p.x, p.y, dist); } }
Tip
Alternately, we could use std::hypot to compute the distance. This will do the calculation just like we did here, but adds additional logic for floating point overflows.
Let’s find the maximum element in a vector. The
algorithmlibrary providesstd::max(a, b)which will return whichever ofaorbis the largest.Starting with the vector:
std::vector<double> vec{-100.3, 5.64, -25.3, 10.9, 50.3, -2.4, 13.9};
With a program that uses a ranged-for loop (what we’ve been using in class) and
std::maxto find the maximum value in the vector.Tip
You’ll want to start with the maximum value initialized to a very small value so the first element you check in the vector will become the maximum. Using
std::numeric_limitscan help here.Note
You should add:
#include <algorithm>
to your code to get access to
std::max.solution
#include <algorithm> #include <iostream> #include <limits> #include <vector> int main() { std::vector<double> vec{-100.3, 5.64, -25.3, 10.9, 50.3, -2.4, 13.9}; double max{std::numeric_limits<double>::lowest()}; for (auto e : vec) { max = std::max(e, max); } std::cout << "the maximum element is " << max << std::endl; }
Tip
Later we’ll see that can use the std::max_element function for this. But we need to learn about pointers first.