You are on page 1of 13

JERICK CANGAS ARIRAO

Computer Programming 2
BSIT-1B

1. Write a program that’s asks for a dimension of a 2-D array. The user enters the elements
then the program prints the matrix.

using System;

public class Exercise 1

class program

public static void Main()

int i,j;
int[,] arr1 = new int[4,3];

Console.WriteLine("Enter values of the element:\n");

for(I=0;I<4;I++)

for(j=0;j<3;j++)

{
Console.Write("element - [{0},{1}] : ",i,j);

arr1[i,j] = Convert.ToInt32(Console.ReadLine());

Console.Write("\nThe matrix : \n");

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

Console.Write("\n");

for(j=0;j<3;j++)

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

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

Console.ReadKey();

2. Write a program to read the values in an array and display it in reverse order.
using System;

public class Exercise 2

class Program

static void Main(string[] args)

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

3. Write a program to print all unique elements in an array. (Unique- means no duplicate).

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

Console.Write("\nThe unique elements found in the array are : \n");

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

ctr=0;

for(j=0; j<i-1; j++)

if(arr1[i]==arr1[j])

ctr++;

for(k=i+1; k<n; k++)

if(arr1[i]==arr1[k])

ctr++;

if(arr1[i]==arr1[i+1])

i++;
}

if(ctr==0)

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

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

}
4. Write a program to merge two arrays of same size sorted in descending order.

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

namespace ConsoleApplication24
{
class Program
{

static void Main(string[] args)


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

s3 = s1 + s2;

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

arr3[i] = arr1[i];

for (j = 0; j < s2; j++)

arr3[i] = arr2[j];
i++;

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;

Console.Write("The merged array in descending order is :\n");

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

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

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

Console.ReadKey();

}
}

5. Write a program to count the frequency of each element of an array.

using System;

public class Exercise8

public static void Main()

int[] arr1 = new int[100];

int[] fr1 = new int[100];

int n, i, j, ctr;

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());
fr1[i] = -1;

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

ctr = 1;

for(j=i+1; j<n; j++)

if(arr1[i]==arr1[j])

ctr++;

fr1[j] = 0;

if(fr1[i]!=0)

fr1[i] = ctr;

Console.Write("\nThe frequency of all elements of the array : \n");

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

if(fr1[i]!=0)

Console.Write("{0} occurs {1} times\n", arr1[i], fr1[i]);


}

6. Write a program in C# to separate odd and even integers in separate arrays.

using System;

namespace HelloWorld

class Program

static void Main(string[] args)

int[] arr1 = new int[10];

int[] arr2 = new int[10];


int[] arr3 = new int[10];

int i,j=0,k=0,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("El");

You might also like