trying to count occurence of a number in an array

Jul 26, 2012 at 9:12pm
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);
}

Jul 26, 2012 at 9:59pm
if (numbers[i] = mark)

Look closely. You are using assignment (=). You want comparison (==)
Jul 27, 2012 at 2:17pm
thanks, but it still doesn't do anything it wont output the count :/
Jul 27, 2012 at 2:19pm
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 Jul 27, 2012 at 2:27pm
Jul 27, 2012 at 5:25pm
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.
Jul 27, 2012 at 5:29pm
post your modified code :D
Jul 27, 2012 at 5:32pm
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);
}
Jul 27, 2012 at 5:38pm
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' .
Jul 27, 2012 at 5:40pm
oops didnt notice that however now it outputs 0 for every thing.
Jul 27, 2012 at 5:43pm
shouldn't cout <<"that mark occurs"<< count<< "times\n"; be outside the for loop ?
Jul 27, 2012 at 5:47pm
yea, another silly mistake sorry however still the same problem as before :/
Last edited on Jul 27, 2012 at 5:48pm
Jul 27, 2012 at 5:57pm
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 Jul 27, 2012 at 6:01pm
Jul 27, 2012 at 6:07pm
1
2
3
4
#include <algorithm>

//where 'search' is some number you are searching for
count = (int) std::count (numbers , numbers + 14, search);
Jul 27, 2012 at 6:09pm
@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 ?
Jul 27, 2012 at 6:18pm
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);
}
Jul 27, 2012 at 6:27pm
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 Jul 27, 2012 at 6:27pm
Jul 27, 2012 at 6:44pm
works for other data types too , right ?

Aye
http://www.cplusplus.com/reference/algorithm/count/
Jul 27, 2012 at 10:33pm
Thanx a lot ^_^
Jul 28, 2012 at 11:09am
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"
Jul 30, 2012 at 4:25pm
He was talking about the last semi colon after the for loop.
Topic archived. No new replies allowed.