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 820871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T02:32:52+00:00 2026-05-15T02:32:52+00:00

I have a class like this that im using as the point in the

  • 0

I have a class like this that im using as the point in the array:

class Point
    {
        public int x;
        public int y;

        public Point(int x, int y)
        {
            this.x = x;
            this.y = y;
        }
    }

and then im trying to get the value that is closest to the end with this:

public void Position()
        {
            for (int x = 0; x < arr.Length; x++)
            {
                for (int y = 0; y < arr[x].Length; y++)
                {
                    if (arr[x][y] == 3)
                        Pos.x = x;
                        Pos.y = y;
                    if (arr[x][y] == 1)
                        Pos.x = x;
                        Pos.y = y;
                    if (arr[x][y] == 2)
                        Pos.x = x;
                        Pos.y = y;
                }
            }
            Console.Write("Last Position: {0}, {1}",LastPos.x, LastPos.y);
        }

and i have this for the point:

public Point Pos = new Point(0, 0);

Its a jagged array filled with all zeros except for a few points. Looks like this:

0 0 0 3 0 0 0 0

0 0 1 0 0 0 0

0 0 0 2 0 0 0 0

0 0 0 0 0

0 0 0 0 0 0 0

Where 2 is the closest to the end in this example. with a position of 2,3 which i want Pos.x and Pos.y to be. It keeps giving me 0,30.

  • 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-15T02:32:52+00:00Added an answer on May 15, 2026 at 2:32 am

    It looks like you are missing some curly braces.

    if (arr[x][y] == 3)
    {
        Pos.x = x;
        Pos.y = y;
    }
    if (arr[x][y] == 1)
    {
        Pos.x = x;
        Pos.y = y;
    }
    if (arr[x][y] == 2)
    {
        Pos.x = x;
        Pos.y = y;
    }
    

    In C# the indentation is not significant so your code is being interpreted as:

    if (arr[x][y] == 3)
    {
        Pos.x = x;
    }
    Pos.y = y;
    if (arr[x][y] == 1)
    {
        Pos.x = x;
    }
    Pos.y = y;
    if (arr[x][y] == 2)
    {
        Pos.x = x;
    }
    Pos.y = y;
    

    You could also simplify your code considerably:

    if (arr[x][y] != 0)
    {
        Pos.x = x;
        Pos.y = y;
    }
    

    Another problem is that you are setting Pos but printing the value of LastPos.

    If you need better performance, note that it would be faster to start searching backwards from the end and break when you hit the first non-zero element instead of searching from the start and remembering the last non-zero element you’ve seen. This could terminate after checking just a few elements in many cases – in the best case only one element needs to be checked. So depending on the size of your data it could be hundreds of times faster to do it this way.

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

Sidebar

Related Questions

I have class that looks like this: class A { public: class variables_map vm
I have a class that looks like this: public class BasePadWI { public WorkItem
Say I have a class that looks like this (get/set omited): class InfoClass{ String
Currently I have a class that looks like this: public class MyClass : IMyClass
Assume I have a class that looks like this: class Sample { public string
I have a class that defines its own enum like this: public class Test
I have a class that roughly looks like this: public class ViewModel { public
I have a class that contains a boolean field like this one: public class
I have a class A that looks like this: class A { // A's
I have a class User that looks like this: class User { private: char*

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.