C# Exercises

Home AgriMetSoft About Contact

Sum, Subtract, Product, and Divide of Array in C#

	    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Sum_Subtract_Multiply_Vector
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
      // c# Sum Subtract Multiply divide vector one by one
      double[] a1 = new double[] { 1.2, 3.2, 2.3, 4.0 };
      double[] a2 = new double[] { 3.1, 2.6, 2.9, 3.4 };
      double[] arr1 = new double[a1.Length];
      double[] arr2 = new double[a1.Length];
      double[] arr3 = new double[a1.Length];
      double[] arr4 = new double[a1.Length];
      // Method 1
      for (int i = 0; i < arr1.Length; i++)
      {
        arr1[i] = a1[i] + a2[i];
        arr2[i] = a1[i] - a2[i];
        arr3[i] = a1[i] * a2[i];
        arr4[i] = a1[i] / a2[i];
      }
      double[] ar1 = new double[a1.Length];
      double[] ar2 = new double[a1.Length];
      double[] ar3 = new double[a1.Length];
      double[] ar4 = new double[a1.Length];
      // Fast Method
      ar1 = a1.Zip(a2, (u, v) => u + v).ToArray();
      ar2 = a1.Zip(a2, (u, v) => u - v).ToArray();
      ar3 = a1.Zip(a2, (u, v) => u * v).ToArray();
      ar4 = a1.Zip(a2, (u, v) => u / v).ToArray();

      //=================2Dim Jagged Array===========================
      double[][] b1 = new double[2][];
      b1[0] = new double[] { 2.2, 2.8, 3.1 };
      b1[1] = new double[] { 3.2, 2.3, 4.0 };
      double[][] b2 = new double[2][];
      b2[0] = new double[] { 2.6, 2.9, 3.4 };
      b2[1] = new double[] { 3.1, 2.8, 3.9, };
      var b3 = b1.Zip(b2, (u, v) => u.Zip(v, (i, j) => i + j).ToArray()).ToArray();
    }
  }
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


Sum, Subtract, product, and divide of vectors - Element by Element in C#


List of Exercises