Element-wise Sum
Calculate element-wise sum of two arrays.
IntermediatePhase 4 - Level 4: Aggregation & ComparisonExample 7 of 10
element-wise-sum.py
1# Take arrays2n = int(input("Enter array size: "))3arr1 = []4for i in range(n):5 arr1.append(int(input(f"Array1 element {i+1}: ")))67arr2 = []8for i in range(n):9 arr2.append(int(input(f"Array2 element {i+1}: ")))1011# Element-wise sum12sum_arr = []13for i in range(n):14 sum_arr.append(arr1[i] + arr2[i])1516print(f"Sum array: {sum_arr}")
Output
Enter array size: 3 Array1 element 1: 1 Array1 element 2: 2 Array1 element 3: 3 Array2 element 1: 4 Array2 element 2: 5 Array2 element 3: 6 Sum array: [5, 7, 9]
What's going on
Add corresponding elements.
Key Concepts:
Arrays must be same size
Add arr1[i] + arr2[i]
Create new array
🔗Related Content
- 📖
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- 📝
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- 💻
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum