0

I have been messing around with Lambda expressions in C#, teaching myself, and trying to test myself here. The problem with my code is not in evaluating the array for these set conditions, but rather I am having a difficult time capturing user input to build out the array. Have tried a few different methods, some mentioned here, but seem to have trouble adapting them to my code. Also any input on simplifying my expressions for checking if it has odd only or even only would be appreciated! I feel they are slightly bloated. Thanks!

using System;

namespace BuildingArrays
{
  class Program
  {
    static void Main(string[] args)
    {
     int[] numbers = {Int32.TryParse(string, Console.ReadLine())};

     bool hasOddOnly = Array.Exists(numbers, (int num) =>
      {
        bool hasEven = num % 2 == 0;
        return hasEven;
      });
      hasOddOnly = !hasOddOnly;

      bool hasEvenOnly = Array.Exists(numbers, (int num) =>
        {
          bool hasOdd = num % 2 != 0;
          return hasOdd;
        });
      hasEvenOnly = !hasEvenOnly;

     bool hasOddAnd4 = Array.Exists(numbers, (int num) =>
     {
       bool hasOdd = num % 2 != 0;
       bool is4 = num == 4;
       return is4 && hasOdd;
     });

     bool multipleOf4 = Array.Exists(numbers, (int num)=>
     {
       bool multiple = num % 4 == 0;
       bool multiple2 = num % 3 == 0;
       return multiple || multiple2;
     });

    bool multipleOf4and3 = Array.Exists(numbers, (int num)=>
    {
      bool multipleBoth = num % 4 == 0 && num % 3 == 0;
      return multipleBoth;
    });

     Console.WriteLine($"This array contains odd numbers and 4 is {hasOddAnd4}");
     Console.WriteLine($"This array contains ONLY odd numbers is {hasOddOnly}.");
     Console.WriteLine($"This array contains ONLY even numbers is {hasEvenOnly}.");
     Console.WriteLine($"This array contains either a multiple of 4, or a multiple of 3 is {multipleOf4}.");
     Console.WriteLine($"This array contains a number which is both a multiple of 4 and 3 is {multipleOf4and3}");
    }
  }  
}

4
  • A user cannot enter an array via the console. A user can only enter a string. I'm guessing you either need to prompt the user multiple times (so they can enter numbers one at a time) or you expect the user to enter a delimited string of some kind (so they can enter them all at once). You will need a method to convert that/those string/s into an array. Where is it? Commented Feb 10, 2020 at 22:30
  • Asking them multiple times would probably fit the problem. I want them to be able to essentially enter a string like "1, 4, 9, 15, 12", have it convert that string into integers and convert that list of integers to the array. I have not tried to create a method to convert the strings yet. Very new (1 week in) to C#! thanks for the response! Commented Feb 10, 2020 at 22:34
  • @GeorgeWaterhouse Parsing such a string is going to be way more hassle then it will be worth. do...while() loop. Int.TryParse(). You can recycley your current approach of asking for a Array size, the repeating until it is filled. Commented Feb 10, 2020 at 23:08
  • 1. create an empty List<int> or List<string> or of the appropriate type, 2. in a loop, use Console.Readline to get the next entry, 3. add it to the list, 4. use Readline again, to ask if the user is done, 5. when user is done, call myList.ToArray() to create your array. Commented Feb 11, 2020 at 0:29

1 Answer 1

1

Array.Exists takes a Predicate<T> as the second parameter. That should look like this:

bool hasEven = Array.Exists(numbers, x => x % 2 == 0);

To fill your array, you're going to need something like this:

for (int i=0; i< numbers.Length; i++)
    numbers[i] = Int32.Parse(Console.ReadLine());

Further Reading
Array.Exists on MSDN
How to fill an array from user input in C#

Sign up to request clarification or add additional context in comments.

4 Comments

"should" is a bit strong... There is absolutely nothing wrong with definition of predicates in original post...
I stand by my assertion. The form the OP used is very unlikely to be seen in the wild for simple predicates.
Sure... Note that you get array reading code not exactly correct which is what OP seem to have main problem with...
Yeah, it just illustrates the main problem. Don't operate heavy machinery with this code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.