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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:18:13+00:00 2026-05-23T09:18:13+00:00

I have an 8 X 8 matrix. Now, The below coordinates are occupied {

  • 0

I have an 8 X 8 matrix. Now, The below coordinates are occupied

{ 6, 3 }, { 5, 5 }, { 3, 3 }.... What needs to be done is that, I need to build straight line

through these points and needs to count how many coordinates they have touched?

My program so far stands as

private static void GetCount(int[,] Positions)
{

    int rcount = 8;
    int firstRow = Positions[0, 0];            

    for (int i = 1; i < Positions.Length/2; i++)
    {              
       int currentRow = Positions[i, 0];
       if (currentRow != firstRow)
        {
          rcount += 8;
          firstRow = currentRow;
        }      
     }

     int cCount = 8;
     int firstCol = Positions[0, 1];

     for (int i = 1; i < Positions.Length / 2; i++)
     {


        int currentCol = Positions[i, 1];
        if (currentCol != firstCol)
        {
            cCount += 8;
            firstCol = currentCol;
         }

      }
     int totalCount = rcount - cCount;
     Console.WriteLine(totalCount);

}

And I am invoking it as

GetCount(new int[,] { { 6, 3 }, { 5, 5 }, { 3, 3 } });

The output will be 40 here. (count will be 24 for each 3 unique rows i.e. 6,5,3 and count will be 16 for 2 unique columns i.e. 3 and 5… So, the total count is 24+16 = 40)
But I am getting the output as 48.

Also is it possible to do the porgram using one single loop?

I am using C# 1.0

Edited

This does work

List<int> lstRows = new List<int>();
            List<int> lstCols = new List<int>();
            int count = 0;

            //Get the unique rows and columns
            for (int i = 0; i < marinePositions.Length / 2; i++)
            {
                if (!lstRows.Contains(marinePositions[i, 0])) lstRows.Add(Positions[i, 0]);
                if (!lstCols.Contains(marinePositions[i, 1])) lstCols.Add(Positions[i, 1]);
            }
            //get row count
            for (int i = 0; i < lstRows.Count; i++) count += 8;
            //get column count
            for (int i = 0; i < lstCols.Count; i++) count += 8;

            Console.WriteLine(count);

But need a much better one.. if possible using linq/lambda and no loop

Please help

  • 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-23T09:18:13+00:00Added an answer on May 23, 2026 at 9:18 am

    Here u go… but this in is LINQ and not C# 1.0 which is way too old.. not sure why are you using such old version of the language:

            private static void GetCount(int[,] Positions)
            {
                List<int> x = new List<int>();
                List<int> y = new List<int>();
                for (int i = 0; i < Positions.Length/2; i++)
                {
                    x.Add(Positions[i, 0]);
                    y.Add(Positions[i, 1]);
                }
                int result = (x.Distinct().Count() * 8) + (y.Distinct().Count() * 8);
                Console.WriteLine(result);
            }
    

    No loops magic:

        private static void GetCount(int[,] Positions)
        {
            var range = Enumerable.Range(0, Positions.Length / 2);
            var result = (range.Select(i => Positions[i, 0]).Distinct().Count() * 8) +
                         (range.Select(i => Positions[i, 1]).Distinct().Count() * 8);
            Console.WriteLine(result);
    
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a matrix report now that has Position, Hours and Wages for a location
Related to my other question , I have now modified the sparse matrix solver
I have a matrix in MATLAB and I need to find the 99% value
Suppose, in MATLAB, that I have a matrix, A, whose elements are either 0
I have a feature matrix implemented with Silverlight's Grid where users need to select
I have a sparse matrix that is not symmetric I.E. the sparsity is somewhat
I have a matrix that I am showing in html table. I have my
I have a list like df_all (see below). A = matrix( ceiling(10*runif(8)), nrow=4) colnames(A)
I have a large correlation matrix result in R - for now about 30
I have a big text file containing data that needs to be extracted and

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.