none
even and odd numbers in Array of 50

    Question

  • Hi

    I wanted to know how do i print the output in an array of 50

    int

    1: the array contaning the even numbers: printing the even numbers and also printing its length as an array

    2: the array contaning the odd numbers: printing the odd numbers and also printing its length as an array

     

    thanks for your help

     

    [] arr = new int[50];
    • Edited by Hawk337 Thursday, September 23, 2010 7:40 PM
    Thursday, September 23, 2010 12:22 PM

Answers

  • Something like this will work...

    int[] array = new int[] { 1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12 };
    
    int[] even = (from y in array where ((y % 2) == 0) select y).ToArray();
    
    int[] odd = (from y in array where ((y % 2) != 0) select y).ToArray();
    

    And to display the number of elements in each...

    Console.WriteLine("Even numbers: {0}", even.Length);
    Console.WriteLine("Odd numbers: {0}", odd.Length);

     

    K

     

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:02 PM
    Thursday, September 23, 2010 12:35 PM
  • I suppose you mean the elements inside the array that are even or the the one that are odd

    A loop would check each element

    the condition for parity or not is divisibility by 2 so the condition is number%2==0(equal) or number%2!=0(different)

    The ammount can be computed by a counter.

    Ex:

    int amount=0;

    for(int c=0;c<50;c++)

    if(arr[c]%2==0)amount++;

    For even it will be the same but instead of == it will be !=

    Regards

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:02 PM
    • Edited by Serguey123 Friday, September 24, 2010 12:01 PM typho
    Thursday, September 23, 2010 12:37 PM
  • int[] arr = new int[50];
    
    // Array and counter for the odd array.
    int[] odd = null;
    int[] _odd = new int[arr.Length];
    int _oddLen = 0;
    
    // Array and counter for the even array.
    int[] even = null;
    int[] _even = new int[arr.Length];
    int _evenLen = 0;
    
    for (int i = 0; i < arr.Length; i++)
    {
    	byte firstByte = BitConverter.GetBytes(arr[i])[0];
    
    	if ((firstByte & 1) == 1) // First bit is high (odd number).
    	{
    		_odd[_oddLen] = arr[i];
    		_oddLen++;
    	}
    	else // First bit is low (even number).
    	{
    		_even[_evenLen] = arr[i];
    		_evenLen++;
    	}
    }
    
    // Put the _odd array into the odd array, if there is any odd number to add.
    if (_oddLen != 0)
    {
    	odd = new int[_oddLen];
    
    	for (int i = 0; i < _oddLen; i++)
    	{
    		odd[i] = _odd[i];
    	}
    }
    
    // Put the _even array into the even array, if there is any even number to add.
    if (_evenLen != 0)
    {
    	even = new int[_evenLen];
    
    	for (int i = 0; i < _evenLen; i++)
    	{
    		even[i] = _even[i];
    	}
    }
    
    // If there is no values inside the odd or even array, they are set to null.
    // To check if there is nothing inside the array without getting runtime errors:
    //
    // if(odd != null)
    // {
    //   DoWork();
    // }
    //
    // or
    //
    // if(even != null)
    // {
    //   DoWork();
    // }


    To get the values you could use a for loop:

     

    for (int i = 0; i < odd.Length; i++)
    {
    	int value = odd[i];
    
    	// Now you can do whatever you want to do with 'value', like print it.
    }

    Hope this helps ^^

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:04 PM
    Thursday, September 23, 2010 8:09 PM
  • Hi, print the elements of the arrays to a PrintPreviewControl ... I really dont know if "print" means that

    Form1.cs

      public partial class Form1 : Form
      {
        private int[] f = new int[25];
        private int[] z = new int[25];
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          for (int i = 0; i < 50; i++ )
          {
            if ((i & 0x01) == 1)
              z[i / 2] = i;
            else
              f[i / 2] = i;
          }
    
          this.printPreviewControl1.Document = this.printDocument1;
        }
    
        //simple output with GraphicsUnit.Pixel, for realworld scenarios see MSDN-Examples for printing documents
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
          Font font = new Font("Arial", 12);
    
          //Height of printed Number
          float h = e.Graphics.MeasureString("25", font).Height;
    
          //pseudo margins
          float lefttop = 100;
          float initialHeight = h * 2;
    
          //Headline 1
          e.Graphics.DrawString("Array f, Length: " + f.Length.ToString(), font, Brushes.Black, new PointF(lefttop, lefttop));
    
          //its width
          float w = e.Graphics.MeasureString("Array f, Length: " + f.Length.ToString(), font).Width + 100;
    
          //Headline 2
          e.Graphics.DrawString("Array z, Length: " + f.Length.ToString(), font, Brushes.Black, new PointF(lefttop + w, lefttop));
    
          for (int i = 0; i < f.Length; i++)
            e.Graphics.DrawString(f[i].ToString(), font, Brushes.Black, new PointF(lefttop, initialHeight + lefttop + h * i));
    
          for (int i = 0; i < z.Length; i++)
            e.Graphics.DrawString(z[i].ToString(), font, Brushes.Black, new PointF(lefttop + w, initialHeight + lefttop + h * i));
    
          font.Dispose();
    
          e.HasMorePages = false;
        }
      }
    

    Form1.Designer.cs:

      partial class Form1
      {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
          if (disposing && (components != null))
          {
            components.Dispose();
          }
          base.Dispose(disposing);
        }
    
        #region Vom Windows Form-Designer generierter Code
    
        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
          this.printPreviewControl1 = new System.Windows.Forms.PrintPreviewControl();
          this.button1 = new System.Windows.Forms.Button();
          this.printDocument1 = new System.Drawing.Printing.PrintDocument();
          this.SuspendLayout();
          // 
          // printPreviewControl1
          // 
          this.printPreviewControl1.Location = new System.Drawing.Point(22, 34);
          this.printPreviewControl1.Name = "printPreviewControl1";
          this.printPreviewControl1.Size = new System.Drawing.Size(701, 572);
          this.printPreviewControl1.TabIndex = 0;
          // 
          // button1
          // 
          this.button1.Location = new System.Drawing.Point(729, 25);
          this.button1.Name = "button1";
          this.button1.Size = new System.Drawing.Size(75, 23);
          this.button1.TabIndex = 1;
          this.button1.Text = "button1";
          this.button1.UseVisualStyleBackColor = true;
          this.button1.Click += new System.EventHandler(this.button1_Click);
          // 
          // printDocument1
          // 
          this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
          // 
          // Form1
          // 
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(842, 636);
          this.Controls.Add(this.button1);
          this.Controls.Add(this.printPreviewControl1);
          this.Name = "Form1";
          this.Text = "Form1";
          this.ResumeLayout(false);
    
        }
    
        #endregion
    
        private System.Windows.Forms.PrintPreviewControl printPreviewControl1;
        private System.Windows.Forms.Button button1;
        private System.Drawing.Printing.PrintDocument printDocument1;
      }
    

    regards,

      Thorsten

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:04 PM
    Thursday, September 23, 2010 8:26 PM

