Curtis
Curtis

Reputation: 827

Foreach to calculate multiple arrays

I'm stuck on a problem I'm trying to sort out with calculating multiple arrays.

Here's the scenario...

A user will input their card number i.e. 1234, and then I need it multiple each number stored in another array in a sequence of 1,2,1,2.

So it would calculate in according 1 * 1, 2 * 2, 3 * 1, 4 * 2, etc.

I've tried a few different to calculate both inside a single foreach loop, however I'm having no such luck as I'm returning duplicates, so I'm seeing if there's even a way I can combine both arrays into a single foreach loop?

I'm able to print the data, so now I'm just seeing how I can multiple them together. Here's what I have so far...

class Program
{
    static void Main(string[] args)
    {
       Console.WriteLine("Enter your Card Number");
       char[] card = Console.ReadLine().ToCharArray();
       int[] card_m = { 1,2,1,2 };

       foreach (char c in card)
       {
           int number = (int)char.GetNumericValue(c);
           Console.WriteLine("Converted Number: {0}", number);
       }

       foreach (int m in card_m)
           Console.WriteLine("Card Number Multiplier: {0}", m);

       Console.Read();
    }
}

Upvotes: 1

Views: 1411

Answers (8)

Joachim Isaksson
Joachim Isaksson

Reputation: 180917

For any length card number, you can just use Aggregate() to calculate the value for each digit, and sum them up.

int coefficient = 2;
chksum =  input.Aggregate(0, (sum, ch) => sum + (ch - '0') * (coefficient ^= 3));

Upvotes: 2

Austin Salonen
Austin Salonen

Reputation: 50225

For your example,

var multiplied = card.Select(char.GetNumericValue).Zip(card_m, (a,b) => a*b);

This won't be valid if card_m.Length != card.Length because it'll only "zip" until the end of the shortest list.

MSDN Zip -- Merges two sequences by using the specified predicate function.

You can add the extension method below and run this code to repeat card_m:

var multiplied = card.Select(char.GetNumericValue)
                     .Zip(card_m.Indefinite(), (a,b) => a*b);

public static class Extensions
{
    public static IEnumerable<int> Indefinite(this int[] source)
    {
        int i = 0;

        while (true)
            yield return source[i++ % source.Length];
    }
}

Upvotes: 1

Bhavin
Bhavin

Reputation: 427

Not sure what you are doing do your card and card_m always are of same length?

if so you could do something like ( you have to make sure first)

for(int i =0;i<card.Length;i++)
{
   Console.WriteLine("Result: {0}", ((int)char.GetNumericValue(card[i]))*card_m[i]);
} 

Upvotes: 1

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

Maybe you can try with this assuming you might have more than 4 digits in your card number

            Console.WriteLine("Enter your Card Number");
            char[] card = Console.ReadLine().ToCharArray();
            int[] card_m = { 1, 2, 1, 2 };
            int counter = 0;
            foreach (char c in card)
            {
                int value = (int)char.GetNumericValue(c) * card_m[counter++%card_m.Length];
                Console.WriteLine("Converted Number: {0}", value);
            }

Upvotes: 0

jheddings
jheddings

Reputation: 27563

Assuming the arrays may be different lengths, use the modulus operator to loop on your multiplier array:

    static void Main(string[] args) {
        Console.WriteLine("Enter your Card Number");

        char[] card = Console.ReadLine().ToCharArray();

        int[] card_m = { 1, 2 };

        for (int idx = 0; idx < card.Length; idx++) {
            int number = (int) char.GetNumericValue(card[idx]);
            Console.WriteLine("Converted Number: {0}", number);

            // pull the multiplier from the card_m array
            int m = card_m[idx % card_m.Length];

            Console.WriteLine("Card Number Multiplier: {0}", m);
        }

        Console.Read();
    }

Upvotes: 0

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28970

You can use

for (int i = 0; i < card.Length; i++)
{
  var result = (int)char.GetNumericValue( card[i]) * (int)char.GetNumericValue(m_card[i]);
}

Upvotes: 0

Thom Smith
Thom Smith

Reputation: 14086

card.Select(char.GetNumericValue).Zip(card_m, (c, n) => c*n);

Upvotes: 0

Joey
Joey

Reputation: 354506

You cannot iterate two arrays in a single foreach loop. You have to use a plain old for loop in that case. Rosetta Code has an example.

Upvotes: 0

Related Questions