Thread: C# Array help

Results 1 to 2 of 2
  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,198
    Thanks given
    1,112
    Thanks received
    1,141
    Discord
    View profile
    Rep Power
    5000
    Code:
    if (e != 0) {
     Console.WriteLine(e);
    }
    lol saem



    Reply With Quote  
     

  3. Thankful user:



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
  •