You are on page 1of 5

Huynh nhat nam

1/

Write a program that has following functions to manage an integer array:

1. Enter an integer array that has N elements 


2. Print all elements to the screen
3. Sum values of the array
4. Calculate the average value of array elements

using System;

public class Avg

public static void Main()

int[] arr = { 1, 2, 6, 2, 18 };

int i=0;

int sum = 0;

float average = 0.0F;

for (i = 0; i < arr.Length; i++)

sum += arr[i];

}
average = (float)sum / arr.Length;

Console.WriteLine("Average of Array elements: "+ average);

5. Find the index of an array element

using System;

public static class Extensions

public static int findIndex<T>(this T[] array, T item) {

return Array.IndexOf(array, item);

public class Example

public static void Main()

int[] array = { 1, 2, 3, 4, 5 };
int item = 4;

int index = array.findIndex(item);

if (index != -1) {

Console.WriteLine(String.Format("Element {0} is found at index {1}", item, index));

else {

Console.WriteLine("Element not found in the given array.");

6. Find the maximum value of the array And 7.Find the minimum value of the array

using System;

public class Exercise9

public static void Main()

int[] arr1= new int[100];

int i, mx, mn, n;

Console.Write("\n\nFind maximum and minimum element in 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());

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. Quit

2/

Write a program that has following functions:

1. Create a random integer array that has N elements


2. Print all elements to the screen
3. Sort the array in ascending order
4. Sort the array in descending order
5. Add a new element at the beginning of the array
6. Add a new element at the end of the array
7. Add a new element at specific position of the array
8. Quit

You might also like