1

For some reason I am not getting the desired answer and I can't fathom where the error is.

For example, given the array ar = [1,2,3], 1+2+3 = 6, so return 6, but I am getting 650462183 instead.

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>
    
using namespace std;
    
int main ()
{
    int i;
    
    cout << "enter the value of i" << endl;
    
    cin >> i;
    
    int arr[i];
    
    cout << "enter the value of yout arr\n";
    
    cin >> arr[i];
    
    int count = 0;
    
    for (int j = 0; j <= i; j++)
    {
          count += arr[j];
    }
    
    cout << count << endl;
    
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 5
    Well, for starters, `int arr[i];` is [non-standard C++](https://stackoverflow.com/questions/1887097/), you should be using [`std::vector`](https://en.cppreference.com/w/cpp/container/vector) instead. And `cin >> arr[i];` is writing a single value out of bounds of the array. You are not actually filling the array with any values at all before entering your `count` loop. Also, FYI, the standard C++ library has a function for summing the values of a range, such as from an array: [`std::accumulate()`](https://en.cppreference.com/w/cpp/algorithm/accumulate) – Remy Lebeau Jun 19 '21 at 01:00
  • What do you expect the line `cin >> arr[i];` to do? Do you expect it to read several numbers and fill them into the array? If that is what you want, then you will need a loop instead. – Andreas Wenzel Jun 19 '21 at 01:00
  • You didn't input an array.. you had requested value for a single element arr[i] which is outside of array's bounds. Also j<=i is incorrect for same reason – Swift - Friday Pie Jun 19 '21 at 01:01
  • Addendum: A `<=` in a for loop condition is so often wrong that if you see it when reviewing code, yours or anyone's, take a closer look at the loop because it's probably a bug. – user4581301 Jun 19 '21 at 01:04
  • Please do read more about Array basics. You really need to do that. – Abhishek Dutt Jun 19 '21 at 03:07

1 Answers1

8

The array int arr[i]; has only i elements: arr[0] to arr[i-1].

Therefore, the condition of the loop should be j < i, not j <= i.

cin >> arr[i]; is also wrong. You should use another loop to read i elements instead of readling only one to an out-of-range element like this.

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>

using namespace std;

int main ()
{

  int i;

  cout << "enter the value of i" << endl;

  cin >> i;

  int arr[i];

  cout << "enter the value of yout arr\n";

  for (int j = 0; j < i; j++)

  {

    cin >> arr[j];

  }

  int count = 0;

  for (int j = 0; j < i; j++)

  {

    count += arr[j];

  }

  cout << count << endl;

  return 0;
}

Note that variable-length arrays (VLA) like int arr[i]; is not supported in the standard C++. You should use std::vector instead. You can do this by changing int arr[i]; to std::vector<int> arr(i); in this case.

Another choice is not using arrays and summing what are entered directly:

#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cctype>

using namespace std;

int main ()
{

  int i;

  cout << "enter the value of i" << endl;

  cin >> i;


  cout << "enter the value of yout arr\n";

  int count = 0;

  for (int j = 0; j < i; j++)

  {

    int element;

    cin >> element;

    count += element;

  }

  cout << count << endl;

  return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70