Arrays
 What are arrays?
 Types of Arrays
 Single dimensional arrays
 Searching an element
 Linear Search
 Binary Search
 Sorting an array
 --Bubble Sort
 ---Selection Sort
 Double dimensional arrays
 --Add/subtract row/column wise
 --Adding diagonal elements
With and without an array
• Think of an array as a "box" drawn in memory with a series of subdivisions,
called elements, to store the data. The box below could represent an array
of 10 integers.
• While this array contains only 10 elements, arrays are traditionally used to
handle large amounts of data. It is important to remember that all of the
elements in an array must contain the same type of data.
• Let's consider a programming situation "with" and "without" the use of an
array:
7 6 5 8 3 9 2 6 10 2
• An array is a container object that holds a fixed number of
values of a single type.
• The length of an array is established when the array is
created. After creation, its length is fixed.
• Arrays are used to store multiple values in a single
variable, instead of declaring separate variables for each
value. To declare an array, define the variable type with
square brackets:
• String[] cars;
• Suppose you have to calculate the average marks of 4
subjects:
• Suppose you have to find the average marks obtained by
50 students:
• Ans: Loops
• But
Arrays.pptx
DECLARING ARRAYS
Declaration of an integer array of
10 elements
Declare a string array of 4 elements:
double a[]= new double[4];
What is the length of the array?
4.1 2.2 2.25 1.89
What is the value of a[2]?
How will you access the 2nd
element?
What is the output of the
following?
S.o.pln(a[4]);
Arrays.pptx
• WAP to initialize a String array with 5 names and display
the 1st and last name.
• WAP to accept marks in 10 subjects and display them.
• WAP to accept the names & marks of 6 students in your
class as floating point decimals. Display the name and
marks of the student who has got the highest marks.
Searching an Element in an array
• Binary Search-
• Linear Search- It is the simplest way of searching an
element in an array.
• Algorithm:
Step 1: Traverse the array
Step 2: Match the key element with array element
Step 3: If key element is found, return the index position
of the array element
Step 4: If key element is not found, return -1
Linear Search
Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[].
Examples :
Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170}
x = 110;
Output : 6
Element x is present at index 6
Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170}
x = 175;
Output : -1
Element x is not present in arr[].
• A simple approach is to do linear
search, i.e
• Start from the leftmost element of
arr[] and one by one compare x
with each element of arr[]
• If x matches with an element, return
the index.
• If x doesn’t match with any of
elements, return -1.
import java.util.Scanner;
class LinearSearch
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
System.out.println("Enter " + n + " integers");
for (c = 0; c < n; c++)
array[c] = in.nextInt();
System.out.println("Enter value to find");
search = in.nextInt();
for (c = 0; c < n; c++)
{
if (array[c] == search) /* Searching element is present */ { System.out.println(search
+ " is present at location " + (c + 1) + ".");
break;
}
}
if (c == n) /* Element to search isn't present */
System.out.println(search + " isn't present in array.");
}
}
• WAP to accept the names of the seven wonders of the
world and its ranking in an array. Accept a name from the
user and check whether the name of the wonder is
present or not. If it is present display- for e.g.-
”Great_Pyramid_at_Giza is at 1st position”.
• Display appropriate messages if the name is not present.
Binary Search
• Binary search is used to
search a key element from
multiple elements. Binary
search is faster than linear
search.
• In case of binary search,
array elements must be in
ascending order.
• Divide the array
Arrays.pptx
Arrays.pptx
Example of Binary Search
Consider a sorted array of 10 integers as given below,
import java.util.Scanner;
class BinarySearch
{
public static void main(String args[])
{
int c, n, search, array[];
Scanner in = new Scanner(System.in);
System.out.println("Enter number of elements");
n = in.nextInt();
array = new int[n];
Arrays.pptx
import java.util.*;
class StringEven
{
public static void main()
{
Scanner sc =new Scanner(System.in);
String s[]=new String[10];
System.out.println("Enter the Strings");
for(int i=0;i<s.length;i++)
{
s[i]=sc.next();
}
System.out.println("Even letter Strings");
for(int i=0;i<10;i++)
{
if((s[i].length())%2==0)
System.out.println(s[i]);
}
}}
Arrays.pptx
Arrays.pptx
6 2 4 9 1 3
no. of elements =6
Arrays.pptx
• Question 1
• Write a program in Java to store 20 numbers (even and
odd numbers) in a Single Dimensional Array (SDA).
Calculate and display the sum of all even numbers and all
odd numbers separately.
• Question 2
• Write a program in Java to store 20 temperatures in °F
in a Single Dimensional Array (SDA) and display all the
temperatures after converting them into °C.
• Hint: (c/5) = (f - 32) / 9
• Question 3
• Write a program in Java to store 10 numbers (including
positive and negative numbers) in a Single Dimensional
Array (SDA). Display all the negative numbers followed by
the positive numbers without changing the order of the
numbers.
• Sample Input:
• n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
• 15 21 -32 -41 54 61 71 -19 -44 52
• Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52
Question 4
• Write a program to accept the year of graduation from
school as an integer value from the user. Using the linear
search technique on the sorted array of integers given
below, output the message "Record exists" if the value
input is located in the array. If not, output the message
"Record does not exist".
• Sample Input:
• n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9]
• 1982 198719931996199920032006200720092010
Question 5
• Write a program to read the data, calculate and display
the following:
• (a) Average marks obtained by each student.
• (b) Print the roll number and the average marks of the
students whose average is above. 80.
• (c) Print the roll number and the average marks of the
students whose average is below 40.

More Related Content

PPT
Data Structures- Part3 arrays and searching algorithms
PDF
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
PDF
03 Linear Arrays Memory Representations .pdf
PPT
1 D Arrays in C++
PPT
4.1 sequentioal search
PPTX
Data Structures - Array presentation .pptx
PPTX
07+08slide.pptx
PPTX
ARRAY PPT.pptx for mca finals placement
Data Structures- Part3 arrays and searching algorithms
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
03 Linear Arrays Memory Representations .pdf
1 D Arrays in C++
4.1 sequentioal search
Data Structures - Array presentation .pptx
07+08slide.pptx
ARRAY PPT.pptx for mca finals placement

Similar to Arrays.pptx (20)

PPTX
ARRAY in python and c with examples .pptx
PPTX
Lecture 4_Linear & Binary search from data structure and algorithm
PPT
05slide_arrays_creation_searching_sorting.ppt
PPT
Array
PPT
9781111530532 ppt ch09
PDF
An Introduction to Programming in Java: Arrays
PPTX
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
PPTX
Array 1D.................................pptx
PDF
Starting Out with Java Early Objects 6th Edition Gaddis Test Bank
PPT
PPTX
6_Array.pptx
PPTX
Arrays in C.pptx
PDF
5 structured programming
PPTX
Module_3_Arrays - Updated.pptx............
PPT
arrays1.ppt python programme arrays insertion
PDF
Data Structures
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
DOCX
The Java Learning Kit Chapter 6 – Arrays Copyrigh.docx
DOCX
Array assignment
PPTX
Chapter 7.1
ARRAY in python and c with examples .pptx
Lecture 4_Linear & Binary search from data structure and algorithm
05slide_arrays_creation_searching_sorting.ppt
Array
9781111530532 ppt ch09
An Introduction to Programming in Java: Arrays
ADVANCED DATA STRUCTURES AND ALGORITHMS.pptx
Array 1D.................................pptx
Starting Out with Java Early Objects 6th Edition Gaddis Test Bank
6_Array.pptx
Arrays in C.pptx
5 structured programming
Module_3_Arrays - Updated.pptx............
arrays1.ppt python programme arrays insertion
Data Structures
358 33 powerpoint-slides_5-arrays_chapter-5
The Java Learning Kit Chapter 6 – Arrays Copyrigh.docx
Array assignment
Chapter 7.1
Ad

More from Epsiba1 (16)

PPTX
Intro to Access 2016 class 8 2024-25.pptx
PPTX
Internet in education, social media.pptx
PPTX
chapter 2_Matter_Physics_icse_soild_liquid.pptx
PPTX
Area and perimeter for class 8 -mensuration.pptx
PPTX
Uses Of Computer in schools offices public places like station, airport.pptx
PPTX
App development for class 8 creating apps open source mit .pptx
PPTX
class 8 presentation on computer virus-macro.pptx
PPTX
Cloud computing.pptxcloud, technology, apps, protocols, google drive, drop bo...
PPTX
Surface area and volume.pptx
PPTX
constructors.pptx
PPTX
Assertive Nationalists.pptx
PPTX
class as the basis.pptx
PPTX
Linear Equations and Inequations in one variable.pptx
PPTX
Cube and Cube root.pptx
PPTX
Circle.pptx
PPTX
Gideon Story.pptx
Intro to Access 2016 class 8 2024-25.pptx
Internet in education, social media.pptx
chapter 2_Matter_Physics_icse_soild_liquid.pptx
Area and perimeter for class 8 -mensuration.pptx
Uses Of Computer in schools offices public places like station, airport.pptx
App development for class 8 creating apps open source mit .pptx
class 8 presentation on computer virus-macro.pptx
Cloud computing.pptxcloud, technology, apps, protocols, google drive, drop bo...
Surface area and volume.pptx
constructors.pptx
Assertive Nationalists.pptx
class as the basis.pptx
Linear Equations and Inequations in one variable.pptx
Cube and Cube root.pptx
Circle.pptx
Gideon Story.pptx
Ad

Recently uploaded (20)

PPTX
Introduction to pro and eukaryotes and differences.pptx
PDF
Practical Manual AGRO-233 Principles and Practices of Natural Farming
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PDF
Hazard Identification & Risk Assessment .pdf
PDF
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
PPTX
Unit 4 Computer Architecture Multicore Processor.pptx
PPTX
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
PPTX
Share_Module_2_Power_conflict_and_negotiation.pptx
PDF
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
PPTX
Computer Architecture Input Output Memory.pptx
PPTX
20th Century Theater, Methods, History.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Complications of Minimal Access-Surgery.pdf
DOCX
Cambridge-Practice-Tests-for-IELTS-12.docx
PDF
International_Financial_Reporting_Standa.pdf
PPTX
History, Philosophy and sociology of education (1).pptx
PPTX
A powerpoint presentation on the Revised K-10 Science Shaping Paper
PDF
IGGE1 Understanding the Self1234567891011
Introduction to pro and eukaryotes and differences.pptx
Practical Manual AGRO-233 Principles and Practices of Natural Farming
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
Hazard Identification & Risk Assessment .pdf
احياء السادس العلمي - الفصل الثالث (التكاثر) منهج متميزين/كلية بغداد/موهوبين
Unit 4 Computer Architecture Multicore Processor.pptx
CHAPTER IV. MAN AND BIOSPHERE AND ITS TOTALITY.pptx
Share_Module_2_Power_conflict_and_negotiation.pptx
BP 704 T. NOVEL DRUG DELIVERY SYSTEMS (UNIT 1)
Computer Architecture Input Output Memory.pptx
20th Century Theater, Methods, History.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
David L Page_DCI Research Study Journey_how Methodology can inform one's prac...
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Complications of Minimal Access-Surgery.pdf
Cambridge-Practice-Tests-for-IELTS-12.docx
International_Financial_Reporting_Standa.pdf
History, Philosophy and sociology of education (1).pptx
A powerpoint presentation on the Revised K-10 Science Shaping Paper
IGGE1 Understanding the Self1234567891011

Arrays.pptx

