User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 370,604 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,043 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2010 | Replies: 4
Reply
Join Date: Nov 2005
Posts: 27
Reputation: jack223 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
jack223 jack223 is offline Offline
Light Poster

counts how many times the number 5 appears in array?

  #1  
Nov 15th, 2005
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);
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: counts how many times the number 5 appears in array?

  #2  
Nov 15th, 2005
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.
Reply With Quote  
Join Date: Apr 2004
Posts: 3,412
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 15
Solved Threads: 137
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: counts how many times the number 5 appears in array?

  #3  
Nov 15th, 2005
Same answer to the same question.
Reply With Quote  
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation: server_crash is on a distinguished road 
Rep Power: 9
Solved Threads: 18
server_crash's Avatar
server_crash server_crash is offline Offline
Postaholic

Re: counts how many times the number 5 appears in array?

  #4  
Nov 16th, 2005
Multiple opinions are always good.
Reply With Quote  
Join Date: Mar 2005
Posts: 2
Reputation: geekbutproud is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
geekbutproud's Avatar
geekbutproud geekbutproud is offline Offline
Newbie Poster

Solution Re: counts how many times the number 5 appears in array?

  #5  
Nov 17th, 2005
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
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC