Answered step by step
Verified Expert Solution
Question
1 Approved Answer
#include #include #include #include using namespace std; / / function that calculates the error / / A:the starting index of the subarray points / /
#include
#include
#include
#include
using namespace std;
function that calculates the error
A:the starting index of the subarray "points"
B:the ending index of the subarray "points"
double findTheErrorconst vector& Pnts int A int B
here the vector ins a constant
so that the values of the vector cannot be changed within the function
calculating the average
double sum;
for int i A; i B; i
sum sum Pntsi;
double avg;
avg sum B A ; B A: difference between starting and ending
by adding we include the A itself
the sum of squared differences from the average
double error ;
for int i A; i B; i
error powPntsi avg, ; the given equation from the task that finds the error
return error; returns the error that it found
function that calculates the partition error
n: number of points
N: number of groups
double minErrorconst vector& Pnts int n int N
if n && N if there are no points or groups
return ;
double result INTMAX; initializing the result with the largestmax value so thats why i included the climits library
the loop goes through possible ending positions
from the last group and
it keeps updating the error result.
for int p n ; p ; p
calculating the current error
double currentError minErrorPnts p N findTheErrorPnts p n ;
update result
if currentError result
result currentError;
return result;
a function that prints the groups
void Grpsconst vector& Pnts const vector& Groups
int A ;
for int i;i N;
vector Pnts ; initializing the vector with the examole's points
vector Groups ; Example of the groups with the best error beacause i couldntt figure out how to find the groups that have the error
printing the groups
cout "The groups are:"
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started