  • 1. Arrays  What are arrays?  Types of Arrays  Single dimensional arrays  Searching an element  Linear Search  Binary Search  Sorting an array  --Bubble Sort  ---Selection Sort  Double dimensional arrays  --Add/subtract row/column wise  --Adding diagonal elements
  • 2. With and without an array • Think of an array as a "box" drawn in memory with a series of subdivisions, called elements, to store the data. The box below could represent an array of 10 integers. • While this array contains only 10 elements, arrays are traditionally used to handle large amounts of data. It is important to remember that all of the elements in an array must contain the same type of data. • Let's consider a programming situation "with" and "without" the use of an array: 7 6 5 8 3 9 2 6 10 2
  • 3. • An array is a container object that holds a fixed number of values of a single type. • The length of an array is established when the array is created. After creation, its length is fixed. • Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each value. To declare an array, define the variable type with square brackets: • String[] cars;
  • 4. • Suppose you have to calculate the average marks of 4 subjects: • Suppose you have to find the average marks obtained by 50 students: • Ans: Loops • But
  • 6. DECLARING ARRAYS Declaration of an integer array of 10 elements Declare a string array of 4 elements:
  • 7. double a[]= new double[4]; What is the length of the array? 4.1 2.2 2.25 1.89 What is the value of a[2]? How will you access the 2nd element? What is the output of the following? S.o.pln(a[4]);
  • 9. • WAP to initialize a String array with 5 names and display the 1st and last name. • WAP to accept marks in 10 subjects and display them.
  • 10. • WAP to accept the names & marks of 6 students in your class as floating point decimals. Display the name and marks of the student who has got the highest marks.
  • 11. Searching an Element in an array • Binary Search- • Linear Search- It is the simplest way of searching an element in an array. • Algorithm: Step 1: Traverse the array Step 2: Match the key element with array element Step 3: If key element is found, return the index position of the array element Step 4: If key element is not found, return -1
  • 12. Linear Search Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[]. Examples : Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170} x = 110; Output : 6 Element x is present at index 6 Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170} x = 175; Output : -1 Element x is not present in arr[].
  • 13. • A simple approach is to do linear search, i.e • Start from the leftmost element of arr[] and one by one compare x with each element of arr[] • If x matches with an element, return the index. • If x doesn’t match with any of elements, return -1.
  • 14. import java.util.Scanner; class LinearSearch { public static void main(String args[]) { int c, n, search, array[]; Scanner in = new Scanner(System.in); System.out.println("Enter number of elements"); n = in.nextInt(); array = new int[n];
  • 15. System.out.println("Enter " + n + " integers"); for (c = 0; c < n; c++) array[c] = in.nextInt(); System.out.println("Enter value to find"); search = in.nextInt(); for (c = 0; c < n; c++) { if (array[c] == search) /* Searching element is present */ { System.out.println(search + " is present at location " + (c + 1) + "."); break; } } if (c == n) /* Element to search isn't present */ System.out.println(search + " isn't present in array."); } }
  • 16. • WAP to accept the names of the seven wonders of the world and its ranking in an array. Accept a name from the user and check whether the name of the wonder is present or not. If it is present display- for e.g.- ”Great_Pyramid_at_Giza is at 1st position”. • Display appropriate messages if the name is not present.
  • 17. Binary Search • Binary search is used to search a key element from multiple elements. Binary search is faster than linear search. • In case of binary search, array elements must be in ascending order.
  • 18. • Divide the array
  • 21. Example of Binary Search Consider a sorted array of 10 integers as given below,
  • 22. import java.util.Scanner; class BinarySearch { public static void main(String args[]) { int c, n, search, array[]; Scanner in = new Scanner(System.in); System.out.println("Enter number of elements"); n = in.nextInt(); array = new int[n];
  • 24. import java.util.*; class StringEven { public static void main() { Scanner sc =new Scanner(System.in); String s[]=new String[10]; System.out.println("Enter the Strings"); for(int i=0;i<s.length;i++) { s[i]=sc.next(); } System.out.println("Even letter Strings"); for(int i=0;i<10;i++) { if((s[i].length())%2==0) System.out.println(s[i]); } }}
  • 27. 6 2 4 9 1 3 no. of elements =6
  • 29. • Question 1 • Write a program in Java to store 20 numbers (even and odd numbers) in a Single Dimensional Array (SDA). Calculate and display the sum of all even numbers and all odd numbers separately. • Question 2 • Write a program in Java to store 20 temperatures in °F in a Single Dimensional Array (SDA) and display all the temperatures after converting them into °C. • Hint: (c/5) = (f - 32) / 9
  • 30. • Question 3 • Write a program in Java to store 10 numbers (including positive and negative numbers) in a Single Dimensional Array (SDA). Display all the negative numbers followed by the positive numbers without changing the order of the numbers. • Sample Input: • n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] • 15 21 -32 -41 54 61 71 -19 -44 52 • Sample Output: -32, -41, -19, 44, 15, 21, 54, 61, 71, 52
  • 31. Question 4 • Write a program to accept the year of graduation from school as an integer value from the user. Using the linear search technique on the sorted array of integers given below, output the message "Record exists" if the value input is located in the array. If not, output the message "Record does not exist". • Sample Input: • n[0] n[1] n[2] n[3] n[4] n[5] n[6] n[7] n[8] n[9] • 1982 198719931996199920032006200720092010
  • 32. Question 5 • Write a program to read the data, calculate and display the following: • (a) Average marks obtained by each student. • (b) Print the roll number and the average marks of the students whose average is above. 80. • (c) Print the roll number and the average marks of the students whose average is below 40.