0

I tried to write a simple code to calculate an array elements' sum. every thing looks normal but the function return the sum value wrongly (it always multiply it by two). Although if I want just print the value, it works fine. this is the code:

#include <iostream>

using namespace std;

void getElements(int[],int);
int sumOfElements(int[],int);

int number;
int sum=0;
int main()
{
   int a[10];
    getElements(a,5);
    sumOfElements(a,5);
   cout<<"The sum is "<<sumOfElements(a,5)<<endl;
    return 0;
}



//Getting  array's elements

void getElements(int numbers[],int size_)
{
    for (int i=0; i<size_; i++)
    {
        cout<<"numbers["<<i<<"]: ";
        cin>>number;
        numbers[i]=number;
    }
    cout<<'\n';
}
//Calculation the sum of array's elements
int sumOfElements(int numbers[],int size_)
{
    for(int i=0;i<size_;i++)
        {
            sum+=numbers[i];
        }
        cout<<sum<<endl;
    return sum;

}

any idea? thank you in advance!

3
  • 2
    calling sumOfElements modifies the global variable sum ... and you call sumOfElements twice. so... yeah...you're gonna have a double sum. Probably should have declared sum local to sumOfElements. Commented May 18, 2021 at 4:16
  • Now I got it! Thanks. Commented May 18, 2021 at 4:34
  • as alluded to, it's better practice to scope variables only where they're needed. You should move int number into getElements and int sum into sumOfElements. Then in main, you can capture the return value of sumOfElements and print it out, or print out the return value directly as you have done. After that change, you can call sumOfElements 10 times if you want and it will always return the same, correct sum "automatically". Furthermore, calling it without capturing its return value would essentially be a noop. Commented May 18, 2021 at 4:58

2 Answers 2

1

You defined int sum globally and were calling sumOfElementstwice, so sum contained twice what you expected.

Here is a modified version of your code that does what you want:

#include <iostream>
using namespace std;

void getElements(int[], int);
int sumOfElements(int[], int);

int main() {
  int numbers[5];
  getElements(numbers, 5);
  cout << sumOfElements(numbers, 5);
  return 0;
}

void getElements(int numbers[], int size) {
  for (int i = 0; i < size; i++) {
    cin >> numbers[i];
  }
}

int sumOfElements(int numbers[], int size) {
  int sum = 0;
  for (int i = 0; i < size; i++) {
    sum += numbers[i];
  }
  return sum;
}
Sign up to request clarification or add additional context in comments.

Comments

1

Here is a modified and simpler version of your program:

#include <array>
#include <iostream>
#include <numeric>

using namespace std;

int main(){
    const int num_elements_to_sum = 5;
    array<int, num_elements_to_sum> elements;
    for(int i=0; i<num_elements_to_sum; ++i){
        cin>>elements[i];
    }
    int sum = accumulate(elements.begin(), elements.end(), 0);
    cout<<"Sum: "<<sum<<endl;
    return 0;
}

C++ has a dedicated fixed size array container, use this instead of C-style arrays. This then allows to use standard library algorithms instead of your own implementation (e.g. accumulate).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.