All replies

  • Something like this will work...

    int[] array = new int[] { 1, 3, 5, 7, 9, 11, 2, 4, 6, 8, 10, 12 };
    
    int[] even = (from y in array where ((y % 2) == 0) select y).ToArray();
    
    int[] odd = (from y in array where ((y % 2) != 0) select y).ToArray();
    

    And to display the number of elements in each...

    Console.WriteLine("Even numbers: {0}", even.Length);
    Console.WriteLine("Odd numbers: {0}", odd.Length);

     

    K

     

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:02 PM
    Thursday, September 23, 2010 12:35 PM
  • I suppose you mean the elements inside the array that are even or the the one that are odd

    A loop would check each element

    the condition for parity or not is divisibility by 2 so the condition is number%2==0(equal) or number%2!=0(different)

    The ammount can be computed by a counter.

    Ex:

    int amount=0;

    for(int c=0;c<50;c++)

    if(arr[c]%2==0)amount++;

    For even it will be the same but instead of == it will be !=

    Regards

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:02 PM
    • Edited by Serguey123 Friday, September 24, 2010 12:01 PM typho
    Thursday, September 23, 2010 12:37 PM
  • Hi

    I wanted to know how do i print the output in an array of 50

    int

    1: the array contaning the even numbers: printing the even numbers and also printing its length as an array

    2: the array contaning the odd numbers: printing the odd numbers and also printing its length as an array

     

    thanks for your help

     

    [] arr = new int[50];
    Thursday, September 23, 2010 7:41 PM
  • Hi,

    what exactly does "print" mean? The output on the console? Or do you want to print a document containing the output?

    You can get the arrays in many different ways. For fw2.0 compatiblility [here a winforms example]:

        private int[] f = new int[25];
        private int[] z = new int[25];
    
        private void button1_Click(object sender, EventArgs e)
        {
          for (int i = 0; i < 50; i++ )
          {
            if ((i & 0x01) == 1)
              z[i / 2] = i;
            else
              f[i / 2] = i;
          }
    
          //do something with the arrays;
        }
    

    regards,

      Thorsten

    Thursday, September 23, 2010 8:05 PM
  • int[] arr = new int[50];
    
    // Array and counter for the odd array.
    int[] odd = null;
    int[] _odd = new int[arr.Length];
    int _oddLen = 0;
    
    // Array and counter for the even array.
    int[] even = null;
    int[] _even = new int[arr.Length];
    int _evenLen = 0;
    
    for (int i = 0; i < arr.Length; i++)
    {
    	byte firstByte = BitConverter.GetBytes(arr[i])[0];
    
    	if ((firstByte & 1) == 1) // First bit is high (odd number).
    	{
    		_odd[_oddLen] = arr[i];
    		_oddLen++;
    	}
    	else // First bit is low (even number).
    	{
    		_even[_evenLen] = arr[i];
    		_evenLen++;
    	}
    }
    
    // Put the _odd array into the odd array, if there is any odd number to add.
    if (_oddLen != 0)
    {
    	odd = new int[_oddLen];
    
    	for (int i = 0; i < _oddLen; i++)
    	{
    		odd[i] = _odd[i];
    	}
    }
    
    // Put the _even array into the even array, if there is any even number to add.
    if (_evenLen != 0)
    {
    	even = new int[_evenLen];
    
    	for (int i = 0; i < _evenLen; i++)
    	{
    		even[i] = _even[i];
    	}
    }
    
    // If there is no values inside the odd or even array, they are set to null.
    // To check if there is nothing inside the array without getting runtime errors:
    //
    // if(odd != null)
    // {
    //   DoWork();
    // }
    //
    // or
    //
    // if(even != null)
    // {
    //   DoWork();
    // }


    To get the values you could use a for loop:

     

    for (int i = 0; i < odd.Length; i++)
    {
    	int value = odd[i];
    
    	// Now you can do whatever you want to do with 'value', like print it.
    }

    Hope this helps ^^

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:04 PM
    Thursday, September 23, 2010 8:09 PM
  • Hi, print the elements of the arrays to a PrintPreviewControl ... I really dont know if "print" means that

    Form1.cs

      public partial class Form1 : Form
      {
        private int[] f = new int[25];
        private int[] z = new int[25];
    
        public Form1()
        {
          InitializeComponent();
        }
    
        private void button1_Click(object sender, EventArgs e)
        {
          for (int i = 0; i < 50; i++ )
          {
            if ((i & 0x01) == 1)
              z[i / 2] = i;
            else
              f[i / 2] = i;
          }
    
          this.printPreviewControl1.Document = this.printDocument1;
        }
    
        //simple output with GraphicsUnit.Pixel, for realworld scenarios see MSDN-Examples for printing documents
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
          Font font = new Font("Arial", 12);
    
          //Height of printed Number
          float h = e.Graphics.MeasureString("25", font).Height;
    
          //pseudo margins
          float lefttop = 100;
          float initialHeight = h * 2;
    
          //Headline 1
          e.Graphics.DrawString("Array f, Length: " + f.Length.ToString(), font, Brushes.Black, new PointF(lefttop, lefttop));
    
          //its width
          float w = e.Graphics.MeasureString("Array f, Length: " + f.Length.ToString(), font).Width + 100;
    
          //Headline 2
          e.Graphics.DrawString("Array z, Length: " + f.Length.ToString(), font, Brushes.Black, new PointF(lefttop + w, lefttop));
    
          for (int i = 0; i < f.Length; i++)
            e.Graphics.DrawString(f[i].ToString(), font, Brushes.Black, new PointF(lefttop, initialHeight + lefttop + h * i));
    
          for (int i = 0; i < z.Length; i++)
            e.Graphics.DrawString(z[i].ToString(), font, Brushes.Black, new PointF(lefttop + w, initialHeight + lefttop + h * i));
    
          font.Dispose();
    
          e.HasMorePages = false;
        }
      }
    

    Form1.Designer.cs:

      partial class Form1
      {
        /// <summary>
        /// Erforderliche Designervariable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
    
        /// <summary>
        /// Verwendete Ressourcen bereinigen.
        /// </summary>
        /// <param name="disposing">True, wenn verwaltete Ressourcen gelöscht werden sollen; andernfalls False.</param>
        protected override void Dispose(bool disposing)
        {
          if (disposing && (components != null))
          {
            components.Dispose();
          }
          base.Dispose(disposing);
        }
    
        #region Vom Windows Form-Designer generierter Code
    
        /// <summary>
        /// Erforderliche Methode für die Designerunterstützung.
        /// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
        /// </summary>
        private void InitializeComponent()
        {
          this.printPreviewControl1 = new System.Windows.Forms.PrintPreviewControl();
          this.button1 = new System.Windows.Forms.Button();
          this.printDocument1 = new System.Drawing.Printing.PrintDocument();
          this.SuspendLayout();
          // 
          // printPreviewControl1
          // 
          this.printPreviewControl1.Location = new System.Drawing.Point(22, 34);
          this.printPreviewControl1.Name = "printPreviewControl1";
          this.printPreviewControl1.Size = new System.Drawing.Size(701, 572);
          this.printPreviewControl1.TabIndex = 0;
          // 
          // button1
          // 
          this.button1.Location = new System.Drawing.Point(729, 25);
          this.button1.Name = "button1";
          this.button1.Size = new System.Drawing.Size(75, 23);
          this.button1.TabIndex = 1;
          this.button1.Text = "button1";
          this.button1.UseVisualStyleBackColor = true;
          this.button1.Click += new System.EventHandler(this.button1_Click);
          // 
          // printDocument1
          // 
          this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
          // 
          // Form1
          // 
          this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
          this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
          this.ClientSize = new System.Drawing.Size(842, 636);
          this.Controls.Add(this.button1);
          this.Controls.Add(this.printPreviewControl1);
          this.Name = "Form1";
          this.Text = "Form1";
          this.ResumeLayout(false);
    
        }
    
        #endregion
    
        private System.Windows.Forms.PrintPreviewControl printPreviewControl1;
        private System.Windows.Forms.Button button1;
        private System.Drawing.Printing.PrintDocument printDocument1;
      }
    

    regards,

      Thorsten

    • Marked as answer by Hawk337 Thursday, September 23, 2010 10:04 PM
    Thursday, September 23, 2010 8:26 PM
  • number % 2 != 2

    This will always be false. number % 2 can only be 0 or 1.
    Thursday, September 23, 2010 10:26 PM