Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.
#include <iostream>
using namespace std;
int sumOfElements(const int arr[], int size);
int main()
{
int answer;
int arrSize;
cout << "how many numbers do you want to add" << endl;
cin >> arrSize;
int num[arrSize];
for (int i = 0; i < arrSize; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> num[i];
}
answer = sumOfElements(num,arrSize);
cout << "The sum of the numbers entered is: " << answer << endl;
return 0;
}
int sumOfElements(const int arr[], int size)
{
int sum = arr[0];
if (size == 1)
return sum;
else
return sum+= sumOfElements(arr, size + 1);
}
Add your voice to the tech community where 5M+ people just like you are talking about what matters.
#include <iostream>
using namespace std;
int sumOfElements(const int arr[], int size, int sum);
int main()
{
int answer;
int arrSize;
int sum = 0;
cout << "how many numbers do you want to add" << endl;
cin >> arrSize;
int num[arrSize];
for (int i = 0; i < arrSize; i++)
{
cout << "Enter number " << i + 1 << ": ";
cin >> num[i];
}
sum = num[0];
answer = sumOfElements(num,arrSize, sum);
cout << "The sum of the numbers entered is: " << answer << endl;
return 0;
}
int sumOfElements(const int arr[], int size, int sum)
{
if (size == 1)
return sum;
else
return sum+= sumOfElements(arr, size - 1, sum);
}
If you are trying to add elements of array, you should be passing only the index NOT sum and the it should return value of that element likeI think there's a mix-up of terminology going on in that statement. I agree that you should be "passing" some sort of index (or indexed value), but the "return" is valid as is written = )
return arr[index];
instead of return sum
#include<iostream>
using namespace std;
int sumOfElements(const int[], int);
int main()
{
int arr[3] = {3, 5, 7};
int sum = sumOfElements(arr, 2);
cout << "Sum : " << sum << endl;
//system("pause");
return 0;
}
int sumOfElements(const int arr[], int index)
{
if(index > 0)
{
return arr[index] + sumOfElements( arr, index -1);
}
else
{
return arr[0];
}
}
#include<iostream>
using namespace std;
int sumOfElements(const int[], int);
int main()
{
int arr[3] = {3, 5, 7};
int sum = sumOfElements(arr, 2);
cout << "Sum : " << sum << endl;
//system("pause");
return 0;
}
int sumOfElements(const int arr[], int index)
{
if(index > 0)
{
return arr[index] + sumOfElements( arr, index -1);
}
else
{
return arr[0];
}
}
Kindly tell me how would you write it. It produces the correct result in every situation.Ah, but in your newest code you are using index rather than size. That is expected. I explained the use of size above (for Dmon443's benefit) = )
int main()
{
int arr[] = {3, 5, 7};
int max_index = sizeof(arr)/sizeof(int) -1 ;
int sum = sumOfElements(arr, max_index);
cout << "Sum : " << sum << endl;
system("pause");
return 0;
}
If you are experiencing a similar issue, please ask a related question
Join the community of 500,000 technology professionals and ask your questions.