Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply all values in an array?

I have an assignment where I need to find the product of all of the numbers in an array, I'm not sure how to do this.

    int[] numbers = new int[SIZE];

    Console.WriteLine("Type in 10 numbers");
    Console.WriteLine("To stop, type in 0");
    for (int input = 0; input < SIZE; input++)
    {
        userInput = Console.ReadLine();
        numberInputed = int.Parse(userInput);

        if (numberInputed == ZERO)
        {
            numberInputed = ONE;
            break;
        }
        else
        {
            numbers[input] = numberInputed;
        }

    }

This is where I'm trying to find the product of all of the numbers in the array.

    foreach (int value in numbers)
    {
        prod *= value;
    }

    Console.WriteLine("The product of the values you entered is {0}", prod);

What am I doing wrong in the foreach statement? Thanks in advance

Edit, left out my declared values

    const int SIZE = 10;
    const int ZERO = 0;
    string userInput;
    int numberInputed;
    int prod = 1;

It now works when I type in all ten values but if I put a 0 in order to break the loop then everything equals 0. How do I prevent a 0 from being entered into the array?

like image 264
user2781666 Avatar asked Nov 21 '13 22:11

user2781666


People also ask

How multiply all values of an array Javascript?

function multiply (array) { var sum=1; for (var i=0; i<array. length; i++) { sum = sum * array[i]; } return sum; } console. log(multiply[1,2,3]);

How do you multiply an array by itself?

You don't need to make a copy of the array if you just want to square it, you can just multiply each entry with itself and store it where you read it. Additionally, you use index < 10 as a condition, but it's better to have it dynamic by reading the length (the number of elements) of the input array arr .


2 Answers

It's possible you initialize prod to 0, which means no matter what numbers are in your array, prod will remain 0. Make sure you initialize it to 1 to get the correct result:

int prod = 1;
foreach (int value in numbers)
{
    prod *= value;
}

You could also use Linq's Aggregate extension method to do the same thing:

using System.Linq; // put with other using directives

int prod = numbers.Aggregate(1, (a, b) => a * b);

Update

The real problem (which I failed to notice before) is that your array isn't being fully populated if you break out of your loop early. So any array entries you didn't set are still initialized to 0. To fix this, use a List<int> instead of an int[]:

using System.Collections.Generic; // put with other using directives

List<int> numbers = new List<int>(SIZE); // Capacity == SIZE

...

for (int input = 0; input < SIZE; input++)
{
    ...
    if (numberInputed == ZERO)
    {
        break;
    }
    else
    {
        numbers.Add(numberInputed);
    }
}
like image 184
p.s.w.g Avatar answered Oct 30 '22 06:10

p.s.w.g


The problem is that you don't keep track of how many items there are in the array that actually are assigned a value. If you exit from the loop using a zero input, then the rest of the items are unchanged. As they are zero by default, you will be using those zeroes in your second loop, and when you have a zero somewhere in the array, the total product becomes zero.

Keep track of how many items there are by keeping the loop variable outside the loop:

int input = 0;
while (input < SIZE)
{
    userInput = Console.ReadLine();
    numberInputed = int.Parse(userInput);
    if (numberInputed == ZERO) {
      break;
    }
    numbers[input] = numberInputed;
    input++;
}

Now you can use only the items that are actually assigned:

for (int i = 0; i < input; i++) {
    prod *= numbers[i];
}
like image 1
Guffa Avatar answered Oct 30 '22 06:10

Guffa