-2

I have been working on on this problem for awhile I have two arrays in parallel that are the same length. I have nested foreach loops to compare the arrays so I can tell what number is greater. One number is randomly generated and the other is gathered from user input. The problem I'm having is the number that is randomly generated is being compared to each user input before moving to the next randomly generated number. This is my code:

Random r = new Random();
int length = 10;

int[] ranNum = new int[length];
int[] userNum = new int[length];

for (int i = 0; i < length; i++) 
{
    ranNum[i] = r.Next(1, 100);
}

for (int i = 0; i < length; i++) 
{
    Console.WriteLine("Enter a number between 1 and 100");
    userNum[i] = Convert.ToInt32(Console.ReadLine());
}

foreach(int rn in ranNum) 
{
    foreach(int ui in userNum) 
    {
        if (rn < ui) 
        {
            Console.WriteLine(rn + " is less than " + ui);
        } 
        else if (rn > ui) 
        {
            Console.WriteLine(rn + " is greater than " + ui);
        } 
        else 
        {
            Console.WriteLine(rn + " is equal to " + ui);
        }
    }
}

I'm sure I'm missing something obvious any help would be appreciated Thank you in advance

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
  • 1
    Why were the curly braces edited to match Java/JavaScript styling? Curly braces on newlines is standard in C#. – Bradford Dillon Feb 05 '21 at 21:25
  • How do you want it to work. Let's say you have two arrays: `[1, 5, 200]` and `[2, 4, 100]`. What would you want to do with the arrays and their elements? – Flydog57 Feb 05 '21 at 21:26
  • @BradfordDillon: _"Curly braces on newlines is standard in C#."_ Watch out, brace-styling in C family languages is a religious issue. Serious spats (often escalating to flame-wars or lunch-room ostracization) have been fought over this issue in the past several decades. There's even a Wiki page devoted to it: https://en.wikipedia.org/wiki/Indentation_style. Note that there is no C# heading. – Flydog57 Feb 05 '21 at 21:29
  • Just use a single indexed for loop and access the values using the same index. `foreach` is not meant for this kind of logic. – Heretic Monkey Feb 05 '21 at 21:32
  • see [Iterate two Lists or Arrays with one ForEach statement in C#](https://stackoverflow.com/q/1955766) – greenjaed Feb 05 '21 at 21:39

2 Answers2

2

Nested foreach loops are exactly like nested for loops in that they loop through each of the outer values with all of the inner values. If you're just trying to compare the values one-to-one, you'll need to iterate through both arrays at the same time. You can do this by grabbing their iterators and using a while loop; You can use LINQ and Zip both arrays; Or, you can use another for loop, like you used to generate your arrays, and use a common index to iterate through both loops:

for (int i = 0; i < length; i++) {
    var rn = ranNum[i];
    var ui = userNum[i];
    if (rn < ui) {
      Console.WriteLine(rn + " is less than " + ui);
    } else if (rn > ui) {
      Console.WriteLine(rn + " is greater than " + ui);
    } else {
      Console.WriteLine(rn + " is equal to " + ui);
    }    
}

Depending on what you're doing with these values, you might consider consolidating these loops, but this is how you would iterate through both at the same time.

greenjaed
  • 589
  • 8
  • 20
0

There are TWO nested foreach loops here, which will create every possible combination of values. If you want to just match by index you can use a single for loop, or you can use the IEnumerable.Zip() function:

var results = ranNum.Zip(userNum, (rn, ui) => {
    if (rn < ui) return $"{rn} is less than {ui}";
    if (rn > ui) return $"{rn} is greater than {ui}";
    return $"{rn} is equal to {ui}";
});
foreach (var result in results)
{
    Console.WriteLine(result);
}
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794