I am trying to solve this simple problem in C++ (complier: Visual Studio). The result should be the smallest and largest sum you can make with the elements of a given vector, but always excluding one element when getting the sum. My solution was to make all the sums, put them in a vector, sort them and then show the first and last element. The problem I have is that the program is not executing. I get errors like arr / vector / miniMaxSum undeclared identifier... I have all the right headers included, so I don't understand why it doesn't work...
UPDATE: My program is modified and it works now. Thank you all for your input :)
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
//new and working function:
void miniMaxSum(vector<int> arr) {
double minSum=0, maxSum=0;
sort(arr.begin(), arr.end());
for (int j = 0; j<arr.size()-1; j++) {
minSum = minSum + arr[j];
maxSum= maxSum + arr[j+1];
}
cout << minSum << " " << maxSum;
}
//old and not working implementation
void miniMaxSum(vector<int> arr) { //Third error here:Error C2065 'vector': undeclared identifier
vector<int> sums;
int i = 0;
while (i <= arr.size()) {
for (int j = 0; j<arr.size(); j++) {
if (i == j) { sums[i] = sums[i] + arr[j + 1]; }
else { sums[i] = sums[i] + arr[j]; }
}
i++;
}
sort(sums.begin(), sums.end());
cout << sums[0] << " " << sums[sums.size() - 1];
}
int main() {
vector<int> arr { 1,2,3,4,5 };
miniMaxSum(arr);//First error here: Error C2065 'arr': undeclared identifier / Second error also here: Error C3861 'miniMaxSum': identifier not found
}
I checked your program with Visual Studio. It compiles.
Please try to select the commands "Clean Solution" from the build menu and then "Rebuild Solution"
Maybe that will help you.
Additionally:
But when you run your program, exceptions will be thrown. This, because in Visual Studios default debug modus, out-of-bounds checks will be performed. And, in case of problems, an exception will be thrown.
And you have several out of bounds errors here. The root cause is, that you need to understand, that arrays start with index 0. And if you have an array with 5 elements, so a size of 5, then the valid 5 indices are: 0,1,2,3,4.
If you write i <= arr.size()
then the last value for i will be 5. And that is out of bounds. And, you access arr[j + 1]
. This will also be out of bounds.
So, your program cannot run.
Please see here your programm with comments, where I see a problem:
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std; // Should never be used. Always use fully qualified names
void miniMaxSum(vector<int> arr) { // Vector should be passed by reference to the function
vector<int> sums; // Problem. Vector is Empty. It has no element
int i = 0;
while (i <= arr.size()) { // <= is false. Arrays in C++ start with index 0. So, this will go out of bounds
for (int j = 0; j < arr.size(); j++) {
if (i == j) {
sums[i] = sums[i] + arr[j + 1]; // Will go out of bounds for i and j
}
else {
sums[i] = sums[i] + arr[j]; // Will go out of bounds for i and j
}
}
i++;
}
sort(sums.begin(), sums.end());
cout << sums[0] << " " << sums[sums.size() - 1];
}
int main() {
vector<int> arr{ 1,2,3,4,5 };
miniMaxSum(arr);
// Return missing
}
For the problem solving approach. I do not fully understand your question.
The result should be the smallest and largest sum you can make with the elements of a given vector, but always excluding one element when getting the sum
This does not seem to fit to your implementation. Example. If you would sort the std::vector
in the very beginning, then the sum of the smallest element, except one, would be extremely simple. JUst sum up the first n-1 elements. For the maximum value it would be the corresponding approach.
If you clarify the requirements, I will edit my anser and add a correct solution.
Based on my original understanding, the solution could be:
#include <iostream>
#include <vector>
#include <limits>
#include <algorithm>
void showMinMaxSum(std::vector<int>& data) {
// Initialize temporary variables
int sum{}, minValue{ std::numeric_limits<int>::max() }, maxValue{ std::numeric_limits<int>::min() };;
// Calculate sum of vector elements and get may and min value
for (int temp : data) {
sum += temp;
maxValue = std::max(maxValue, temp);
minValue = std::min(minValue, temp);
}
std::cout << "Min Sum: " << sum - maxValue << " \tMax Sum: " << sum - minValue << '\n';
}
int main() {
// Test data
std::vector<int> data{ 1,2,3,4,5 };
// Calculate and show results
showMinMaxSum(data);
return 0;
}