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

Java 1D Array

1D java array

Uploaded by

rachitvaishnav
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)
7 views11 pages

Java 1D Array

1D java array

Uploaded by

rachitvaishnav
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/ 11

Real time problem

A teacher wants to compute average Input : stu_1_mark, stu_2_mark,


stu_3_mark, stu_4_mark, stu_5_mark
marks in her history classes
avg = (stu_1_mark + stu_2_mark +
In her class , students count is 5 stu_3_mark + stu_4_mark +
stu_5_mark) / 5

print avg
Real time problem
A teacher wants to compute average Input: stu_1_mark, stu_2_mark, …,
stu_10_mark
marks in her history classes
avg = (stu_1_mark + stu_2_mark + … +
In her class , students count is 10 stu_10_mark) / 10

print avg
Real time problem
A teacher wants to compute average Input: stu_1_mark, stu_2_mark, …,
…….stu_120_mark
marks in her history classes
avg = (stu_1_mark + stu_2_mark + … +
In her class , students count is 120 stu_120_mark) / 120

print avg
Design for large inputs
1. Too many variables. • Goals
– Variable count != number of
2. Lets find a solution inputs
• One variable for entire set

– Still identify inputs individually


• Like first number + second
number + …
Design for large inputs
• Goals • Solution:
– Variable count != number of – marks[] = {21,24,25,28,32}, size = 5;
inputs
• One variable for entire set – marks
• holds the list of numbers
– Still identify inputs individually • Type – Array: Indicated by []
• Like first number + second
number + … – size
• Number of elements in list marks
Design for large inputs
• Solution: How to access elements in the list???
– marks[] = {21,24,25,28,32}, size = 5;
First element - marks[0]
– marks
• holds the list of numbers Second element - marks[1]
• Type – Array: Indicated by []
Last element ?
marks[4]
– size
• Number of elements in list marks
Memory Allocation 1000 - 1003 01 marks[0]
int marks = new int[4];

Syntax: 1004 - 1007 02 marks[1]


var-name = new type[size];
1008 - 1011 30 marks[2]
marks[0] = 1
marks[1] = 2 1012 - 1015 04 marks[3]
marks[2] = 3
marks[3] = 4 1016 - 1019

How to access ith element? ……


Index = i - 1
Access it as marks[i - 1] 2000 - 2003
Invalid indexing like marks[10] or marks[5]
returns undefined value.
2000 - 2003 1000 a

Array
import java.util.Scanner; 1000 - 1003 marks[0]
public class Main
{ 1004 - 1007 marks[1]
public static void main(String[] args)
1008 - 1011 marks[2]
{
int i,number;
Scanner s = new Scanner(System.in); 1012 - 1015 marks[3]
number= s.nextInt(); // 4
int array[] = new int[number]; 1016 - 1019

……

2000 - 2003
2000 - 2003 1000 a

Array
for(i = 0; i < number; i++) 1000 - 1003 1 marks[0]
{
array[i]=s.nextInt(); 1004 - 1007 2 marks[1]
}
1008 - 1011 3 marks[2]
for(i = 0; i < number; i++)
{
1012 - 1015 4 marks[3]
System.out.println(array[i]);
}
1016 - 1019
}
} ……

2000 - 2003
THANK YOU

You might also like