#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);
}
#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;
}
Title | # Comments | Views | Activity |
---|---|---|---|
autoit - check if option is checked in another program | 2 | 71 | |
Websocket through haproxy reused connection causes "Not a valid frame" error. | 4 | 42 | |
Eclipse IDE - Cannot copy/paste from console output | 8 | 73 | |
Header of docx file | 17 | 60 |
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
16 Experts available now in Live!