What do you mean make it more complicated :S
The first function returns a vector of integers.. A vector is basically a dynamic array. So essentially I'm using that as an array of integers. What that function is doing is deleted a value from the vector.
Next function will sum the smallest values in the array you provide it. it uses a vector to copy the array for use with the delete function.
Everytime it finds a small value, it stores it adds it to the previous value and then deletes it from the vector. This allows us to add as many minimum values from the array as we like.
I got lazy and did not want to use
int* ListOfValues = new int[size];
delete[] ListOfValues..
Thus I used a vector.
You can use that above function like this.. lets say you want the sum of the 5 smallest values in the following array:
int Array[12] = {0, 4, 1, 2, 3,7, 6, 8, 99, 10, 13, 12}
You'd do:
SumSmallestAmount(Array, 12, 5); Where 12 is the size, 5 is the amount of small values you wish to sum.
Simple as that :S I do not wish to make anyone's life harder than it already is. I hope I did not discourage you. I just wrote what I thought would be easiest for you to understand rather than me placing pointers everywhere. I used to teach C++ to myself and I know how it gets which is why I posted that instead of manually resizing the arrays which is tedious. I know a lot of ppl say C++ is hard :c Trust me I know enough students I tutored saying it's hard. I wouldn't try to make it harder for you :c
If you prefer arrays then this is just a simple solution:
C++ Code:
#include <windows.h>
#include <iostream>
#include <vector>
using namespace std;
/** Finds the Smallest Value(s) in an array **/
int Smallest(int Array[], int size)
{
if (size == 1) //So we check if the size of the array is 1. If it is, return the only element.
return Array[0];
int A = Array[0]; //Initialize a global variable to hold our result as the first element.
for (int I = 0; I < size; I++) //Loop through our array while checking if the previous value is less or greater than the current one.
A = Array[I] < A ? Array[I] : A; //If Array[I] < A then A = Array[I] else A = itself. This compares if the previous value (A) is less than the current on (Array[I]).
return A; //return the smallest value.
}
/** Simulates Delete a Value from an array **/
void DeleteElement(int Array[], int& size, int Value)
{
for (int I = 0; I < size; I++) //First we loop through our array.
if (Array[I] == Value) //We check if the value we want to delete is in our array.
{
for (I; I < size - 1; I++) //if it is, loop through our array and shift that value to the very end.
Array[I] = Array[I + 1];
--size; //this is our tracker variable and we minus one from the size to simulate deleting.
break; //after deleting once, we break out of our loop unless you want to delete all of the found values.
}
}
/** Finds Sum of the smallest values in an array **/
int SumSmallestAmount(int Array[], int size, int HowMany)
{
int result = 0; //initialize a global variable that holds the sum of our values.
for (int I = 0; I < HowMany; I++) //we want to sum a certain amount of values so just loop until we reach that amount.
{
int J = Smallest(Array, size); //lets store the smallest amount in a variable.
result += J; //we add our new smallest amount to our previous one which can be 0 or more.
DeleteElement(Array, size, J); //after adding our value, we delete it from the array and start all over until we are done summing the amount.
}
return result; //finally return our sum of "HowMany" values.
}
int main()
{
int Array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; //Our list of numbers.
cout<<SumSmallestAmount(Array, 10, 4); //Prints the sum of the 4 smallest values in the array of size 10.
}
The reason we delete after summing the result is because when we find the smallest value 2 or more times, we don't want that value to always be the same unless it's a duplicate. So after summing it, we just remove it and call smallest again which should give us a new one unless of course it occurred one or more times.
The code looks WAY CLEANER without comments :c Now it looks horrible and looks hard at first sight.