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

  • Home
  • SEARCH
  • 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 3239774
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:00:33+00:00 2026-05-17T18:00:33+00:00

First off, I know my title can be formulated better, but my math classes

  • 0

First off, I know my title can be formulated better, but my math classes are so far gone I can’t remember the correct words anymore..

I need to do something like this (pseudo c#)

int[] digits1 = new int[10]{0,1,2,3,4,5,6,7,8,9};
int[] digits2 = new int[10]{0,1,2,3,4,5,6,7,8,9};
int result = digits1*digits2

This would be the sum of the product of element[i] of each array.

This obviously doesn’t work.
Any suggestions towards either a better title or the solution?

EDIT
clarification: I know I could loop them both and do the math.
Basically I would think there is a better way to do this and I’m looking for it purely out of personal curiousity.

  • 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-17T18:00:34+00:00Added an answer on May 17, 2026 at 6:00 pm

    With LINQ:

    int dotProduct = digits1.Zip(digits2, (d1, d2) => d1 * d2)
                            .Sum();
    

    Zipwill produce a streaming sequence containing the products of corresponding elements from both arrays, which is then summed into an integer with Sum.

    Note that this will not fail like it should when the arrays of unequal length, so you probably need to validate the input:

    //null checks here
    
    if(digits1.Length != digits2.Length)
       throw new ArgumentException("...");
    

    EDIT:
    As Jeff M points out,Enumerable.Zipwas only added to the framework in .NET 4.0. In .NET 3.5, you can do this (the idea is only efficient for collections that expose fast indexers):

    int dotProduct = Enumerable.Range(0, digits1.Length)
                               .Sum(i => digits1[i] * digits2[i]);
    
    //from Jeff M's comment:
    int dotProduct = digits1.Select((n, i) => n * digits2[i])
                            .Sum();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know I can read off /proc/[id]/maps but I'm not sure if the first
First off, I know the title is generic and not fitting. I just couldn't
Let me first start off, sorry for the confusing title. I didn't know how
First off: I know that this isn't reliable for actually checking if I can
First off, I know there are ways to make it so that text can
I know first off that this is probably the worst looking regex ever but
First off I'm not sure that's the proper title for this question but hopefully
First off, I think I know what's going on, but I thought I'd bring
First off all I know: Premature optimization is the root of all evil But
First off, I'd like to apologize for the horrid title there, but it was

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.