0

I'm trying to write a function that calculates the sum of an array, but when i declare int size = 0; , the function runs 0 times because i=0 ; i

int arraChec(int arra[]) {

    int size = 0;
    int sum = 0;

    for (int i = 0; i < size; i++) {
        sum = sum + arra[i];
    }
    return sum;
}


int main() {

    int arra1[7] = { 2,3,5,7,8,9,1 };

    cout << arraChec(arra1) << endl;

    system("pause");
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Johan
  • 69
  • 2
  • 8
  • 1
    Add another function parameter that represents the array size. Use accordingly. – Ron Oct 14 '17 at 09:09
  • The problem is that i dont want to put i < 7, for example, i want the program to figure the size by itself, when i declare the array in main – Johan Oct 14 '17 at 09:11
  • 3
    Then use `std::array` or `std::vector`/ In general stop using C-style arrays and pointers. – Richard Critten Oct 14 '17 at 09:44

4 Answers4

1

You need to pass two arguments to the function--either the beginning of the array plus the size, or the beginning and (one past the) end, as is conventional in C++:

int arraChec(int* begin, int* end) {
    int sum = 0;
    for (int* it = begin; it < end; ++it) {
        sum += *it;
    }
    return sum;
}

int main() {

    int arra1[7] = { 2,3,5,7,8,9,1 };

    cout << arraChec(std::begin(arra1), std::end(arra1)) << endl;

    system("pause");
}

Of course, you can implement is using the standard library:

cout << std::accumulate(std::begin(arra1), std::end(arra1), 0) << endl;
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

Pass in the array size as a parameter:

#include <iostream>
int arraChec(int arra[], int size) {
    int sum = 0;
    for (int i = 0; i < size; i++) {
        sum += arra[i];
    }
    return sum;
}
int main() {
    int arra1[7] = { 2, 3, 5, 7, 8, 9, 1 };
    std::cout << arraChec(arra1, 7) << std::endl;
}

Or use std::vector:

#include <iostream>
#include <vector>

int arraChec(std::vector<int>& arra) {
    int sum = 0;
    for (int i = 0; i < arra.size(); i++) {
        sum += arra[i];
    }
    return sum;
}

int main() {
    std::vector<int> arra1 = { 2, 3, 5, 7, 8, 9, 1 };
    std::cout << arraChec(arra1) << std::endl;
}

If you are referring to some C style (sizeof(arra) / sizeof(*arra)) construct I suggest you refrain from using it.

Ron
  • 14,674
  • 4
  • 34
  • 47
1

Use std::array instead of fixed size C-style array.

#include <iostream>
#include <array>
#include <numeric>
using namespace std;

int main() {
    array<int, 7> arr = { 2, 3, 5, 7, 8, 9, 1 };
    cout << accumulate(arr.begin(), arr.end(), 0) << endl;
    return 0;
}

Output

35

Read more about std::accumulate.

Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
1

Another way not mentioned yet is:

template<size_t N>
int arraChec(int (&arra)[N]) {
    int sum = 0;

    for (size_t i = 0; i < N; i++) {
        sum = sum + arra[i];
    }
    return sum;
}
M.M
  • 138,810
  • 21
  • 208
  • 365
  • @Blastfurnace Indeed. I borrowed the terminology from [this SO post](https://stackoverflow.com/questions/2384107/magic-arguments-in-function-templates). – Ron Oct 14 '17 at 10:01