954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

counts how many times the number 5 appears in array?

Question: counts how many times the number 5 appears in array called NumArray?
Can anybody help me with this problem??
I got 4 "5s" but i don't know if i understood the question correctly or not....does "55" counts as two 5s???
and if it does then how do i count that?

#include <iostream>
using std::cout;
using std::endl;

const int Max = 10;
int main ()
{
     int NumArray [Max] = {3, 5, 10, 14, 25, 33, 41, 55, 88, 155};
     int count = 0;
     
     for (int i = 0; i < Max; i++) {
            if (NumArray[i] == 5) {
               count++;
            }
      }
      cout<<"The number 5 appears " << count <<" times in the array."<<endl;


       return (count);
}
jack223
Light Poster
27 posts since Nov 2005
Reputation Points: 10
Solved Threads: 0
 

I'd say no, but there's no way to tell. The best thing to do is ask your teacher/professor. You can always do both and that way you won't have to worry.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Same answer to the same question.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

Multiple opinions are always good.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

divide the number by 10 and extract the remainder...if the remainder is 5 then increment the counter.take an int temp variable...set-->temp=NumArray(i)

while dividing by 10 set-->temp=temp/10 and when checking the remainder check

if(temp%10==5)
{
count++;
}

The code would look something like this:

int temp=0;
for(int i=0; i<Max; i++)
{

/*the following code will check for 5's in each number
regardless of the number of digits in the number*/

          temp=NumArray[i];
          while(temp!=0)
          {
                   if(temp%10==5)
                   {
                            count++;
                   }
                   temp=temp/10;
          }
}


The code should work...if there is any problem do let me know.

ciao.gud luk.
Ashley

geekbutproud
Newbie Poster
2 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You