Recursive
2012-06-05 16:16:22
I am trying to make a cpp program that asks you for an array size, it will then get the array size and the values you want in the array, then it will calculate the value the 2 smallest numbers in the array. So far this is what I have
This works to an extent in that it does not calculate the values of the 2 smallest numbers if they are both the same. Example of the result I get after running it is:
Help?
#include <iostream>
using namespace std;
int addstuff(int array[], int size)
{
int small;
int Nexsmall;
int sum;
if (size == 1)
return array[0];
else
small = array[0];
for (int A = 0; A < size; A++)
{
if (array[A] < small)
small = array[A];
}
Nexsmall = array[1];
for (int X = 0; X < size; X++)
{
if ((array[X] < Nexsmall)&&(array[X] >= small))
{
Nexsmall = array[X];
}
}
//if (Nexsmall == small)
sum = (Nexsmall + small);
return sum;
}
int main()
{
int size;
cout << "Enter the size of the array you want then enter the values in the array: ";
cin>> size;
int Addit[size];
cout<<"Enter the values in the array: ";
for (int I = 0; I < size; I++)
{
cin>> Addit[I];
}
cout<<"The numbers in the array were: "<<endl;
for(int J = 0; J < size; J++)
{
cout<<Addit[J]<<", ";
}
cout<<endl;
cout<<"The sum of the 2 smallest values is: "<<addstuff(Addit, size);
return 0;
}This works to an extent in that it does not calculate the values of the 2 smallest numbers if they are both the same. Example of the result I get after running it is:
Enter the size of the array you want then enter the values in the array: 6
Enter the values in the array: 1
2
3
4
5
1
The numbers in the array were:
1, 2, 3, 4, 5, 1,
The sum of the 2 smallest values is: 3Help?