Passing Arrays

why is is that i get the "[Error] expected primary-expression before ']' token " for my code i cant seem to find the problem ?

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
 #include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>

using namespace std;



int numOccurences(int Array[],int sizeOfArray,int Value);

int main (void) {

int Value;
int sizeOfArray;


cout << " enter the size of your array and a value " ;
cin >> sizeOfArray >> Value;

int Array[sizeOfArray]; 

for (int i =0;i<sizeOfArray;i++){
	
	cout << "Please enter a value ";
	cin >> Array[i];
	
}
                                    //why do i keep getting an error here
cout << "the number of times that value occured is " << numOccurences(Array[], sizeOfArray ,Value) << endl;

    system("PAUSE"); return 0;
}

int numOccurences(int Array[],int sizeOfArray,int Value){
int count;
	for (int i =0; i< sizeOfArray ;i++){
		
		if (Array[i] = Value){
			count++;
		}
		
		
	}
	
	return count;
}
 
Remove the [] after the array name.
First of all, line 21 is illegal. Your compiler can support it as extension, but it is non-standard and won't work everywhere.

Line 30: if you want to pass array, then pass an array and not some subscript (remove brackets)

line 39 does not do what you want.
numOccurences(Array[], sizeOfArray ,Value)
is not how a function should take in an array parameter when being called.
Get rid of the [].

Other issues:
* Your compiler may not like that you are allocating a non-const int for the size of your array.
* in your numOccurences() function, you never initialize count to be 0 before you increment it.
* if (Array[i] = Value){ should be
if (Array[i] == Value)
= is assignment operator.
== tests for equality.

Edit: Woops, sorry for saying what has already been said, the above posts didn't show up for me.
Last edited on
Ok I got rid of the braces im really a beginner dont blame me

but how do i make my function return the number of times the value appears in the array ?
You are already returning it. But there is an error in line 39 which makes it return either 0 or sizeOfArray

Also you should initialize it to 0 before doing any manipulations
Okay Guys figured it out Thanksa Alot for your help !! Appreciate it!
Topic archived. No new replies allowed.