开发者

BCB 6.0 "raised exception class EAccessViolation with message 'Access violation at address'"

开发者 https://www.devze.com 2023-02-25 03:45 出处:网络
I\'m newer to C++. I have written some code, but when i run it, there\'s always this: raised exception class

I'm newer to C++. I have written some code, but when i run it, there's always this:

raised exception class EAccessViolation with message 'Access violation at address'

i don't understand this. Would you like to help me solve it? It's important to me. Really, really thank you!

#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <math.h>
#include <conio.h>
#define k 2
#define minoffset 0.5

using namespace std;

struct Point
{
  double X;
  double Y;
};

vector<Point> dataprocess();
void k_means(vector<Point> points,int N);

double getdistance(Point p1,Point p2)
{   double distance;
    distance=sqrt((p1.X-p2.X)*(p1.X-p2.X)+(p1.开发者_JAVA百科Y-p2.Y)*(p1.Y-p2.Y));
    return distance;
}
int getmindis(Point p,Point means[])
{
    int i;
    int c;
    double dis=getdistance(p,means[0]);
    for(i=1;i<k;i++)
    {
       double term=getdistance(p,means[i]);
       if(term<dis)
       {
          c=i;
          dis=term;
       }
    }
    return c;
}



Point getmeans(vector<Point> points)
{
    int i;
    double sumX,sumY;
    Point p;
    int M=points.size();
    for(i=0;i<M;i++)
    {
       sumX=points[i].X;
       sumY=points[i].Y;
    }
    p.X=sumX/M;
    p.Y=sumY/M;
    return p;
}


int main()
{   int N;
    vector<Point> stars;
    stars=dataprocess();
    N=stars.size();
    cout<<"the size is:"<<N<<endl;

    k_means(stars,N);
    getch();
}

vector<Point> dataprocess()
{
   int i;
   int N;
   double x,y;
   vector<Point> points;


   Point p;
   string import_file;

   cout<<"input the filename:"<<endl;
   cin>>import_file;
   ifstream infile(import_file.c_str());

   if(!infile)
   {
      cout<<"read error!"<<endl;
   }

   else
   {
      while(infile>>x>>y)
      {
         p.X=x;
         p.Y=y;
         points.push_back(p);
      }
   }
    N=points.size();
    cout<<"output the file data:"<<endl;
    for(i=0;i<N;i++)
    {
        cout<<"the point"<<i+1<<"is:X="<<points[i].X<<"   Y="<<points[i].Y<<endl;
    }

    return points;
}

void k_means(vector<Point> points,int N)
{
    int i;
    int j;
    int index;
    vector<Point> clusters[k];
    Point means[k];
    Point newmeans[k];
    double d,offset=0;
    bool flag=1;
    cout<<"there will be"<<k<<"clusters,input the original means:"<<endl;

    for(i=0;i<k;i++)
    {
        cout<<"k"<<i+1<<":"<<endl;
        cin>>means[i].X>>means[i].Y;
    }

    while(flag)
    {
        for(i=0;i<N;i++)
        {
            index=getmindis(points[i],means);
            clusters[index].push_back(points[i]);
        }

        for(j=0;j<k;j++)
        {
            newmeans[j]=getmeans(clusters[j]);
            offset=getdistance(newmeans[j],means[j]);
        }

        if(offset>d)
        {
            d=offset;
        }
        flag=(minoffset<d)?true:false;
        for(i=0;i<k;i++)
        {
            means[i]=newmeans[i];
            clusters[i].clear();
        }
    }

    for(i=0;i<k;i++)
    {
     cout<<"N"<<i+1<<"="<<clusters[i].size()<<endl;
     cout<<"the center of k"<<i+1<<"is:"<<means[i].X<<"  "<<means[i].Y<< endl;
    }
}


You surely have some algo errors in you code. It is difficult to deal with code without input data, that caused an error, but let's try:

  • First, lets look at function Point getmeans(vector<Point> points)

it is supposed to evaluate mean coordinates for cluster of points: if you pass an empty cluster to this function it will cause an error:

look here - int M=points.size()

and here - for(i=0;i<M;i++) { sumX=points[i].X; sumY=points[i].Y; }

if your cluster is empty than M will be zero and you loop will iterate 2^31 times (until 32 bit integer overflow) and each time you will try to read values of nonexistent vector items

So, You have to test if you vector is not empty before running main function loop and you have to decide which mean values should be assigned for zero cluster (May be you need an additional flag for empty cluster which will be checked before dealing with cluster's mean values)

  • Then lets examine function int getmindis(Point p,Point means[]) and, also, a place, where we call it: index=getmindis(points[i],means); clusters[index].push_back(points[i]);

This function assings points to clusters. cluster number is ruled by c variable. If input point doesn't fit to any cluster, function will return uninitialized variable (holding any possible value) which. then is used as vector index of nonexisting element - possible access violation error

You probably have to initialize c to zero in declaration

Tell us when you will be ready with errors described above and also show us a sample input file (one which causes errors, if all datasets cause errors, show us the smallest one)

0

精彩评论

暂无评论...
验证码 换一张
取 消