Reputation: 45
I am getting the following erors:
1. expected = , ; before { (line 2)
2. expected { at end of input (line 12)
Here is my code:
#include <stdio.h>
#include "evenSum.h"
int Even_Sum(int array, int i)
{
for(i = 0; i < 10; ++i)
{
if(array[i] % 2 =0)
{
int sum=0;
sum += array[i];
return sum;
}
}
}
My header file contains the line
int Even_Sum(int array, int i)
Upvotes: 0
Views: 110
Reputation: 72667
Once you fixed all the syntax problems, are you sure you want to return as soon as you've found the first even number in the array? Maybe you meant something like this, which iterates over the whole array and sums all even numbers. Note that you need a pointer to int as the first Even_Sum parameter.
Note also that the i
parameter is useless in your code; in particular if it is meant to pass the number of elements in the array, you should not use it as an index variable. I've renamed it n
and made the loop run from 0 to n-1.
#include <stdio.h>
#include "evenSum.h"
int Even_Sum (int *array, int n)
{
int sum = 0;
int i;
for (i = 0; i < n; ++i)
{
if (array[i] % 2 == 0)
{
sum += array[i];
}
}
return sum;
}
Upvotes: 1
Reputation: 1253
As a general debugging tip for messages like this, if you're getting errors (especially that "expected ;
" one) at the start of your file then check the header file for typos. When you #include
a header file it is like the compiler inserting the whole file into your file at that point, so errors on the first or second line hint to problems with the header file (since that's effectively "the line before").
Upvotes: 0
Reputation: 40499
Add a ;
after the int Even_Sum(int array, int i)
in your header file.
Without that ;
the compiler sees
int Even_Sum(int array, int i)
int Even_Sum(int array, int i)
{
for(i = 0; i < 10; ++i)
{
... etc ...
This, of course, is not valid c syntax. Therefore, you need the ;
.
Edit as others pointed out, you want to work on the int array
parameter since array
is used as an array of int not as an int.
Upvotes: 4