Answered step by step
Verified Expert Solution
Link Copied!

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 findTheError(const 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=0;
for (int i = A; i <= B; i++){
sum = sum + Pnts[i];
}
double avg=0;
avg = sum /(B - A +1); //B - A: difference between starting and ending
// by adding 1 we include the A itself
//the sum of squared differences from the average
double error =0;
for (int i = A; i <= B; ++i){
error += pow(Pnts[i]- avg, 2); //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 minError(const vector& Pnts, int n, int N){
if (n ==0 && N ==0){//if there are no points or groups
return 0;
}
double result = INT_MAX; //initializing the result with the largest/max 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 -1; p >=0; --p){
// calculating the current error
double currentError= minError(Pnts, p, N -1)+ findTheError(Pnts, p, n -1);
// update result
if (currentError < result){
result= currentError;
}
}
return result;
}
//a function that prints the groups
void Grps(const vector& Pnts, const vector& Groups){
int A =0;
for (int i=0;i> N;
vector Pnts ={1,2,3,6,8,12,13,15}; //initializing the vector with the examole's points
vector Groups ={2,4,2}; //Example of the groups with the best error beacause i couldntt figure out how to find the groups that have the 8.66667 error
//printing the groups
cout<< "The groups are:" <

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning Microsoft SQL Server 2012 Programming

Authors: Paul Atkinson, Robert Vieira

1st Edition

1118102282, 9781118102282

More Books

Students also viewed these Databases questions