Array algorithms: max, count, search, average · خوارزميات المصفوفات: الحد الأقصى، العد، البحث، المتوسط
Four classic array jobs
- Most array work is one of four scans: find the maximum, count matches, search for a value, or take an average.
- Each one is a single
forloop over the array, with a variable that remembers something. - Once you know these four, most array problems are a small change to one of them.
Finding the maximum
- Start by assuming the first item is the biggest:
int best = a[0];. - Then look at the rest. If an item is bigger than
best, it becomes the newbest. - This works for negative numbers too, because you start from a real item, not
0.
Counting with a condition
- A counter starts at
0and adds1each time an item passes a test. - For example, count even numbers by testing
a[i] % 2 == 0inside the loop. - The counter's final value is your answer.
Linear search
- To search, walk the array and compare each item to the target.
- Return the index as soon as you find a match. If the loop ends with no match, return
-1. -1is a common "not found" signal because it is never a valid index.
Average without integer-division bugs
- Add all the items into an
inttotal, then divide byn. - Dividing two
ints drops the fraction, so cast:(double)total / n. - Return a
doubleso the caller gets the exact average.
Common mistakes
- Start a max or min from the first element, then compare the rest.
- Do not read past the end of the array.
Now you try
- Pass the array and its length
n, and pick the right "remember" variable for each job. - Do not write a
main— the checker provides one.
Scanning an array · مسح المصفوفة
One pass keeps a running result (max, sum, count) across the array. · تمريرة واحدة تحافظ على نتيجة متجمعة (الأعلى، المجموع، العدد) عبر المصفوفة.
Complete int max(const int a[], int n) so it returns the largest item (assume n >= 1). Start from a[0] so negatives work. Do not write a main. · أكمل int max(const int a[], int n) لتعيد أكبر عنصر (افترض أن n >= 1). ابدأ من a[0] لضمان عمل القيم السالبة. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int count_even(const int a[], int n) so it returns how many items are even. Use % 2. Do not write a main. · أكمل int count_even(const int a[], int n) لتعيد عدد العناصر الزوجية. استخدم % 2. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int index_of(const int a[], int n, int target) so it returns the index of the first target, or -1 if it is not there. Do not write a main. · أكمل int index_of(const int a[], int n, int target) لتعيد فهرس أول عنصر يساوي target، أو -1 إذا لم يكن موجوداً. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete double average(const int a[], int n) so it returns the average of the items (assume n >= 1). Cast to avoid integer division. Do not write a main. · أكمل double average(const int a[], int n) لتعيد المتوسط الحسابي للعناصر (افترض أن n >= 1). قم بتحويل النوع لتجنب القسمة الصحيحة. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.