# Solved: C# Array Multiplying



## supergoten

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;


----------



## DYS

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?


----------



## supergoten

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.


----------



## DYS

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.


----------



## supergoten

I think it's close, but it still has an error.... you've been a big help though... does anyone else have any help?


----------



## MMJ

Exactly what items do you want to multiply together?


----------



## DYS

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.


----------



## MMJ

PHP:


for (int i = 0; i <= 9; i++)
{
	array3.Add(array1[i] * array2[i]);
}


----------



## DYS

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.


----------



## supergoten

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'


----------



## MMJ

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];
           }  

        }
    }
}


----------



## supergoten

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];
            }
        }
    }
}


----------



## MMJ

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.


----------



## supergoten

I copied that and it seems to be working! Thank you so much


----------



## MMJ

NP, I don't have VS so I couldn't give the the right answer on my first reply.


----------

