-1

This is the program for printing out sum of array elements. It is showing run time error. The output is coming out to be 0 instead of printing out the sum of the elements.

#include<iostream.h>
using namespace std;
void simpleArraySum()   
{
    int ar[100],n,i,sum=0;

    for(i=0;i<n;i++)
    {
        sum=sum + ar[i];
    }

    cout<<sum;
}
int main()
{
    int ar[100],n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
    }

    simpleArraySum();
    return 0;
}

  • From the requirements that you posted, the program is behaving exactly as specified. – Thomas Matthews Jun 30 '20 at 17:52
  • What is the program supposed to do? Hackerrank has very many challenges, so which one is this? – Thomas Matthews Jun 30 '20 at 17:53
  • 2
    you have two arrays, one you read input into and one you sum, I suppose they should be one and the same – 463035818_is_not_an_ai Jun 30 '20 at 17:54
  • 1
    The array in `simpleArraySum` is not the same array in `main()`. – Thomas Matthews Jun 30 '20 at 17:54
  • 1
    btw you dont need any array, simply add up the numbers and print the result – 463035818_is_not_an_ai Jun 30 '20 at 17:55
  • 3
    Variables and arrays in different functions aren't the same just because you give them the same name. That's not how C++ works (or any programming language I can think of). You probably need to get acquainted with some basic programming concepts, here's a list of C++ books https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – john Jun 30 '20 at 17:58
  • It is the SimpleArraySum problem of Algorithms section. The sum is supposed to be coming out but instead 0 is getting printed for any group of members – Eesha Srivastava Jun 30 '20 at 18:01
  • 1
    once you got this code correct you want to take a look at `std::vector`. Much easier to use and with a fixed size array you either waste memory or have too little space in the array, while a vector can change its size at runtime – 463035818_is_not_an_ai Jun 30 '20 at 18:08
  • 1
    As you're programming in C++, I suggest you declare ar an std::vector, reserve its size to n elements, and pass it by reference to simpleArraySum. Then you could use the size() member function to loop over it, or, better yet, use STL's accumulate to sum up the elements. – Uri Raz Jun 30 '20 at 18:10
  • 1
    @UriRaz Actually, this is not C++ as we know it, but a pre-standard variant from the 1980s that you have to compile in a DOS emulator. Don't know why C++11 was tagged, but observe the sole include. – Asteroids With Wings Jun 30 '20 at 18:52
  • @AsteroidsWithWings How do you know? And why would anyone teach it? – Uri Raz Jul 01 '20 at 04:06
  • @UriRaz Because of the include (as I already said), and because of experience. As for why anyone would teach it, I don't know _why_, but the Indian programming curriculum uses Turbo C++ throughout. – Asteroids With Wings Jul 01 '20 at 14:35

4 Answers4

3

On this line in your main:

int ar[100], n;

You create an array of 100 elements. You later fill that array using cin

for(int i = 0 ; i < n ; i++)
{
    cin >> ar[i];
}

Then you do nothing with that array. You are not calculating any sum. You let that array go, forgotten.

Then, you call a simpleArraySum function. That function is creating an entirely new, distinct array.

//  v-----v------There
int ar[100],n,i,sum=0;

That array has no value assigned to it. In fact, reading from it is undefined behavior.

What you want is to receive that array in the arguments of your function:

void simpleArraySum(int* ar, int n) {
    // ...
}

And call it like that in your main:

simpleArraySum(ar, 100);
Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
  • Thank you so much everyone. I solved it by using void SimpleArraySum(int *ar, int n) – Eesha Srivastava Jun 30 '20 at 18:04
  • I am actually suprised he got 0 for the sum, C++ does not intialize default values for arrays, I would have expected him to get something like 180982938 or something like that.... – Yunfei Chen Jul 01 '20 at 02:44
2

You can avoid the issues of arrays and functions by not using them:

int main()
{
  int quantity = 0;
  std::cin >> quantity;
  int sum = 0;
  int value;
  while (std::cin >> value)
  {
     sum += value;
  }
  std::cout << sum << "\n";
  return EXIT_SUCCESS;
}
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • sorry for the nitpick, but not knowing the details of the task, this code might do something different. There might be more numbers after the first `n` numbers ;) – 463035818_is_not_an_ai Jun 30 '20 at 18:13
1

In simpleArraySum, the variable n is uninitialized. So this loop:

for(i=0;i<n;i++)

invokes undefined behavior when reading from n.

Also, you are summing a different array in the function, than the one you read in mian. It seems that you need to pass in the array from main to this function:

void simpleArraySum(int *ar, int n) {

and call it like this:

simpleArraySum(ar, n);

Finally, you don't even need a function for this, since there is an existing algorithm std::accumulate that you can use:

cout << std::accumulate(ar, ar + n, 0);
cigien
  • 57,834
  • 11
  • 73
  • 112
0

In the function, you're adding the elements of ar which is local to the function simpleArraySum() and is not of the array ar that is local to main().

So, pass the array and its length to the function and return its sum. Here is your corrected code:

#include<iostream>
using namespace std;
void simpleArraySum(int ar[], int n)   
{
    int i, sum = 0;

    for(i=0;i<n;i++)
    {
        sum=sum + ar[i];
    }

    cout<<sum;
}
int main()
{
    int ar[100],n;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>ar[i];
    }

    simpleArraySum(ar, n);
    return 0;
}