trying to count occurence of a number in an array

hi im new to c++ so i apologize for any stupidly obvious mistakes. im trying count the amount of times a certain number appears in an array ive had a go from what ive seen in other posts but i dont think im even close heres the code so far:

#include <iostream>

using namespace std;

int main()
{

int mark, i;
int count = 0;
int numbers[14]={0,1,2,3,4,5,6,4,4,7,7,7,8,8};


cout<<"please enter the mark you need to count";
cin>> mark;

for (i = 0; i < count++;)
{
if (numbers[i] = mark)
{
++i;
}

cout <<count;
}


system ("pause");

return (0);
}

if (numbers[i] = mark)

Look closely. You are using assignment (=). You want comparison (==)
thanks, but it still doesn't do anything it wont output the count :/
1
2
3
4
if (numbers[i] = mark)
{
++i;
}


1. Your for loop should loop from i = 0 to i = 14, not count, because your count is 0 so the for loop is not working.
It should be for(int i=0;i<14;++i)
2. should be '==' instead of '=' for comparison instead of assignment
3. use count++ instead of ++i.
your code means you check the 3rd element and jumped 2nd element if your 1st element is same as your input, not counting the occurence.
Last edited on
that has helped and it does now output a number however it only says occurs once even if it occurs several times in the array.
post your modified code :D
heres the most recent version:

#include <iostream>

using namespace std;

int main()
{

int mark, i;
int count = 0;
int numbers[14]={0,1,2,3,4,5,6,4,4,7,7,7,8,8};


cout<<"please enter the mark you need to count\n";
cin>> mark;

for (i = 0; i <14; i++);
{
if (numbers[i] = mark)
{
count++;
}

cout <<"that mark occurs"<< count<< "times\n";
}


system ("pause");

return (0);
}
if (numbers[i] = mark)
should be
if (numbers[i] == mark)

because the
=
operator means that you asign that value to numbers[i] and then if numbers[i] is equal with 0 the if statement will return false otherwise it will return true and the
==
operator means 'is equal to' .
oops didnt notice that however now it outputs 0 for every thing.
shouldn't cout <<"that mark occurs"<< count<< "times\n"; be outside the for loop ?
yea, another silly mistake sorry however still the same problem as before :/
Last edited on
for (i = 0; i <14; i++);
should be for (i = 0; i <14; i++)
remove the ';' because now it increases 'i' 14 times and then it checks the if statement 1 time

in the future instead of declaring 'i' in the beginning simply use
for (int i = 0; i <14; i++) so it's best performance and you avoid these bugs
Last edited on
1
2
3
4
#include <algorithm>

//where 'search' is some number you are searching for
count = (int) std::count (numbers , numbers + 14, search);
@clanmjc :D that's a new approach for me :D but it looks just fine :D i might use it next time ^_^ works for other data types too , right ?
there's a run-time error now which says the variable 'i' is being used without being initialized. and there are errors when i remove any semi colons.
heres the code ive got now:
#include <iostream>

using namespace std;

int main()
{

int mark,i ;
int count = 0;
int numbers[14]={0,1,2,3,4,5,6,4,4,7,7,7,8,8};


cout<<"please enter the mark you need to count\n";
cin>> mark;

for (int i = 0; i <14; i++);
{
if (numbers[i] == mark)
{
count++;
}


}
cout <<"that mark occurs"<< count<< "times\n";

system ("pause");

return (0);
}
int mark, i ; should be int mark; so you won't declare i multiple times and you still didn't remove the semicolon from for (int i = 0; i <14; i++);
Last edited on
works for other data types too , right ?

Aye
http://www.cplusplus.com/reference/algorithm/count/
Thanx a lot ^_^
i get build errors when i remove any semi colons from the for loop and, i cant remove the 'i' from 'int mark, i ;' as it says "i is undefined"
He was talking about the last semi colon after the for loop.
Topic archived. No new replies allowed.