Adding up array and returning bool function??

Hi there! I'm wanting my varSum function to take in an array with a value of N, where if the sum of the array ==15 it returns true, false otherwise.

I am having trouble with this, as well as successfully calling the varSum function in int main! Please help, I am very much a beginner still.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

#include <iostream>
#include <string>

int main()
{

  int k;
  k = varSum (5, 4, 3);
  std:: cout << k;
  
}

bool varSum (int array[], int N, int sum){
    //int array[N];
    
    sum = 15;
    
    for (N =0; N<10; N++){
        N+=sum;
    
    if (array[sum] != array[N]){
        return false;
    }
    else{
        return true;
    }
    }
    return N;
}
A few problems:

Line 4: You need a function prototype in order to call varSum().

Line 9: first argument: varSum is expecting an array. You're passing a single value.
You probably want to declare an array as follows:
1
2
// line 7:  
  int arr[] = { 5, 4, 3};


Lines 17-29: if all you want to do is sum the array and check if the result is 15:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>

bool varSum (int array[], int N);

int main()
{   bool k;
    int arr[] = { 5, 4, 3 };
    
    k = varSum (arr, 3);
    std::cout << k;  
}

bool varSum (int arr[], int N)
{   int sum = 0;  
    for (int i=0; i<N; i++)
        sum += arr[i];     
    return sum == 15; 
}

Topic archived. No new replies allowed.