•
•
•
•
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
![]() |
•
•
Join Date: Nov 2005
Posts: 27
Reputation:
Rep Power: 3
Solved Threads: 0
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?
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);
}•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
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.
•
•
Join Date: Jun 2004
Location: H4x0rville
Posts: 2,105
Reputation:
Rep Power: 9
Solved Threads: 18
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:
The code should work...if there is any problem do let me know.
ciao.gud luk.
Ashley
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
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
Other Threads in the C++ Forum
- Previous Thread: Compiling Problems
- Next Thread: look around this problem



Linear Mode