1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: C# Array Multiplying

Discussion in 'Software Development' started by supergoten, Apr 2, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    This is probably a really simple problem.

    I have three arrays here. The third array needs to be the product of the first two... how do I multiply the arrays for each value? I know there has to be a for loop but i'm not sure how it should be written.

    Code:
       double[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                double[] array2 = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
                double[] array3;
     
  2. DYS

    DYS

    Joined:
    Dec 28, 2008
    Messages:
    462
    So do you want to multiple, for example, element 1 in array 1, with element 1 in array 2, and so on?

    I'm not a genius like others here are, but if you're doing straight multiplication, why do the arrays need to be doubles? They can be integers, can't they?
     
  3. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    Yes, that's the way it could be done.
    And yeah they could be done as integers i suppose.

    Thanks for any help you can give.
     
  4. DYS

    DYS

    Joined:
    Dec 28, 2008
    Messages:
    462
    Code:
    for (int i = 0; i<=9; i++)
    {
    array1[i] * array2[i];
    }
    
    I think the code should be something like that, although I'm working from Java, rather than C#, as I've only used C# to do XNA, and I can't really recall having to write a loop to multiply stuff.

    Anyway, I'm not too sure how to add each answer to each element in the third array. I think someone who knows more about this will have to answer that for you. But I hope (for myself as well) that I have been some help.
     
  5. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    I think it's close, but it still has an error.... you've been a big help though... does anyone else have any help?
     
  6. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Exactly what items do you want to multiply together?
     
  7. DYS

    DYS

    Joined:
    Dec 28, 2008
    Messages:
    462
    Okay mate. Glad I could be of some help at least. Like I said, I had Java in mind, as I have more experience of that.

    MMJ, supergoten wants to multiply each element against the corresponding position in the respective arrays.

    So basically:
    1 * 2
    2 * 4
    3 * 6

    and so on... Or at least that's how I've understood it.
     
  8. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    PHP:
    for (int i 0<= 9i++)
    {
        
    array3.Add(array1[i] * array2[i]);
    }
     
  9. DYS

    DYS

    Joined:
    Dec 28, 2008
    Messages:
    462
    Would he need to tell the third array to increase the element position each time? Like:
    Code:
    for (int i = 0; i <= 9; i++)
    {
         array3[i].Add(array1[i] * array2[i]);
    }
    
    Kinda learning myself here too I guess ha ha.
     
  10. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    Ok, this is what i have right now.

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ArrayProgram
    {
        class Program
        {
            static void Main(string[] args)
            {
                double[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                double[] array2 = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
               double[] array3;
    
               for (int i = 0; i <= 9; i++)
               {
                   array3.Add(array1[i] * array2[i]);
               }  
    
            }
        }
    }
    
    There's still an error though, 'System.Array' does not contain a definition for 'Add' :confused:
     
  11. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Sorry, Add is a function for lists, which have more use in C# then arrays

    Try this:

    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ArrayProgram
    {
        class Program
        {
            static void Main(string[] args)
            {
                double[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                double[] array2 = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
               double[] array3;
    
               for (int i = 0; i <= 9; i++)
               {
                   array3[i] = array1[i] * array2[i];
               }  
    
            }
        }
    }
     
  12. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    It still isn't working :(

    I'm not sure if this code is any better, it still isn't right though.

    Code:
       double[][] anArray = new double[2][];
                anArray [0] = new double [] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                anArray [1] = new double [] { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
                double[] array3 = new double[10];
    
                for (int i = 0; i <= 9; i++)
                {
                    array3[i] = anArray[0] * anArray[1];
                }
            }
        }
    }
     
  13. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ArrayProgram
    {
        class Program
        {
            static void Main(string[] args)
            {
                double[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                double[] array2 = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
                double[] array3 = new double[10];
    
    
               for (int i = 0; i <= 9; i++)
               {
                   array3[i] = array1[i] * array2[i];
               }
    
            }
        }
    }
    Post any error code.
     
  14. supergoten

    supergoten Thread Starter

    Joined:
    Apr 14, 2009
    Messages:
    28
    I copied that and it seems to be working! Thank you so much :)
     
  15. MMJ

    MMJ Guest

    Joined:
    Oct 15, 2006
    Messages:
    3,625
    NP, I don't have VS so I couldn't give the the right answer on my first reply.
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/914256

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice