๐Ÿ“š

All Subjects

ย >ย 

๐Ÿ’ปย 

AP Comp Sci A

ย >ย 

โŒš๏ธ

Unit 6

6.4 Developing Algorithms Using Arrays

5 min readโ€ขnovember 16, 2020

peter-cao

Peter Cao

caroline49234295

Caroline Koffke


AP Computer Science Aย ๐Ÿ’ป

Bookmarkedย 2.4kย โ€ขย 93ย resources
See Units

Topic 6.4

Using the traversals that we have learned in the previous two topics, we can make many useful algorithms. Here, we will have a snippet for each algorithm you are expected to know with annotations for each.
Finding the Minimum and Maximum
/** Finds the maximum */ public static int maximum(int[] array) { int maxValue = array[0]; for (int number: array) { if (number > maxValue) { //if new max value found, replace current maxValue maxValue = number; } } return maxValue; } /** Finds the minimum */ public static int minimum(int[] array) { int minValue = array[0]; for (int number: array) { if (number < minValue) { //if new min value found, replace current minValue minValue = number; } } return minValue; }
Initializing the maxValue and minValue to 0 is a common mistake. If all the values in the array are positive, it would incorrectly keep minValue at 0 (all the values are greater than 0, leaving 0 as the minimum). If all the values in the array are negative, it would incorrectly keep maxValue at 0 (all the values are less than 0, leaving 0 as the maximum). To counter these errors, initialize these to the first value in the array.
Finding a Sum
/** Sums up all elements in the array */ public static int sum(int[] array) { int sum = 0; for (int number: array) { sum += number; //adds every element to sum } return sum; }
Finding a Mean
/** Finds the mean/average of the array */ public static int mean(int[] array) { int sum = sum(array); // find the sum of the array, can be replaced with sum return (double) sum / (array.length); //algorithm above } // then divide by number of items
Finding a Mode
/** Finds the mode of an array **Prerequisite:** The array must have a mode */ public static int mode(int[] array) { int mostCommon = 0; int mostCommonFrequency = 0; for (int i = 0; i < array.length - 1; i++) { //traverse through the array int currentFrequency = 1; for (int j = i + 1; j < array.length; j++) { //traverse through rest of array if (array[j] == array[i]) { // if any element matches current element currentFrequency++; // being checked, add 1 to frequency } } if (currentFrequency > mostCommonFrequency) { mostCommon = array[i]; // replaces current mode if new most common element mostCommonFrequency = currentFrequency; } } return mostCommon; // can also be modified to return the frequency }
Determining If All Values Have a Certain Property
/** Determines whether all values are even */ public static boolean isEven(int[] array) { //Assume all values are positive first for (int number: array) { if (number % 2 == 1) { //If there is one value that is not positive, return false return false; } } return true; //No odd numbers were found }
Accessing All Consecutive Pairs/Triplets/Sequences of Length n of Elements
/** Returns all consecutive sequences of length n in the array */ public static void returnAllConsecutiveSequences(int[] array, int length) { for (int i = 0; i <= array.length - length; i++) { for (int j = 0; j < length; j++) { System.out.print(array[i+j] + " "); //2 loops, one to get the starting number } //the other to go through the sequences System.out.println(); } }
Checking if There are Duplicate Elements
/** Checks to see if there are duplicate elements */ public static boolean duplicates(int[] array) { for (int i = 0; i < array.length - 1; i++) { //traverse through the array for (int j = i + 1; j < array.length; j++) { //traverse through rest of array if (array[j] == array[i]) { // if any element matches current element return true; // being checked, return true } } } return false; // if this point reached, no duplicates found }
Determining How Many Elements Fit a Criteria
/** Returns how many even numbers there are */ public static int evenFrequency(int[] array) { int numberEven = 0; for (int number: array) { if (number % 2 == 0) { numberEven++; // increments every time an even integer is found } } return numberEven; }
Shifting Elements One Index Left
/** Shifts Elements One Index to the Left */ public static int[] shiftLeft(int[] array) { int firstItem = array[0] for (int i = 0; i < array.length - 1; i++) { array[i] = array[i+1]; // Does the shifting } array[array.length - 1] = firstItem; return array; }
Shifting Elements One Index Right
/** Shifts Elements One Index to the Right */ public static int[] shiftRight(int[] array) { int lastItem = array[array.length - 1] for (int i = array.length - 1; i > 0; i--) { array[i] = array[i-1]; // Does the shifting } array[0] = lastItem; return array; }
Reversing an Array
/** Reverses the array */ public static int[] reverse(int[] array) { int[] newArray = new int[array.length]; for (int i = 1; i <= array.length; i++) { newArray[i] = array[array.length - i]; // places the items in the new array in // opposite order of the original } return newArray; }

Was this guide helpful?

Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
Hours Logo
Studying with Hours = the ultimate focus mode
Start a free study session
๐Ÿ” Are you ready for college apps?
Take this quiz and find out!
Start Quiz
Browse Study Guides By Unit
๐Ÿ™Exam Reviews
๐Ÿ–ฑUnit 10: Recursion
โž•Unit 1: Primitive Types
๐Ÿ“ฑUnit 2: Using Objects
๐Ÿ–ฅUnit 3: Boolean Expressions and if Statements
๐Ÿ•นUnit 4: Iteration
โš™๏ธUnit 5: Writing Classes
โŒš๏ธUnit 6: Array
๐Ÿ’พUnit 7: ArrayList
๐Ÿ’ปUnit 8: 2D Array
๐Ÿ–ฒUnit 9: Inheritance
FREE AP comp sci a Survival Pack + Cram Chart PDF
Sign up now for instant access to 2 amazing downloads to help you get a 5
Join us on Discord
Thousands of students are studying with us for the AP Computer Science A exam.
join now
๐Ÿ’ช๐Ÿฝ Are you ready for the Comp Sci exam?
Take this quiz for a progress check on what youโ€™ve learned this year and get a personalized study plan to grab that 5!
START QUIZ
๐Ÿ“ฑ Stressed or struggling and need to talk to someone?
Talk to a trained counselor for free. It's 100% anonymous.
Text FIVEABLE to 741741 to get started.
ยฉ 2021 Fiveable, Inc.