Arrays: //exercise1//: Using Using Using Using Using Namespace Class Static Void String Int New Int Int

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

Arrays:

//exercise1//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{
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.ReadLine();
}
}
}
Output:
//exercise2//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

int n;
Console.Write("Enter number of elements");
n = Convert.ToInt32(Console.ReadLine());
int[] arr1 = new int[n];
int j;
Console.Write("reversing the elements in array\n");
for(j=0;j<n;j++)
{
Console.Write("elements {0} :", j);
arr1[j] = Convert.ToInt32(Console.ReadLine());
}
for(j=n-1;j>=0;j--)
{
Console.Write("{0} ", arr1[j]);
}
Console.ReadLine();
}
}
}
Output:
//exercise3//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

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.ReadLine();
}
}
}
Output: u/

///////////
//rgerhbtrnbsgrngnbfvbffd
wwwwwwwwwwwerfgegefbefwwwwwwwwwffghgtntgntgn
//////

//exercise4//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

int[] a = new int[100];


int i, n;
int[] b = new int[100];

Console.Write("\n\ncopy 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++)


{
Console.Write(b[i]= a[i]);
}

Console.ReadLine();
}
}
}

//exercise 5//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

int[] a = new int[100];


int i,j,p, n;
int isunique;

Console.Write("\n\nduplicate 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++)


{
isunique = 1;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(p=j;p<n-1;p++)
{
a[p] = a[p + 1];
}
n--;
j--;
isunique = 0;
}
}
if(isunique!=1)
{
for(j=i;j<n-1;j++)
{
a[j] = a[j + 1];
}
n--;
i--;
}
}
for(i=0;i<n;i++)
{
Console.WriteLine(a[i] + "\t");
}

Console.ReadLine();
}
}
}
//exercise6//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

int[] a = new int[100];


int[] b = new int[100];
int[] c = new int[100];

int i,j,n1,n2,n3,p;

Console.Write("\n\nmerge elements of array:\n");


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

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


n1 = Convert.ToInt32(Console.ReadLine());
Console.Write("Input the number of elements to be stored in the array2 :");
n2 = Convert.ToInt32(Console.ReadLine());

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


for (i = 0; i < n1; i++)
{
Console.Write("element - {0} : ", i);
a[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Input {0} elements in the array :\n", n2);
for (i = 0; i < n2; i++)
{
Console.Write("element - {0} : ", i);
b[i] = Convert.ToInt32(Console.ReadLine());
}
n3 = n2 + n1;
for (i = 0; i < n1; i++)
{
c[i] = a[i];
}
for(j=0;j<n2;j++)
{
c[i] = b[j];
i++;
}
for (i = 0; i < n3; i++)
{
Console.Write("{0}\t", c[i]);
}

for (i=0;i<n3;i++)
{

for(j=i+1;j<n3;j++)
{
if(c[i]>=c[j])
{
p = c[j];
c[j] = c[i];
c[i] = p;

}
}
Console.Write("\n\n");
for (i=0;i<n3;i++)
{

Console.Write("{0}\t", c[i]);
}

Console.ReadLine();
}
}
}
//exercise7//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

int[] a = new int[100];


int[] b = new int[100];

int i,j,n,ctr;

Console.Write("\n\nfrequency elements of array:\n");


Console.Write("--------------------------------------\n");
Console.Write("Input the number of elements to be stored in the array1 :");
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());
b[i] = -1;
}
for (i = 0; i < n; i++)
{
ctr = 1;
for (j = i + 1; j < n; j++)
{
if (a[i] == a[j])
{
ctr++;
b[j] = 0;
}
}

if (b[i] != 0)
{
b[i] = ctr;
}
}
Console.Write("\nThe frequency of all elements of the array : \n");
for (i = 0; i < n; i++)
{
if (b[i] != 0)
{
Console.Write("{0} occurs {1} times\n", a[i], b[i]);
}
}

Console.ReadLine();
}
}
}
//exercise8//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

int[] a = new int[100];

int i,n,minimum,max;

Console.Write("\n\nmax and min elements of array:\n");


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

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


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());

}
max = a[0];
for (i = 0; i < n; i++)
{

if (max <= a[i])


{
max = a[i];
}
}
minimum = a[0];
for (i = 0; i < n; i++)
{

if (minimum >= a[i])


{
minimum = a[i];
}
}

Console.Write("minimum and maximum elements in array are {0} and


{1}",minimum,max);

Console.ReadLine();
}
}
}
//exercise9//
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace csharp_arrays1
{
class Program
{
static void Main(string[] args)
{

int[] a = new int[100];


int[] b = new int[100];
int[] c = new int[100];

int i,p=0,q=0,n;

Console.Write("\n\nmax and min elements of array:\n");


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

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


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++)
{
if(a[i]%2==0)
{
b[p++] = a[i];
}
else
{
c[q++] = a[i];
}
}
Console.Write("even elements are :", b[i]);
for (i=0;i<p;i++)
{

Console.Write("{0}", b[i]);
}
Console.Write("\n");
Console.Write("odd elements are :");
for (i = 0; i < q; i++)
{

Console.Write("{0}", c[i]);
}
Console.ReadLine();
}
}
}

You might also like