You are on page 1of 7

1)

using System;
public class Exp1
{
public static void Main()
{
int[] arr = new int[10];
int i;
Console.Write("\n\nRead and Print elements of an array:\n");
Console.Write("-----------------------------------------\n");

Console.Write("Input 10 elements in the array :\n");


for (i = 0; i < 10; i++)
{
Console.Write("element - {0} : ", i);
arr[i] = Convert.ToInt32(Console.ReadLine());
}

Console.Write("\nElements in array are: ");


for (i = 0; i < 10; i++)
{
Console.Write("{0} ", arr[i]);
}
Console.Write("\n");
Console.ReadKey();
}
}

2)

using System;
public class Exp2
{
public static void Main()
{
int i, n;
int[] a = new int[100];

Console.Write("\n\nRead n number of values in an array and display it in reverse


order:\n");

Console.Write("Input the number of elements to store in the array :");


n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} number of elements in the array :\n", n);


for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
a[i] = Convert.ToInt32(Console.ReadLine());
}

Console.Write("\nThe values store into the array are : \n");


for (i = 0; i < n; i++)
{
Console.Write("{0} ", a[i]);
}

Console.Write("\n\nThe values store into the array in reverse are :\n");


for (i = n - 1; i >= 0; i--)
{
Console.Write("{0} ", a[i]);
}
Console.Write("\n\n");
Console.ReadKey();
}
}

3)

using System;
public class Exp3
{
public static void Main()
{
int[] a = new int[100];
int i, n, sum = 0;

Console.Write("\n\nFind sum of all elements of array:\n");


Console.Write("--------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");


n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n", n);


for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
a[i] = Convert.ToInt32(Console.ReadLine());
}

for (i = 0; i < n; i++)


{
sum += a[i];
}

Console.Write("Sum of all elements stored in the array is : {0}\n\n", sum);


Console.ReadKey();
}
}

4)

using System;
public class Exp4
{
public static void Main()
{
int[] arr1 = new int[100];
int[] arr2 = new int[100];
int i, n;

Console.Write("\n\nCopy the elements one array into another array :\n");


Console.Write("----------------------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");


n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n", n);


for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
/* Copy elements of first array into second array.*/
for (i = 0; i < n; i++)
{
arr2[i] = arr1[i];
}

/* Prints the elements of first array */


Console.Write("\nThe elements stored in the first array are :\n");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", arr1[i]);
}

/* Prints the elements copied into the second array. */


Console.Write("\n\nThe elements copied into the second array are :\n");
for (i = 0; i < n; i++)
{
Console.Write("{0} ", arr2[i]);
}
Console.Write("\n\n");
Console.ReadKey();
}
}

5)

using System;
public class Exp5
{
public static void Main()
{
int[] arr1 = new int[100];
int[] arr2 = new int[100];
int[] arr3 = new int[100];
int s1, s2, mm = 1, ctr = 0;
int i, j;
Console.Write("\n\nCount total number of duplicate elements in an array:\n");
Console.Write("---------------------------------------------------------\n");

Console.Write("Input the number of elements to be stored in the array :");


s1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n", s1);


for (i = 0; i < s1; i++)
{
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
/*----------------- copy in other array ------------------------------------*/
for (i = 0; i < s1; i++)
{
arr2[i] = arr1[i];
arr3[i] = 0;
}
/*------------------- mark the elements are duplicate -------------------------*/
for (i = 0; i < s1; i++)
{
for (j = 0; j < s1; j++)
{
if (arr1[i] == arr2[j])
{
arr3[j] = mm;
mm++;
}
}
mm = 1;
}
/*--------------- Prints the array ------------------------------------*/
for (i = 0; i < s1; i++)
{
if (arr3[i] == 2) { ctr++; }
}
Console.Write("The number of duplicate elements is: {0} \n", ctr);

Console.Write("\n\n");
Console.ReadKey();
}
}

6)

using System;
public class Exp6
{
public static void Main()
{
int n, ctr = 0;
int[] arr1 = new int[100];
int i, j, k;
Console.Write("\n\nPrint all unique elements of an array:\n");
Console.Write("------------------------------------------\n");
Console.Write("Input the number of elements to be stored in the array :");
n = Convert.ToInt32(Console.ReadLine());
Console.Write("Input {0} elements in the array :\n", n);
for (i = 0; i < n; i++)
{
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}
/*Checking duplicate elements in the array */
Console.Write("\nThe unique elements found in the array are : \n");
for (i = 0; i < n; i++)
{
ctr = 0;
/*Check duplicate bifore the current position and
increase counter by 1 if found.*/
for (j = 0; j < i - 1; j++)
{
/*Increment the counter when the seaarch value is duplicate.*/
if (arr1[i] == arr1[j])
{
ctr++;
}
}
/*Check duplicate after the current position and
increase counter by 1 if found.*/
for (k = i + 1; k < n; k++)
{
/*Increment the counter when the seaarch value is duplicate.*/
if (arr1[i] == arr1[k])
{
ctr++;
}
/* Duplicate numbers next to each other*/
if (arr1[i] == arr1[i + 1])
{
i++;
}
}
/*Print the value of the current position of the array as unique value
when counter remain contains its initial value.*/
if (ctr == 0)
{
Console.Write("{0} ", arr1[i]);
}
}
Console.Write("\n\n");
Console.ReadKey();
}
}

7)
using System;
public class Exp7
{
public static void Main()
{
int[] arr1 = new int[100];
int[] arr2 = new int[100];
int[] arr3 = new int[200];
int s1, s2, s3;
int i, j, k;
Console.Write("\n\nMerge two arrays of same size sorted in ascending order.\n");
Console.Write("------------------------------------------------------------\n");

Console.Write("Input the number of elements to be stored in the first array :");


s1 = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n", s1);


for (i = 0; i < s1; i++)
{
Console.Write("element - {0} : ", i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}

Console.Write("Input the number of elements to be stored in the second array :");


s2 = Convert.ToInt32(Console.ReadLine());

Console.Write("Input {0} elements in the array :\n", s2);


for (i = 0; i < s2; i++)
{
Console.Write("element - {0} : ", i);
arr2[i] = Convert.ToInt32(Console.ReadLine());
}

/* size of merged array is size of first array and size of second array */
s3 = s1 + s2;

/*----------------- insert in the third array------------------------------------


*/
for (i = 0; i < s1; i++)
{
arr3[i] = arr1[i];
}
for (j = 0; j < s2; j++)
{
arr3[i] = arr2[j];
i++;
}
/*----------------- sort the array in ascending order ---------------------------
*/
for (i = 0; i < s3; i++)
{
for (k = 0; k < s3 - 1; k++)
{

if (arr3[k] >= arr3[k + 1])


{
j = arr3[k + 1];
arr3[k + 1] = arr3[k];
arr3[k] = j;
}
}
}
/*--------------- Prints the merged array ------------------------------------*/
Console.Write("\nThe merged array in ascending order is :\n");
for (i = 0; i < s3; i++)
{
Console.Write("{0} ", arr3[i]);
}
Console.Write("\n\n");
Console.ReadKey();
}
}

You might also like