Array algorithms: max, count, search, average · خوارزميات المصفوفات: الحد الأقصى، العد، البحث، المتوسط
Common array jobs
- Some array tasks come up again and again: find the sum, the biggest, the smallest, or count items.
- Each one uses the same idea: start with a guess, then loop and update it.
- These patterns appear all the time on the AP CSA exam.
مهام شائعة للمصفوفات
- تظهر بعض مهام المصفوفات مرارًا وتكرارًا: إيجاد المجموع، أكبر قيمة، أصغر قيمة، أو عدّ العناصر.
- كل منها يستخدم نفس الفكرة: ابدأ بتخمين، ثم حرك الحلقة وتحديثه.
- تظهر هذه الأنماط باستمرار في امتحان AP CSA.
Sum and count
- Keep a running total that starts at 0, and add each value.
- To count items that pass a test, start a counter at 0 and add 1 when the test is true.
- Below we count how many values are even (
v % 2 == 0).
المجموع والعد
- احتفظ بـ مجموع تراكمي يبدأ من 0، وأضف كل قيمة.
- لـ عدّ العناصر التي تمر باختبار، ابدأ عدادًا من 0 وأضف 1 عندما يكون الاختبار صحيحًا.
- في الأسفل نعد كم عدد القيم الزوجية (
v % 2 == 0).
public class Main {
public static void main(String[] args) {
int[] a = {3, 4, 7, 10};
int evens = 0;
for (int v : a) {
if (v % 2 == 0) {
evens = evens + 1;
}
}
System.out.println(evens); // 2
}
}
Find the maximum
- Start by guessing the first value is the biggest:
int max = a[0];. - Loop through the rest. If a value is bigger than
max, make it the newmax. - This works for negative numbers too, because the guess comes from the array itself.
إيجاد القيمة العظمى
- ابدأ بفرض أن القيمة الأولى هي الأكبر:
int max = a[0];. - مرّ على القيم المتبقية. إذا كانت قيمة أكبر من
max، اجعلهاmaxالجديدة. - هذا يعمل مع الأرقام السالبة أيضًا، لأن التخمين يأتي من المصفوفة نفسها.
public class Main {
public static void main(String[] args) {
int[] a = {3, 9, 2, 7};
int max = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
}
}
System.out.println(max); // 9
}
}
Find the minimum
- The minimum uses the same shape — just flip the test to
<. - Start with
int min = a[0];and keep the smallest value you see. - Never start
minat 0; a real value from the array is a safe first guess.
إيجاد القيمة الصغرى
- استخدام القيمة الصغرى يتبع نفس الشكل — فقط عكس الاختبار ليصبح
<. - ابدأ بـ
int min = a[0];واحتفظ بأصغر قيمة تراها. - لا تبدأ
minعند 0؛ قيمة حقيقية من المصفوفة هي تخمين أول آمن.
public class Main {
public static void main(String[] args) {
int[] a = {3, 9, 2, 7};
int min = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] < min) {
min = a[i];
}
}
System.out.println(min); // 2
}
}
Search for a value
- To find where a value is, loop the index and compare each element.
- Return the index as soon as you find it.
- If the loop finishes with no match, return
-1to mean "not found".
البحث عن قيمة
- لإيجاد أين توجد قيمة، مرّر الفهرس وقارن كل عنصر.
- أدرج الفهرس بمجرد العثور عليها.
- إذا انتهى التكرار بدون تطابق، أدرج
-1ليعني "لم يتم العثور".
public class Main {
public static void main(String[] args) {
int[] a = {5, 8, 13, 21};
int target = 13;
int found = -1;
for (int i = 0; i < a.length; i++) {
if (a[i] == target) {
found = i;
break; // stop at the first match
}
}
System.out.println(found); // 2
}
}
Average
- Average = sum divided by count. The count is
a.length. - To get a decimal, divide by
(double) a.length, so the math is not integer division. (double)turns the length into a decimal before the division.
المتوسط الحسابي
- المتوسط = المجموع مقسومًا على العدد. العدد هو
a.length. - للحصول على رقم عشري، اقسم على
(double) a.length، حتى لا تكون العملية حسابية صحيحة. (double)يحول الطول إلى عدد عشري قبل عملية القسمة.
public class Main {
public static void main(String[] args) {
int[] a = {2, 3, 10};
int total = 0;
for (int v : a) {
total = total + v;
}
double avg = total / (double) a.length;
System.out.println(avg); // 5.0
}
}
Common mistakes
- Start a max or min from the first element, then compare the rest.
- Do not read past
a.length - 1.
أخطاء شائعة
- ابدأ بالقيمة العظمى أو الصغرى من العنصر الأول، ثم قارن البقية.
- لا تقرأ ما بعد
a.length - 1.
Now you try
- Each task completes a method the Harness calls with several arrays.
- Reuse the patterns above: a running total, a "best so far", or a counter.
- Press Run to compile, then Check answer.
الآن جرب بنفسك
- كل مهمة تكمل طريقة (method) يستدعيها الحزام (Harness) مع عدة مصفوفات.
- أعد استخدام الأنماط أعلاه: مجموع متراكم، أو "الأفضل حتى الآن"، أو عداد.
- اضغط تشغيل للتجميع، ثم فحص الإجابة.
Scanning an array for the max · مسح المصفوفة للحد الأقصى
One pass keeps a running max, updating it when a bigger value appears. · تمررة واحدة تحافظ على حد أقصى تراكمي، وتحديثه عند ظهور قيمة أكبر.
Complete max(int[] a) so it returns the largest value in the array. You may assume the array has at least one value. It must work with negative numbers too. · أكمل max(int[] a) لتُرجع أكبر قيمة في المصفوفة. يمكنك افتراض أن المصفوفة تحتوي على قيمة واحدة على الأقل. يجب أن تعمل مع الأرقام السالبة أيضًا.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete countEven(int[] a) so it returns how many values are even. A value is even when v % 2 == 0. An empty array returns 0. · أكمل countEven(int[] a) لتُرجع عدد القيم الزوجية. تكون القيمة زوجية عندما v % 2 == 0. المصفوفة الفارغة تُرجع 0.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete indexOf(int[] a, int target). Return the index of the first time target appears. If it is not in the array, return -1. · أكمل indexOf(int[] a, int target). أرجع فهرس أول ظهور لـ target. إذا لم يكن موجودًا في المصفوفة، أرجع -1.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete average(int[] a). Return the average of the values as a double. Divide by (double) a.length so you get a decimal, not integer division. You may assume the array is not empty. · أكمل average(int[] a). أرجع متوسط القيم كـ double. اقسم على (double) a.length لتحصل على رقم عشري، وليس قسمة صحيحة. يمكنك افتراض أن المصفوفة ليست فارغة.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.