0% found this document useful (0 votes)
56 views11 pages

Tutorial 2 Arrray 21SOEIT11028

Uploaded by

Abhay Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views11 pages

Tutorial 2 Arrray 21SOEIT11028

Uploaded by

Abhay Shukla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Array Tutorial – 2

Abhay shukla
21SOEIT11028

1. Write a C# Sharp program that stores elements in an array and prints them..

Code :

using System;
public class Exercise1
{
public static void Main()
{
Console.WriteLine("Abhay");
int[] arr = new int[5];
int i;

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


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

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


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

Output:

2. Write a program of sorting an array. Declare single dimensional array and accept 5 integer
values from the user. Then sort the input in ascending order and display output.

Code :

using System;
public class Exercise11
{
public static void Main()
{
Console.WriteLine("Abhay");
int[] arr1 = new int[10];
int n, i, j, tmp;

Console.Write("Input the size of 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());
}

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


{
for(j=i+1; j<n; j++)
{
if(arr1[j] < arr1[i])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}
Console.Write("\nElements of array in sorted ascending order:\n");
for(i=0; i<n; i++)
{
Console.Write("{0} ", arr1[i]);
}
Console.Write("\n\n");
}

Output:

3. Write a C# Sharp program to read n values in an array and display them in reverse order.

Code :

using System;
public class Exercise2
{
public static void Main()
{
Console.WriteLine("Abhay");
int i,n;
int[] a= new int[100];

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");
}
}

4. Write a C# Sharp program to copy the elements of one array into another array.

Code :
using System;
public class Exercise4
{
public static void Main()
{
Console.WriteLine("Abhay");
int[] arr1 = new int[100];
int[] arr2 = new int[100];
int i, 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());
}

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


{
arr2[i] = arr1[i];
}

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");
}
}

5. Write a C# Sharp program to count duplicate elements in an array.

Code :

using System;
public class Exercise5
{
public static void Main()
{
Console.WriteLine("Abhay");
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("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());
}

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


{
arr2[i] = arr1[i];
arr3[i] = 0;
}

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");
}
}

6. Write a C# Sharp program to find the maximum and minimum elements in an array.

Code :

using System;
public class Exercise9
{
public static void Main()
{
Console.WriteLine("Abhay");
int[] arr1= new int[100];
int i, mx, mn, 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());
}

mx = arr1[0];
mn = arr1[0];

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


{
if(arr1[i]>mx)
{
mx = arr1[i];
}

if(arr1[i]<mn)
{
mn = arr1[i];
}
}
Console.Write("Maximum element is : {0}\n", mx);
Console.Write("Minimum element is : {0}\n\n", mn);
}
}

7. Write a program in C# Sharp to separate odd and even integers into separate arrays.

Code :

using System;
public class Exercise10
{
public static void Main()
{
Console.WriteLine("UTTAM SOJITRA");
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("element - {0} : ",i);
arr1[i] = Convert.ToInt32(Console.ReadLine());
}

for(i=0;i<n;i++)
{
if (arr1[i]%2 == 0)
{
arr2[j] = arr1[i];
j++;
}
else
{
arr3[k] = arr1[i];
k++;
}
}

Console.Write("\nThe Even elements are : \n");


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

Console.Write("\nThe Odd elements are :\n");


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

8. Write a C# Sharp program to sort array elements in descending order.

Code :

using System;
public class Exercise12
{
public static void Main()
{
Console.WriteLine("Abhay");

int[] arr1 = new int[10];

int n, i, j, tmp;

Console.Write("Input the size of 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());
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(arr1[i] < arr1[j])
{
tmp = arr1[i];
arr1[i] = arr1[j];
arr1[j] = tmp;
}
}
}

Console.Write("\nElements of the array in sorted descending order:\n");


for(i=0; i<n; i++)
{
Console.Write("{0} ", arr1[i]);
}
Console.Write("\n\n");
}
}
9. Write a C# Sharp program to delete an element at the desired position from an array.

Code :

using System;
public class Exercise15
{
public static void Main()
{
Console.WriteLine("Abhay");
int i,pos,n;
int[] arr1 = new int[50];

Console.Write("Input the size of array : ");


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

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


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

Console.Write("\nInput the position where to delete: ");


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

i=0;
while(i!=pos-1)
i++;

while(i<n){
arr1[i]=arr1[i+1];
i++;
}
n--;
Console.Write("\nThe new list is : ");
for(i=0;i<n;i++)
{
Console.Write(" {0}",arr1[i]);
}
Console.Write("\n\n");

}
}
10. Write a C# Sharp program for adding two matrices of the same size.

Code :

using System;
public class Exercise19
{
public static void Main()
{
Console.WriteLine("Abhay");
int i,j,n;
int[,] arr1 = new int[50,50];
int[,] brr1 = new int[50,50];
int[,] crr1 = new int[50,50];

Console.Write("Input the size of the square matrix (less than 5): ");
n = Convert.ToInt32(Console.ReadLine());

Console.Write("Input elements in the first matrix :\n");


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

Console.Write("Input elements in the second matrix :\n");


for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
Console.Write("element - [{0},{1}] : ",i,j);
brr1[i,j] = Convert.ToInt32(Console.ReadLine());
}
}
Console.Write("\nThe First matrix is :\n");
for(i=0;i<n;i++)
{
Console.Write("\n");
for(j=0;j<n;j++)
Console.Write("{0}\t",arr1[i,j]);
}

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


for(i=0;i<n;i++)
{
Console.Write("\n");
for(j=0;j<n;j++)
Console.Write("{0}\t",brr1[i,j]);
}

for(i=0;i<n;i++)
for(j=0;j<n;j++)
crr1[i,j]=arr1[i,j]+brr1[i,j];
Console.Write("\nThe Addition of two matrix is : \n");
for(i=0;i<n;i++){
Console.Write("\n");
for(j=0;j<n;j++)
Console.Write("{0}\t",crr1[i,j]);
}
Console.Write("\n\n");
}
}

You might also like