# Assert on Arrays

## Assert on Arrays

Contents

Problem Description:

``````#include<stdio.h>
#include<assert.h>
const char *authour ="Alexandre Santos";
int ints_get(int *a)
{
int result = 0;
int x;
while (scanf("%d", &x) !=EOF)
{
a[result++] = x;
}
return result;
}

int sum_odd(const int *a, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
if(i%2 != 0)
sum += a[i];
return sum;
}
int sum_all(const int *a, int n)
{
int sum = 0;
for(int i = 0; i < n; i++)
sum = sum + a[i];
return sum;
}

int final(const int *a, int n)
{
return sum_all(a,n) - sum_odd(a,n);
}

void unit_test_even_values_minus_odd_values(void){
int a1 = {1,2,3,4,5,6,7,8};
assert(final(a1, 8) == 4);
assert(final(a1, 6) == 3);
assert(final(a1, 4) == 2);
assert(final(a1, 2) == 1);
}

void unit_tests(void){
unit_test_even_values_minus_odd_values();
}

void test_sum(void)
{
int a;
int n = ints_get(a);
int total = final(a,n);
printf("%dn", total);
}

int main()
{
test_sum();
return 0;
}
``````

I have this program but I don’t understand how the assertions work here, my main question is what the second number represents. For example I understand that `assert(final(a1, 8) == 4)` I understand that `a1` is the array determined in the line above but I can’t understand the second number `(8)`…. Can anyone explain to me how this works? I tried to search a little bit but I still don’t understand…

## Solution – 1

The second argument to `final` is the number of values to work on from that array, starting from the beginning.

`final(a1, 8)` sums all eight values. `final(a1, 6)` only sums the first six values.

Rate this post
Categories c# Tags
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.