Arrays_notes
Arrays_notes
Array is the static memory allocation. It allocates the memory for the same data type in sequence. It contains multiple
values of same types. It also store the values in memory at the fixed size. Multiple types of arrays are used in any
programming language such as: one -dimensional, two - dimensional or can say multi - dimensional.
Declaration of an array:
int num[]; or int num = new int[2];
Some times user declares an array and it's size simultaneously. You may or may not be define the size in the
declaration time. such as: int num[] = {50,20,45,82,25,63};
Ques 3:- How one dimension array is different from double dimension array?
Ans:- One Dimension:- A list of fixed number of homogeneous data elements.
Double Dimension:- An array in which each element itself an array.
Ques 5:- What is the difference between linear and binary search?
Ans:- Linear:- Each element of the array is compared with the given item to be searched for, one by one.
Binary:- Searches for the given item in a sorted array the search segment reduces to half at every successive stage.
POINT TO REMEMBER
1. Data grouped together into a single unit for a specific purpose is known as datastructure.
2. Array is a data structure where similar types of data are put together, stored in continuous locations and is
referred to by a single name.
3. Arrays occupy a group of contiguous memory locations all having the same nameand type.
4. To refer to a particular location, we specify the name of the array and its positionnumber.
5. Its position number is called an element or a subscript or an index of the array.
6. Subscript of an array must be an integer.
7. In Java the first element of an array is its zeroth element and the last element 1 lessthan the total elements in
an array.
8. Subscripts are enclosed within square brackets next to the name of the array.
9. To retrieve an individual array element, array name along with its subscript should be specified.
10. An array of smaller size can be initialised using an initializer list.
11. Newly created array of integers is automatically filled with default values: 0 for numbers, false for boolean,
and null for objects.
12. Searching and sorting are two basic array-processing techniques that are frequently used.
13. Sorting refers to rearranging the data of an array in some particular order increasing or decreasing.
14. Searching refers to finding an item in an array, depending on some specific criterion.
15. Linear searching is the simplest searching method and is done when the required element is searched
sequentially in an array.
16. Binary searching is a faster way as it eliminates one half part every time while the search is carried on.
class lsearch
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int arr[]={35,34,67,87,45,33,11,91,2,44,32};
int n,len,flag,i;
System.out.println( “enter a number”);
n=Sn.nextInt();
flag=0;
len=arr.length;
for(i=0;i<len;i++)
{
if(arr[i]==n)
{
flag=1;
break;
}
}
if(flag==1)
System.out.println( n+”found at index ”+i);
else
System.out.println( n+”is not in list”);
}
}
Program 5- To sort numbers in ascending order using selection sort technique [2006]
import java.util.*;
public class selectionSort{
public static void main(String a[]) throws Exception
{
int i,j;
int a[] = {12,9,4,99,120,1,3,10};
System.out.println("Values Before the sort:\n");
for(i = 0; i < a.length; i++)
System.out.print( a[i]+" ");
System.out.println();
n=a.length;
for(i = 0; i < n; i++)
{
for(j = i+1; j < n; j++)
{
if(a[i] > a[j]){
t = a[j];
a[j]=a[i];
a[i]=t;
}
}
}
System.out.print("Values after the sort:\n");
for(i = 0; i <a.length; i++)
System.out.print(a[i]+" ");
}
}
Program 6- to sort numbers in ascending order using Exchange selection sort technique[ important]
import java.util.*;
public class exchange{
public static void main(String a[]) throws Exception
{
int i,j, pos,min;
int a[] = {12,9,4,99,120,1,3,10};
System.out.println("Values Before the sort:\n");
for(i = 0; i < a.length; i++)
System.out.print( a[i]+" ");
System.out.println();
n=a.length;
for(i = 0; i < n; i++)
{
min=a[i];
pos=i;
for(j = i+1; j < n; j++)
{
if( a[j]<min)
{
min=a[j];
pos=j
}
}
t = a[j];
a[j]=a[pos];
a[pos]=t;
}
}
}
System.out.print("Values after the sort:\n");
for(i = 0; i <a.length; i++)
System.out.print(a[i]+" ");
}
}
Program 7- Write a menu driven program to find sum of prime numbers or find smallest and largest number
or array.
import java.util.*;
class array_menu
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
int arr[]={35,34,67,87,45,33,11,91,2,44,32};
Program 8- Write a program to store number in a matrix and make is intractive to find
1. row wise sum
2. column wise sum
3. diagonals sum
4. border elements sum
import java.util.*;
class matrix_menu
{
public static void main(String args[]) throws Exception
{
Scanner Sn=new Scanner(System.in));
tot=tot+marks[i];
}
System.out.println(“Average marks of the student”+(tot/50));
System.out.println(“Highest mark is ”+high+” highest scores is “+name[pos]);
}
}
1) Define an array.
2) Differentiate between data type and data structure.
3) Name four major operations on linear data structures.
4) What are the preconditions for Binary search to be performed on a single dimensional array?
5) State condition(s) which binary search is applicable.
6) Explain the bubble sorting technique in array with an example.
7) What is difference between single dimensional array and multidimensional array
8) What is sorting?
9) What are the advantages of using an array
2.)WAP to enter 11 names of the cricketers & their countries in two arrays . Then Enter the names of a country of
your choice and print all those names of cricketers Who belong to that country. Repeat the process according to the
users choice.
3) WAP to open two arrays and store 10 numbers in each. Store their sum in array S And product in another array P.
Print array S and P.
4) WAP to enter any 20 numbers in an array. Print the largest and the smallest numbers Along with their locations.
5) Write a program to create a single dimension array to input 10 integers. Print array elements in a single line. Alsoe
print sum of those elements which are divisible by 4.
6) A company produces the different products A1 and A2 …………….. A10 with P1, P2 ………………..P10 as per
prices per unit and Q1,Q2,…………….Q10 as the the quantities
Produced respectively.
(i) Store the data in 10 * 20 matrix as follows :
COST/UNIT(RS) QUANTITY
P1 Q1
P2 Q2
: :
: :
P10 Q10
7.) Wap to accept 10 integer from a user in a single dimension array and sort the array using bubble sort.
8) Wap to accept 10 integer from a user in a single dimension array and sort the array using Selection sort.
9) Write a program to create three single dimensional arrays empcode[](integer), sal[](double), spl[](double) to store
employee code, monthly salary and special allowance of 10 employees. Calculate the total monthly salary (sal+spl)
and annual salary of each employee of each employees. Print the salary details in tabular form including all the data.
At the end print total of monthly salary and total of allowances.
12) Design a function void showtax(double tax[]). The function takes single subscripted variable tax[] as function
argument with income tax amount of 8 salarid persons. Print average tax of all the persons along with maximum and
minimum tax amount paid by the persons,
13) Write a program to initialize the following integer arrays. Print a suitable message whether the two array are
identical or not. Make use of Boolean data type to decide whether the two arrays are same or not.
Ar[]={146,236,346,456} and Br={146,236,346,456}
15) Wap to display frequency table from an array marks[] which hold marks of 100 students
ranges are as given 0-10,11-20,21-30…..91-100
16.) Wap to input date in dd,mm,yy and display corresponding date of the year. Eg 21-7-2004
process:- 31+29+31+30+31+30+21=203
output 203
17) Write a program to input a single dimension array ar[] of 10 integer elements store only single digit integers in
another array br[]. Print array ar[] and br[] in two different lines.
18) Write a program to inititalize the given temperatures in an array. Find the minimum and maximum temperatures
along with the average of all the temperatures.
Temperatures: 23.4, 44.5,12.5,28.7,34.5,6.7
Output: Minimum Temperature:- 6.7
Maximum Temperature: 44.5
Average Temperature:- XXX
19) Define a function void findmarks(float m[]). The function takes single subscripted variable m[] as function
argument with marks of 10 students.Print average of 10 students along with highest and lowest marks scored by the
student with suitable message.
20) Wap to reverse the array without using another array.
22) wap to store numebr in a single dimension array and store them even number in array e[] and odd numbers in
array o[].
23) Wap to input decimal number and convert it into binary equivalent.
24) Write a program to input numbers in two array A[] and array B[] of size m and n. create another array C[] that
contains common elements of both array A[] and B[].
25) WAP to store 20 numbers in a 1D array and count the sum of all numbers and the sum of all evrn numbers of two
digits, which are exactly divisible by 7.
26) WAP to store 10 alphabets in a 1D array X and copy all small letters in array Y and capital letters in array Z.
27) WAP to store numbers in array A and sort it in ascending or descending order according to user choice.
28) WAP to store n small alphabets in an array. If the user enters it in capital then store them in the array after
converting them in small. And sort them in descending order using selection sort.
29) WAP to store n numbers in an array and copy all prime numbers in array PRIME and perfect numbers in array
PERFECT.
30) Write a program to read a set of N values from a user and store it in a one dimensional array. Compute and
display AM standard Deviation(SD) and Variance(var) for the N values. N is a user input.
31) Write a program to read a set of N arbitrary integer values from a user and store it in a single dimensional array.
Check whether the number is prime or composite while displaying the array of numbers. N is a user defined constant.
32) Write a program to input n numbers in an array and sort them using selection sort technique in descending order.
Input a number n and search for the number in the array using binary search technique.
33) Write a program to input n number in an array display frequency for each number. As well as display mode. Mode
is the number which occurs most frequently in the array.
34)Write a program to input name and marks of 3 subjects of 50 students. Perform the given
process.
Display name and grade of students in tabular form. Where grade is assigned as per given rule
For percentage >=75 grade ‘A’, for percentage between 60 to 75 grade ‘B’, for percentage between 40 to 59 grade
‘C’, for percentage below 40 grade ‘D’.
Display the name of the student who scored highest percentage
35) Write a program to initialize an array of 5 names and initialize another array with their respective telephone
numbers. Search for a name input by the user, in the list. If found, display “Search Successful” and print the name
along with the telephone number, otherwise display “Search unsuccessful. Name not listed”.
36. WAP to input marks out of 100 of 100 students. Now form four arrays first[ ], second[ ],
third[ ] and fourth[ ]. Now transfer the marks in given arrays depending upon following condition.
MARKS ARRAY
Above 59 First
Between 45 and 59 Second
Between 33 and 44 Third
Below 33 Fourth