2

I'm beginner in C++, and I've got a question about a simple sum code in c++.

Here is my code :

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
 int n;
 int sum;
 int arr_i = 0;
 cin >> n;
 vector<int> arr(n);
 while (arr_i != n)
 {
 cin >> arr[arr_i];
 sum += arr[arr_i];
 //cout << sum << endl;
 if (arr_i == n - 1)
 cout << sum;
 arr_i++;
 }
 return 0;
}

The output doesn't print the correct answer without "cout << sum" before the if condition.

How can I solve this problem ?

Stephen
1,53817 silver badges26 bronze badges
asked Apr 8, 2016 at 22:08
3
  • You don't need to include cmath.h for a simple sum. Commented Apr 8, 2016 at 22:12
  • 2
    @ThomasMatthews - He doesn't need to put them in a vector either. All he needs is iostream. He is beginning C++. Nit: It is either math.h or cmath, not cmath.h Commented Apr 8, 2016 at 22:18
  • ...if you happen to be on C++14, a one-line equivalent of the while loop would be cout << accumulate(arr.begin(), arr.end(), 0); Commented Apr 9, 2016 at 4:41

2 Answers 2

7

You forget to initialize sum to 0.

int sum = 0;
answered Apr 8, 2016 at 22:09
0
1

As the previous post mentioned, sum was not initialized to 0. In terms of good practices and styles its a good idea to initialize any variables that are modified within a loop right before the loop body so that someone reading the code can easily grasp the context of your variables.

int main()
{
 int n;
 int sum;
 int arr_i;
 cin >> n;
 vector<int> arr(n);
 sum = 0;
 arr_i = 0;
 while (arr_i != n)
 {
 cin >> arr[arr_i];
 sum += arr[arr_i];
 //cout << sum << endl;
 if (arr_i == n - 1)
 cout << sum;
 arr_i++;
 }
 return 0;
}

or as I prefer a "for" loop...

int main()
{
 int n;
 int sum;
 int arr_i;
 cin >> n;
 vector<int> arr(n);
 for (sum = 0, arr_i = 0; arr_i != n; arr_i++)
 {
 cin >> arr[arr_i];
 sum += arr[arr_i];
 //cout << sum << endl;
 if (arr_i == n - 1)
 cout << sum;
 }
 return 0;
}
answered Apr 8, 2016 at 22:22
3
  • Even better, initialize them at declaration, and reduce scope of the variable. Commented Apr 8, 2016 at 22:37
  • @Jarod42 Good point. Yes, i didn't want to deviate too much from his original post, but definitely declaring and initializing the variable in a block of code where it is only used is even better. So a "for (int sum = 0; int array_i = 0; arr_i != n; arr_i++)" would be best in this case. Commented Apr 8, 2016 at 22:51
  • A for loop would actually be the preferred method as you know how many times the loop is iterating because they are having the user enter "n" times of iterations. Even though a while loop is justifiable, a for loop seems more proper. Commented Apr 8, 2016 at 23:22

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.