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);
}

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.

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

This article has been dead for over six months. Start a new discussion instead.