Simple Arrays
Short introduction to processing data
Simple Arrays Short introduction to processing data Recording and - - PowerPoint PPT Presentation
Simple Arrays Short introduction to processing data Recording and storing sound Analog http://www.soundsetal.com/blog-how-do-vinyl-records-work/ Playback of stored analog sound Power Amplification What about digital? Sampling: Audio CD
Short introduction to processing data
http://www.soundsetal.com/blog-how-do-vinyl-records-work/
Power Amplification
1 2 3 4 5 6 7 8
5 15 32 38 42 41 40 37 35
9
Array
27
…
…
Sampling: Audio CD quality 44100 samples/second Quantization: representing real values with fixed precision: Audio CD quality 216 possible distinct values
In computer memory, represented as array of data points/measurements
! The order of data is as important as the values themselves
type[] name = new type[n];
int[] intArray = new int[10];
1 2 3 4 5 6 7 8 9
intArray
An array is ordered.(considering indexes not the contents)
1.
An array is homogeneous.
2.
1 2 3 4 5 6 7 8 9
intArray
intArray[0]
intArray[9] = 42;
42
for (int i = 0; i < intArray.length; i++) { intArray[i] = -1; }
1 using the following for loop:
for (int i = 0; i < array.length; i++) { Operations involving the ith element of the array }
private private final final int int CAR_WIDTH CAR_WIDTH=75; =75; private private final final int int X_OFFSET X_OFFSET=50; =50; public public void void run(){ run(){ int int numCars numCars=10; =10; double double carHeight carHeight=getHeight getHeight()/(2* ()/(2*numCars numCars); ); GRect[] cars=new new GRect GRect[numCars numCars]; ]; for for(int int i=0; =0;i<numCars numCars;i++){ ++){ cars[i]=new new GRect GRect(CAR_WIDTH CAR_WIDTH,carHeight carHeight); ); cars[i].setColor(rgen.nextColor()); cars[i].setFilled(true true); ); add(cars[i],X_OFFSET,(2*i)*carHeight); } }
type[] name = {elements};
powersOfTen to the values 100, 101, 102, 103, and 104:
int[] powersOfTen = { 1, 10, 100, 1000, 10000 };
This declaration creates an integer array of length 5 and initializes the elements as specified.
public public void void run() { run() { int int numValues numValues=readInt readInt("Number of values to be entered: " "Number of values to be entered: "); ); /*Creating the array*/ int int[] [] values values=new new int int[numValues numValues]; ]; for for(int int i=0; =0;i<values values.length length;i++) { ++) { values[i]=readInt("Specify input for index "+i+" :"); } println("Max: "+findMax(values)); println("Min: "+findMin(values)); println("Mean: "+findMean(values)); } private private int int findMax findMax(int int[] [] inputArray inputArray) { ) { int int max max=0; =0; /*Implement the method*/ return return max max; }
public public class class MethodsReview MethodsReview extends extends ConsoleProgram ConsoleProgram{ public public void void run() { run() { printInfo(); } private private void void printInfo printInfo( ) { ( ) { println("This method prints some instructions"); println("1-Don't use arguments "); println("..."); } }
You should call the method to make use of it This method does not take any input This method does not return any output
public public void void run() { run() { int int x = sum2ints(5,6); = sum2ints(5,6); } private private int int sum2ints( sum2ints(int int x,int int y) { ) { int int sum sum = = x + + y; return return sum sum; }
You should call the method with two int inputs This method takes two int inputs This method returns an int output
public public class class MethodsReview MethodsReview extends extends ConsoleProgram ConsoleProgram{ private private int int var1 var1=0; =0; public public void void run() { run() { someMethod(); println(var1); } private private void void someMethod someMethod(){ (){ int int var1 var1 = 5; = 5; } }
public public class class MethodsReview MethodsReview extends extends ConsoleProgram ConsoleProgram{ private private int int var1 var1=0; =0; public public void void run() { run() { someMethod(); println(var1); } private private void void someMethod someMethod(){ (){ var1 = 5; = 5; } }
public public class class MethodsReview MethodsReview extends extends ConsoleProgram ConsoleProgram{ private private int int var1 var1=0; =0; public public void void run() { run() { someMethod(); println(var1); } private private int int someMethod someMethod(){ (){ int int var1 = 5; var1 = 5; return return 10; 10; } }
public public class class MethodsReview MethodsReview extends extends ConsoleProgram ConsoleProgram{ private private int int var1 var1=0; =0; public public void void run() { run() { var1=someMethod(); println(var1); } private private int int someMethod someMethod(){ (){ return return 10; 10; } }