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
607k36 gold badges515 silver badges867 bronze badges
asked Jun 19, 2021 at 0:54
5
  • 5
    Well, for starters, int arr[i]; is non-standard C++, you should be using std::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() Commented Jun 19, 2021 at 1: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. Commented Jun 19, 2021 at 1: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 Commented Jun 19, 2021 at 1: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. Commented Jun 19, 2021 at 1:04
  • Please do read more about Array basics. You really need to do that. Commented Jun 19, 2021 at 3:07

1 Answer 1

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;
}
answered Jun 19, 2021 at 1:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.