Arrays

Lesson 4 - Arrays

  • arrays

  • math.random

Arrays

Remove Element from an Array in Java | LaptrinhX

these are all the same

  • here we used arr3[i-1] since the first index starts from 0 but are for loops starts from 1

  • int[] arr3 = new int[10]; we must explicitly to define the size to 10 since we can't infer the the size at the beginning

cool way to visualize

Warm Up I ⭐

בהינתן מערך חשב את הסכום של האיבר עם הקודמים לו

ex.

explanation:

simple-array-ex

Warm Up II ⭐

Read n number of values in an array and display it in reverse order:

now copy those values in reverse

עכשיו תעתיקו את המערך הפוך למערך אחר

Find The max, min, avg and 2nd largest

  • עכשיו במקום להכניס n מספרים הוא יגריל n מספרים

  • בנוסף הוא ימצא את המינ' המקסימום וגם השני הגדול ביותר

  • יודפס גם האיבר ממוצע

ex.

solution:

Duplicate elements ⭐⭐⭐

C Exercises: Count a total number of duplicate elements in an array

ספרו כמה פעמים שי ערכים שחוזרים על עצמם

Watch out not to double count duplicates

Note: we could also write !marked[j] instead of marked[j] == false

Simpler version

This also works and we wont count twice (or more) since we break before reaching the other doubles. By breaking we prevented ourselves from counting more than we should

version 2.1

now print the duplicate values as well

ex.

Sorting array - Bubble Sort ⭐⭐

https://youtu.be/xli_FI7CuzA - link to youtube explanation

Bubble Sort in C Example

Time Complexity of Bubble sort is: O(n^2) so it isn't often used

use: System.out.print((i%15==0 && i!=0 ?"\n" : "") + "| " + arr[i] + " "); for a nicer print

Adding a number to array

array-insertion

Remove Elements from array

הדפסת משולשים

version 2

If we have time

check if an array can be splitted such that the sum of left side of the splitting is equal to the sum of the right side

Last updated

Was this helpful?