0% found this document useful (0 votes)
83 views7 pages

CH 6 Arrays: Array Is A Collection of Variables of The Same Type That Are Referenced by A

An array is a collection of variables of the same type that can be accessed using indices. Arrays allow for easy specification and fast access of elements but the size must be known beforehand. Elements in an array can be accessed, initialized, sorted, and searched. Common array operations include finding the sum, average, maximum and minimum of elements as well as using sorting algorithms like bubble sort and searching techniques like linear search.

Uploaded by

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

CH 6 Arrays: Array Is A Collection of Variables of The Same Type That Are Referenced by A

An array is a collection of variables of the same type that can be accessed using indices. Arrays allow for easy specification and fast access of elements but the size must be known beforehand. Elements in an array can be accessed, initialized, sorted, and searched. Common array operations include finding the sum, average, maximum and minimum of elements as well as using sorting algorithms like bubble sort and searching techniques like linear search.

Uploaded by

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

Ch 6 Arrays

Array is a collection of variables of the same type that are referenced by a


common name.

Advantages of Arrays
a)Easy to specify
b)Free from run-time overheads
c)Random access of elements
d)Fast sequential access
e)Simple code

Disadvantages of Arrays
a)Need to know the size
b)careful design required
c)Not suitable for situations demanding varying memory-sizes

Array Declaration

dataType arrayName[arraySize];

For example,

float mark[5];

Access Array Elements

You can access elements of an array by indices.

1
ICSE Class 10 Computer Applications
Array initialization

int mark[5] = {19, 10, 8, 17, 9};

OR

int mark[] = {19, 10, 8, 17, 9};

Linear Search refers to the searching technique in which each element of an


array is compared with the search item,one by one,until the search-item is
found or all elements have been compared.

Bubble sort is a simple sorting algorithm. This sorting algorithm is


comparison-based algorithm in which each pair of adjacent elements is
compared and the elements are swapped if they are not in order.

//1. Program to count the no. of elements in an array


import java.util.*;

public class ArrayLength


{
public static void main(String[] args)
{
int len1,len2;
int A[]={1,4,5,7,9,11,55,23,44};
String B[]={"lina","tina","mina","nina"};

len1=A.length; // to count the no. of elements in an integer array


len2=B.length; // to count the no. of elements in an string array

System.out.println("Length of an integer array = " + len1);


System.out.println("Length of an string array = " + len2);
}
}

2
ICSE Class 10 Computer Applications
//2. To accept and display the array elements
import java.util.*;

public class ArrayIntro


{
public static void main(String[] args)
{
int i;
int A[]=new int[5]; //declaration of array

Scanner kb=new Scanner(System.in);


System.out.println("Enter 5 elements");
for(i=0;i<5;i++)
{
A[i]=kb.nextInt(); //to accept array elements
}
for(i=0;i<5;i++)
{
System.out.println( A[i] + " "); //to display array elements
}
}
}

3
ICSE Class 10 Computer Applications
//3.To find sum,average,min and max of 5 numbers
import java.util.*;

public class Array1D {

public static void main(String[] args) {


int i,min,max,sum=0,avg=0;
int A[]=new int[5]; //declaration of array

Scanner kb=new Scanner(System.in);


System.out.println("Enter 5 elements");

for(i=0;i<5;i++)
{
A[i]=kb.nextInt(); //to accept array elements
sum=sum+A[i]; //to find the sum of array elements
}

avg=sum/5; //to find the average of the array elements


max=min=A[0];

// to find max and min of the array elements


for(i=0;i<5;i++)
{
if(max<A[i])
max=A[i];
if(min>A[i])
max=A[i];
}

//display the min,max,sum and average


System.out.println("Maximum = " +max);
System.out.println("Minumum = " +min);
System.out.println("Sum = " + sum);
System.out.println("Average = " + avg);
}
}

4
ICSE Class 10 Computer Applications
//4. Linear Search
import java.util.*;

public class LinearSearch


{

public static void main(String[] args)


{
int i,sh,k=0;
int A[]=new int[5]; //declaration of array

Scanner kb=new Scanner(System.in);

System.out.println("Enter 5 elements");
for(i=0;i<5;i++)
{
A[i]=kb.nextInt(); //to accept array elements

System.out.println("Enter the element to be searched");


sh=kb.nextInt(); //to accept element to be searched

for(i=0;i<5;i++)
{
if(A[i]==sh)
k=1;

}
if(k==1)
System.out.println("The required no " + sh + " is present" );
else
System.out.println("The required no " + sh + " is not present" );
}
}

5
ICSE Class 10 Computer Applications
//5. Bubble Sort - Numbers
public class BubbleSortEg
{
public void bubbleSort(int A[])
{
int i,j,t;

for(i=0;i<5;i++) //i = no. of iterations


{
for(j=0;j<4;j++)
{
if(A[j]>A[j+1])
{
//to exchange the values
// A[j]->a, A[j+1]->b
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
}

void display(int A[])


{
int i;
for(i=0;i<5;i++)
{
System.out.print(A[i]+"\t");
}
}

public static void main(String[] args)


{
BubbleSortEg b1=new BubbleSortEg();
int A[]=new int[]{5,3,2,8,4};
b1.bubbleSort(A);
b1.display(A);
}
}

6
ICSE Class 10 Computer Applications
//6. Bubble Sort - Strings
public class BubbleSortStringEg
{
public void bubbleSort(String A[])
{

int i,j;
String t;
for(i=0;i<5;i++)
{
for(j=0;j<4;j++)
{
if(A[j].compareTo(A[j+1])>0)
{
t=A[j];
A[j]=A[j+1];
A[j+1]=t;
}
}
}
}

void display(String A[])


{
int i;
for(i=0;i<5;i++)
{
System.out.print(A[i]+"\t");
}
}

public static void main(String[] args) {


BubbleSortStringEg b1=new BubbleSortStringEg();
String A[]={"Delhi","Bangalore","Agra","Mumbai","Calcutta"};
b1.bubbleSort(A);
b1.display(A);
}
}

7
ICSE Class 10 Computer Applications

You might also like