Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7703709
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:31:14+00:00 2026-05-31T23:31:14+00:00

I am reading some coordinates from a file and trying to assign them cell

  • 0

I am reading some coordinates from a file and trying to assign them cell values (0-2) by diving them into a 3×3 grid. For some values the code is assigning negative cell values like for x=268 or x=269. Where am I going wrong?

I have file output.txt as follows with :

76 62
77 62
78 62
79 62
81 62
83 62
86 62
etc

The code to assign cells:

int x_points[99];
int y_points[99];
int i=0;
int x,y;
int max_x, max_y;
int min_x, min_y;

while(out.good())
{
    out>>x;
    out>>y;

        if(i==0)
{
  max_x=x;
  min_x=x;
  min_y=y;
  max_y=y;
 }
 else if (x>max_x)
 {
    max_x=x;
 }
 else
 if(x < min_x)
 {
   min_x=x;
 }
else if (y>max_y)
 {
    max_y=y;
 }
 else
 if(y < min_y)
 {
   min_y=y;
 }
x_points[i]=x;
y_points[i]=y;
i++;
}


for(i=0; i<99; i++)
cout<<x_points[i]<<","<<y_points[i]<<"\n";

int cells_x[99];
int cells_y[99];

float x_width;
float y_width;
int divide = 3;

//To find out the cells. Divide it by 20

x_width=(max_x-min_x)/divide;
y_width=(max_y-min_y)/divide;

cout<<"\nx_width:"<<x_width;
cout<<"y_width:"<<y_width<<"\n";

int x1;
int y1;
for(i=0; i<99; i++)
{
x1=x_points[i]-min_x;
y1=y_points[i]-min_y;

    for(int j=0; j<divide; j++)
    {
        if(j*x_width<=x1 && x1<((j+1)*x_width))
            cells_x[i]=j;

        if(j*y_width<=y1 && y1<((j+1)*y_width))
            cells_y[i]=j;
    }
    cout<<cells_x[i]<<" "<<i<<" "<<cells_y[i]<<"\n";

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:31:16+00:00Added an answer on May 31, 2026 at 11:31 pm

    The first problem I see is where you try to find the maximum and minimum values of x and y:

    if(i==0)
    {
        max_x=x;
        min_x=x;
        min_y=y;
        max_y=y;
    }
    else if (x>max_x)
    {
        max_x=x;
    }
    else if(x < min_x)
    {
        min_x=x;
    }
    else if (y>max_y) /* <-- This should not have an else! */
    {
        max_y=y;
    }
    else if(y < min_y)
    {
        min_y=y;
    }
    

    As written, it prevents the analysis of y values when an x is a new max or min. This will cause issues with the rest of your program, since max_y and min_y don’t actually contain the max and min.

    Next up is calculating the x_width and y_width:

    x_width=(max_x-min_x)/divide; // should be x_width=(max_x-min_x + 1)/divide;
    y_width=(max_y-min_y)/divide; //           y_width=(max_y-min_y + 1)/divide;
    

    If max_x = 8, and min_x = 0, your code would result in x_width = 8/3, or about 2.6666666666666666666666666666667. But 0-8 can be split up into 3 groups of 3. Adding 1 corrects this by calculating the number of points, not the length of the line.

    It also prevents you from having a width of 0, which would happen if max_x == min_x. A width of 0 causes trouble in the next section:

    for(int j=0; j<divide; j++)
    {
        /* an x_width of 0 will cause this to never evaluate to true */
        /* x1 cannot ever be >= to 0 AND < 0 */
        if(j*x_width<=x1 && x1<((j+1)*x_width))
            cells_x[i]=j;
    
        if(j*y_width<=y1 && y1<((j+1)*y_width))
            cells_y[i]=j;
    }
    

    if x_width is zero, then you will never assign a value to cells_x[i], meaning it will still be set to its initialization value, which could be a negative number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read some xyz coordinates from an input file. This is the
After some reading from this tutorial , I have the following partly working code:
I'm reading some numbers from a data source that represent x- and y-coordinates that
Reading some posts from Jimmy Boggard and wondering - how exactly is it possible
When reading some FreeBSD source code (See: radix.h lines 158-173), I found variable declarations
I was reading some 3rd party code and I found this: x.Flags = x.Flags
I'm reading some code in the Ogre3D implementation and I can't understand what a
I need some logic/programming help with reading more than one record from a text
Reading some piece of code and I keep seeing this : public override bool
Reading some Python (PyQt) code, I came across as follows. @pyqtSignature(QString) def on_findLineEdit_textEdited(self, text):

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.