Solved: C# Array Multiplying

Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

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;
 

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?
 

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.
 

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.
 

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?
 

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.
 

MMJ

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

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.
 

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:
 

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

        }
    }
}
 

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

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.
 

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.
 
Status
This thread has been Locked and is not open to further replies. The original thread starter may use the Report button to request it be reopened but anyone else with a similar issue should start a New Thread. Watch our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top