vendredi 29 avril 2016

Adding the two integers but one declared as an "int" and other as "auto"?

I am trying to find the sum of all the elements in an array, and declaring my initial accumulator variable sum as 0 using auto.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
    int n;
    auto sum {0};
    cin >> n;
    vector<int> arr(n);
    for(int arr_i = 0;arr_i < n;arr_i++){
        cin >> arr[arr_i];
        sum=sum+arr[arr_i];
     }
     cout<<sum;
     return 0;
}

It is giving me a compilation error. I want to know what is wrong with this.

Aucun commentaire:

Enregistrer un commentaire