Open GasimV opened 1 month ago
Step 1: Reading and storing integers
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {4, 8, 15, 16, 23, 42}; // Example input
// Check if the numbers are stored correctly
for (int num : numbers) {
std::cout << num << " ";
}
return 0;
}
Test Output: 4 8 15 16 23 42
Step 2: Calculating the sum
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {4, 8, 15, 16, 23, 42};
int sum = 0;
for (int num : numbers) {
sum += num;
}
std::cout << "Sum: " << sum << std::endl; // Check sum calculation
return 0;
}
Test Output: Sum: 108
Step 3: Calculating the average
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {4, 8, 15, 16, 23, 42};
int sum = 0;
for (int num : numbers) {
sum += num;
}
double average = static_cast<double>(sum) / numbers.size();
std::cout << "Average: " << average << std::endl; // Check average calculation
return 0;
}
Test Output: Average: 18
#include <iostream>
#include <vector>
int main() {
std::vector<int> numbers = {4, 8, 15, 16, 23, 42}; // Step 1
int sum = 0;
// Step 2: Calculate the sum
for (int num : numbers) {
sum += num;
}
// Step 3: Calculate the average
double average = static_cast<double>(sum) / numbers.size();
// Output the result
std::cout << "The average is: " << average << std::endl;
return 0;
}
Final Output: The average is: 18
This example demonstrates the structured approach from breaking down the problem to testing each step and integrating into the final solution.
Step 1: Divide the array
Step 2: Merge subarrays
Step 1: Dividing the array
#include <iostream>
#include <vector>
// Function to split the array into two halves
void splitArray(const std::vector<int>& arr, std::vector<int>& left, std::vector<int>& right) {
int mid = arr.size() / 2;
left.assign(arr.begin(), arr.begin() + mid);
right.assign(arr.begin() + mid, arr.end());
}
int main() {
std::vector<int> arr = {38, 27, 43, 3, 9, 82, 10};
std::vector<int> left, right;
// Test splitting the array
splitArray(arr, left, right);
// Output left half
std::cout << "Left half: ";
for (int num : left) std::cout << num << " ";
std::cout << std::endl;
// Output right half
std::cout << "Right half: ";
for (int num : right) std::cout << num << " ";
std::cout << std::endl;
return 0;
}
Test Output:
Left half: 38 27 43
Right half: 3 9 82 10
Step 2: Merging two sorted subarrays
#include <iostream>
#include <vector>
// Function to merge two sorted arrays into one
std::vector<int> merge(const std::vector<int>& left, const std::vector<int>& right) {
std::vector<int> merged;
int i = 0, j = 0;
while (i < left.size() && j < right.size()) {
if (left[i] < right[j]) {
merged.push_back(left[i++]);
} else {
merged.push_back(right[j++]);
}
}
// Append remaining elements
while (i < left.size()) merged.push_back(left[i++]);
while (j < right.size()) merged.push_back(right[j++]);
return merged;
}
int main() {
std::vector<int> left = {3, 27, 38};
std::vector<int> right = {9, 10, 43, 82};
// Test merging
std::vector<int> merged = merge(left, right);
// Output merged array
std::cout << "Merged array: ";
for (int num : merged) std::cout << num << " ";
std::cout << std::endl;
return 0;
}
Test Output: Merged array: 3 9 10 27 38 43 82
Step 3: Combining Split and Merge Recursively
#include <iostream>
#include <vector>
// Function to perform merge sort
std::vector<int> mergeSort(const std::vector<int>& arr) {
if (arr.size() <= 1) return arr;
// Divide
std::vector<int> left, right;
splitArray(arr, left, right);
// Recursively sort each half
left = mergeSort(left);
right = mergeSort(right);
// Merge sorted halves
return merge(left, right);
}
int main() {
std::vector<int> arr = {38, 27, 43, 3, 9, 82, 10};
std::vector<int> sortedArr = mergeSort(arr);
// Output the sorted array
std::cout << "Sorted array: ";
for (int num : sortedArr) std::cout << num << " ";
std::cout << std::endl;
return 0;
}
Test Output: Sorted array: 3 9 10 27 38 43 82
#include <iostream>
#include <vector>
// Function to split the array into two halves
void splitArray(const std::vector<int>& arr, std::vector<int>& left, std::vector<int>& right) {
int mid = arr.size() / 2;
left.assign(arr.begin(), arr.begin() + mid);
right.assign(arr.begin() + mid, arr.end());
}
// Function to merge two sorted arrays into one
std::vector<int> merge(const std::vector<int>& left, const std::vector<int>& right) {
std::vector<int> merged;
int i = 0, j = 0;
while (i < left.size() && j < right.size()) {
if (left[i] < right[j]) {
merged.push_back(left[i++]);
} else {
merged.push_back(right[j++]);
}
}
// Append remaining elements
while (i < left.size()) merged.push_back(left[i++]);
while (j < right.size()) merged.push_back(right[j++]);
return merged;
}
// Function to perform merge sort
std::vector<int> mergeSort(const std::vector<int>& arr) {
if (arr.size() <= 1) return arr;
// Divide
std::vector<int> left, right;
splitArray(arr, left, right);
// Recursively sort each half
left = mergeSort(left);
right = mergeSort(right);
// Merge sorted halves
return merge(left, right);
}
int main() {
std::vector<int> arr = {38, 27, 43, 3, 9, 82, 10};
std::vector<int> sortedArr = mergeSort(arr);
// Output the sorted array
std::cout << "Sorted array: ";
for (int num : sortedArr) std::cout << num << " ";
std::cout << std::endl;
return 0;
}
Final Output: Sorted array: 3 9 10 27 38 43 82
This example demonstrates applying the structured approach to a complex problem using the Merge Sort algorithm by breaking down the logic, writing and testing individual components, and integrating them into a final working program.
Define Problem Scope:
Break Down Logic:
Write and Test Code for Each Step:
Integrate:
This structured framework ensures clarity, reduces cognitive load, and facilitates systematic development.