0% found this document useful (0 votes)
2 views5 pages

Assignment 3

The document outlines a series of programming tasks involving arrays, each requiring a separate function to implement. Tasks include printing elements in original and reverse order, finding indices of target values, solving the two-sum problem, printing in zig-zag order, finding the maximum element, and counting even numbers. Clear instructions emphasize meaningful naming, commenting, and a deadline for submission, with encouragement to attempt every problem.

Uploaded by

tlhnizam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views5 pages

Assignment 3

The document outlines a series of programming tasks involving arrays, each requiring a separate function to implement. Tasks include printing elements in original and reverse order, finding indices of target values, solving the two-sum problem, printing in zig-zag order, finding the maximum element, and counting even numbers. Clear instructions emphasize meaningful naming, commenting, and a deadline for submission, with encouragement to attempt every problem.

Uploaded by

tlhnizam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Array Assignment – Logic Building Basics

General Instructions
1. Write a separate function/method for every task listed below.

2. Give variables, parameters, and functions meaningful names—avoid single-letter names


like a, b, c unless it’s a loop counter that is genuinely clear.

3. Add 1–2 short comments inside each function to describe the main logic.

4. Deadline: I will check your assignments Monday.

1. Anyone who has not completed the work will be marked absent immediately—no
excuses.

5. Attempt every problem. Even a partial solution is better than leaving it blank.

6. If you have any doubts, DM me anytime. I won’t judge you, but remember—I can only help if
you also put in effort.

1. Print all elements in the original order

🔹 Problem Statement:
You are given an array of elements. Your task is to print each element one by one, from the rst to
the last. The order in which elements are printed must exactly match the order in which they appear
in the array. You must not skip or rearrange any elements.

📌 Examples:

• Input: [3, 5, 7]
Output: 3 5 7
Explanation: The array has three elements, and they are printed in the same order as they
appear.

• Input: ["apple", "banana", "cherry"]


Output: apple banana cherry
Explanation: The elements are strings, but the logic is the same—just print from rst to
last.

• Input: [42]
Output: 42
Explanation: The array contains only one element, so only that element is printed.

2. Print all elements in reverse order


fi
fi
🔹 Problem Statement:
You are given an array. You need to display its elements starting from the end and moving towards
the beginning. Your goal is to print all elements in reverse of their original appearance.

📌 Examples:

• Input: [1, 2, 3, 4]
Output: 4 3 2 1
Explanation: The original order is reversed. You start from the last element and go
backwards.

• Input: ["red", "green", "blue"]


Output: blue green red
Explanation: The last color comes rst now, and we go back to the rst.

• Input: [99]
Output: 99
Explanation: Only one element—so the reverse is the same.

3. Find the index of the rst occurrence of a target

🔹 Problem Statement:
You are given an array of numbers and a target value. Your task is to nd where in the array the
target value appears for the rst time. You should return the index of this rst appearance. If the
target is not present in the array at all, indicate that appropriately.

📌 Examples:

• Input: Array: [2, 4, 6, 4], Target: 4


Output: 1
Explanation: The rst time the number 4 appears is at index 1 (indexing starts from 0).

• Input: Array: [1, 3, 5], Target: 5


Output: 2
Explanation: The target 5 appears once, and it's at index 2.

• Input: Array: [7, 8, 9], Target: 6


Output: -1
Explanation: The target 6 does not appear in the array at all.

4. Find the index of the last occurrence of a target

🔹 Problem Statement:
You are given an array and a target value. Your goal is to nd the last position in the array where
this target appears. If the target value doesn’t appear at all, you should indicate that clearly.
fi
fi
fi
fi
fi
fi
fi
fi
📌 Examples:

• Input: Array: [4, 5, 6, 5, 4], Target: 5


Output: 3
Explanation: The number 5 appears twice, and the last time it appears is at index 3.

• Input: Array: [10, 20, 30], Target: 10


Output: 0
Explanation: 10 appears only once, and it's the rst and last occurrence.

• Input: Array: [1, 2, 3], Target: 7


Output: -1
Explanation: Since 7 is not present, there's no index to return.

5. Two-Sum Problem

🔹 Problem Statement:
You are given an array of numbers and a target value. You need to nd any two numbers in the array
that add up exactlyto the target value. The numbers must be different elements from the array. Your
nal output should be the indices of these two numbers. If no such pair exists, make sure to return
something that makes this clear.

📌 Examples:

• Input: Array: [2, 7, 11, 15], Target: 9


Output: 0, 1
Explanation: 2 (index 0) + 7 (index 1) = 9.

• Input: Array: [3, 3], Target: 6


Output: 0, 1
Explanation: Both 3s add up to 6; they are at different positions.

• Input: Array: [1, 2, 3], Target: 7


Output: -1, -1
Explanation: No two numbers add up to 7, so we return a “not found” indicator.

6. Print the array in zig-zag order

🔹 Problem Statement:
You are given an array of elements. You must print them in a zig-zag manner: rst print the rst
element, then the last, then the second element, then the second last, and so on. This pattern
continues inward until all elements are printed.

📌 Examples:
fi
fi
fi
fi
fi
• Input: [1, 2, 3, 4, 5]
Output: 1 5 2 4 3
Explanation: Start with the outermost pair (1 and 5), move inward (2 and 4), and nally
print the center (3).

• Input: [10, 20, 30, 40]


Output: 10 40 20 30
Explanation: Go from the ends toward the center: 10 → 40 → 20 → 30.

• Input: [100]
Output: 100
Explanation: Only one element—so nothing else to alternate with.

7. Find the maximum element and its index

🔹 Problem Statement:
Given an array of numbers, nd the largest number in the array and also tell its position (index). If
the largest number appears multiple times, just return the position where it rst appears.

📌 Examples:

• Input: [3, 7, 2, 9, 5]
Output: 9 at index 3
Explanation: 9 is the largest number and it appears at index 3.

• Input: [5, 5, 5]
Output: 5 at index 0
Explanation: All elements are equal, so we return the rst occurrence.

• Input: [-1, -4, -3]


Output: -1 at index 0
Explanation: Even though all numbers are negative, -1 is the largest among them.

8. Count how many even numbers are present

🔹 Problem Statement:
You are given an array of integers. Count how many of these numbers are even (i.e., divisible by 2
without any remainder). Return the count of such numbers.

📌 Examples:

• Input: [2, 3, 4, 6, 7]
Output: 3
Explanation: 2, 4, and 6 are even numbers.
fi
fi
fi
fi
• Input: [1, 5, 9]
Output: 0
Explanation: No even numbers are present.

• Input: [0, 8, 11, 24]


Output: 3
Explanation: 0, 8, and 24 are even numbers.

You might also like