- Open Visual Studio 2012
- "File" -> "New" -> "Project..."
- Choose "Template" -> "Visual C#" -> "Console application"
Perform Addition
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] + b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
}
}
Output
Note: In the code shown above the sum of two single-dimension arrays is produced. Which is done simply by the "+" operator.
Perform Subtraction
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] - b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
Output
Perform Multiplication
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] * b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
Output
Perform Division
public static void Main(string[] args)
{
const int n = 5;
int[] a = new int[n] { 10, 20, 30, 40, 50 };
int[] b = new int[n] { 5, 4, 3, 2, 1 };
int[] arr = new int[n];
for (int i = 0, j = 0; i < a.Length; i++, j++)
{
arr[i] = a[i] / b[j];
}
for (int i = 0; i < arr.Length; i++)
{
Console.Write(arr[i] + " ");
}
Console.Read();
}
Output
Summary
In this article I have described all the arithmetic operations on a single-dimension array using the same methods in a simple way.