Thread: C# Array help

Results 1 to 4 of 4
  1. #1 C# Array help 
    Prometheus Owner/Developer
    Charlie''s Avatar
    Join Date
    Sep 2011
    Age
    25
    Posts
    576
    Thanks given
    35
    Thanks received
    27
    Rep Power
    10
    Below is my code in C# to get 10 int inputs and and store them in either evennum or oddnum array, then output the arrays, in my output it all works but it displays the empty values in EvenNum and OddNum as 0 which makes sense, but is there anyway to avoid it displaying them?
    Code:
    usingSystem;
    usingSystem.Linq;
    
    class Program {
    publicstaticvoid Main (string[] args) {
    int[] num = newint[10];
    int[] EvenNum = newint[10];
    int[] OddNum = newint[10];
    
    //Loop 10 times:
    for (int i=0; i < num.Length; ) {
    //Get numbers as input
        Console.WriteLine("Enter a number: ");
          num[i] = Convert.ToInt32(Console.ReadLine());
    if (num[i] % 2 == 0) {
    //If number is even, store in an array called EvenNum.
          EvenNum[i] = num[i];
          } else {
    //If number is odd, store in an array called OddNum.
          OddNum[i] = num[i];
          }
          i++;
    if(i==10) {
    //Display EvenNum array in ascending order
          Console.WriteLine("EvenNumbers in ascending order: ");
    //Find sum of EvenNum array and display sum.
          Console.WriteLine("Sum of EvenNumbers= " + EvenNum.Sum());
    foreach (int e in EvenNum) {
            Array.Sort(EvenNum);
      Console.WriteLine(e);
    }
    //Display OddNum array in descending order
          Console.WriteLine("OddNumbers in descending order: ");
    //Find sum of OddNum array and display sum.
          Console.WriteLine("Sum of OddNumbers= " + OddNum.Sum());
    foreach (int o in OddNum) {
            Array.Sort(OddNum);
            Array.Reverse(OddNum);
              Console.WriteLine(o);
        }
          }
        }
        }
    }
    
    
    Thanks to Format for answering and below is updated code for anyone interested. Also needed to move the Array.Sort's out of the foreach statements to avoid the obvious
    Code:
    
    usingSystem;
    usingSystem.Linq;
    
    class Program {
    publicstaticvoid Main (string[] args) {
    int[] num = newint[10];
    int[] EvenNum = newint[10];
    int[] OddNum = newint[10];
    
    //Loop 10 times:
    for (int i=0; i < num.Length; ) {
    //Get numbers as input
        Console.WriteLine("Enter a number: ");
          num[i] = Convert.ToInt32(Console.ReadLine());
    if (num[i] % 2 == 0) {
    //If number is even, store in an array called EvenNum.
          EvenNum[i] = num[i];
          } else {
    //If number is odd, store in an array called OddNum.
          OddNum[i] = num[i];
          }
          i++;
    if(i==10) {
    //Display EvenNum array in ascending order
            Array.Sort(EvenNum);
          Console.WriteLine("EvenNumbers in ascending order: ");
    //Find sum of EvenNum array and display sum.
          Console.WriteLine("Sum of EvenNumbers= " + EvenNum.Sum());
    foreach (int e in EvenNum) {
    if (e != 0) {
                Console.WriteLine(e);
            }
    }
    //Display OddNum array in descending order
              Array.Sort(OddNum);
              Array.Reverse(OddNum);
          Console.WriteLine("OddNumbers in descending order: ");
    //Find sum of OddNum array and display sum.
          Console.WriteLine("Sum of OddNumbers= " + OddNum.Sum());
    foreach (int o in OddNum) {
    if (o != 0) {
                Console.WriteLine(o);
            }
          }
          }
        }
        }
    }
    
    
    
    
    Last edited by Charlie'; 10-09-2021 at 09:48 AM.

    Reply With Quote  
     

  2. #2  
    L O S E Y O U R S E L F
    Format's Avatar
    Join Date
    Aug 2013
    Age
    22
    Posts
    2,204
    Thanks given
    1,112
    Thanks received
    1,173
    Discord
    View profile
    Rep Power
    5000
    Code:
    if (e != 0) {
     Console.WriteLine(e);
    }
    lol saem



    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Donator


    Join Date
    Feb 2014
    Age
    24
    Posts
    162
    Thanks given
    261
    Thanks received
    62
    Discord
    View profile
    Rep Power
    100
    Use LINQ

    var inputs = ..

    var evenInputs = inputs.Where(x => x % 2 == 0).ToArray();
    var oddInputs = inputs.Where(x => x % 2 != 0).ToArray();
    Last edited by Infernal cape; 10-27-2021 at 09:38 PM.
    Reply With Quote  
     

  5. #4  
    Programmer
    Elysian's Avatar
    Join Date
    Dec 2018
    Posts
    69
    Thanks given
    0
    Thanks received
    15
    Discord
    View profile
    Rep Power
    48
    If you don't want to use LINQ.
    Code:
    class Program
    {
        static int[] num = new int[10];
        static int[] EvenNum = new int[10];
        static int[] OddNum = new int[10];
    
        public static void Main(string[] args)
        {
            //Loop 10 times:
            for (int i = 0; i < num.Length;)
            {
                //Get numbers as input
                Console.Write("Enter a number: ");
                int newNum;
                while (!int.TryParse(Console.ReadLine(), out newNum))
                    Console.Write("That's not a valid input. Please try again: ");
    
                num[i] = newNum;
                if (num[i] % 2 == 0) //If number is even, store in an array called EvenNum.
                    EvenNum[i] = num[i];
                else //If number is odd, store in an array called OddNum.
                    OddNum[i] = num[i];
                i++;
    
                if (i == 10)
                {
                    
                    //Find sum of EvenNum array and display sum.
                    Console.WriteLine($"Sum of EvenNumbers= {Sum(EvenNum)}");
                    //Display EvenNum array in ascending order
                    Console.WriteLine("EvenNumbers in ascending order: ");
                    foreach (int e in SortLowAmount(EvenNum, false))
                        Console.WriteLine(e);
    
                    //Find sum of OddNum array and display sum.
                    Console.WriteLine($"Sum of OddNumbers= {OddNum.Sum()}");
                    //Display OddNum array in descending order
                    Console.WriteLine("OddNumbers in descending order: ");
                    foreach (int o in SortLowAmount(OddNum, true))
                        Console.WriteLine(o);
                }
            }
        }
    
        static int Sum(int[] values)
        {
            int sum = 0;
            for (int i = 0; i < values.Length; i++)
                sum += values[i];
            return sum;
        }
    
        static int[] SortLowAmount(int[] values, bool reverse)
        {
            for (int i = 1; i < values.Length; i++)
            {
                var temp = values[i];
                int j = i - 1;
                while (j >= 0 && values[j] > temp)
                {
                    values[j + 1] = values[j];
                    j--;
                }
                values[j + 1] = temp;
            }
    
            if (reverse)
                Reverse(values);
            
            return values;
        }
    
        static int[] Reverse(int[] arr)
        {
            for (int i = 0; i < arr.Length / 2; i++)
            {
                int tmp = arr[i];
                arr[i] = arr[arr.Length - i - 1];
                arr[arr.Length - i - 1] = tmp;
            }
    
            return arr;
        }
    }
    I'm using an insertion algorithm for the sort because that's what Array.Sort(); uses when the collection contains <= 16 elements.
    https://referencesource.microsoft.co...c8d19d6b629d8c
    https://source.dot.net/#System.Priva...d8c,references
    https://docs.microsoft.com/en-us/dot..._System_Array_
    Last edited by Elysian; 11-13-2021 at 06:05 PM. Reason: Adding references.
    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Array Help
    By TheRedArmy in forum Help
    Replies: 3
    Last Post: 04-26-2010, 03:01 AM
  2. Array help
    By teken2 in forum Help
    Replies: 3
    Last Post: 01-13-2010, 03:08 AM
  3. 2d array help;
    By Kastro in forum Help
    Replies: 3
    Last Post: 11-30-2009, 01:22 PM
  4. array help pl0x
    By Roger in forum Help
    Replies: 1
    Last Post: 06-19-2009, 09:43 PM
  5. [help] array help [rep++]
    By Robin Spud in forum Help
    Replies: 10
    Last Post: 06-07-2009, 08:47 AM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •