I am trying to fix a code with errors, and I have one more error left I cannot figure out. The error says: expected primary-expression before ']' token, what does this mean? I have checked for misplaced semicolons and variable names, but cannot find anything. Here is my code(I have commented the line with an error):
// countOnes.cpp
#include<iostream>
#include<cstdlib>
using namespace std;
void countOnes( int array[] ); // Count the number of 1s in a given int array.
const int arraySize = 10;
int array[ arraySize ];
int countOne = 0;
int main()
{
for ( int i = 0; i <= arraySize; i++ )
{
array[ i ] = rand() % 3; // Fill array with 0, 1 or 2 randomly
cout << array[ i ] << " ";
}
countOnes( array[], arraySize ); //ERROR
cout << "\n The number of 1s in the array: " << countOne;
return 0;
}
void countOnes( int array[], int arraySize )
{
for ( int i = 0; i <= arraySize; i++ )
if ( array[ i ] == 1 )
countOne = countOne + 1;
return;
}