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

How to find occurence of a digit in a number.

hey guys..
greetings to all
please help me regarding my problem
i want to find how many times a digit has occurred in a number.
for example

i have an array which consists of numbers

1011 1022 1033

number of 1's= 5
number of 2's= 2
number of 3's= 2

.. i want to to that .. please help how can i do this.

hydersha
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

>please help how can i do this
Two easy ways:Throw all of the numbers into a better suited data structure (such as an std::map).
Sort the array and do some counting.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

we havent been taught any std::map something like that..just help me by telling me how to count the occurrence of nmumber 0-9 in that array

thanks

hydersha
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Make an int array that has 10 elements (so you have one for each digit). Up the count in that "bin" when you encounter a particular digit. Output your array.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

you dont need to use map for this you can simply use an array

here is an example.

#include <iostream>

using namespace std;

main(){     
            
       int array[20]; // loading 20 digits
       int count [10]={0};
       
       //loading digits
       for(int i=0; i<20; ++i)
               cin>>array[i];
          
       //counting        
       for(int i=0; i<20; ++i){
               
               if(array[i]>=0 && array[i]<10)
               count[array[i]]++;               
       } 
       
       //output
       for(int i=0; i<10; ++i)
               cout<<"Number of"<<i<<":"<<count[i]<<endl;
               
       system("pause");   

}
Jeronim
Light Poster
25 posts since Jan 2010
Reputation Points: 2
Solved Threads: 0
 

i understood all of this.. but this is not working properly..try entering 2 digit numbers in array than see the result

hydersha
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 
but this is not working properly


That's ok because he/she probably shouldn't have given the whole thing away. Think about how else you might store a group of digits...

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
i understood all of this.. but this is not working properly..try entering 2 digit numbers in array than see the result

Ofc it dont 'cause i made it to load 20 digits just to show you idea hwo to solve it now it's on to you i you want to improve it adding dynamic allocation.

you can also make on array of 1000 elements and set them to 10.

You are counting digits so the enters is invalid and you will be able to find the end of input and simply load it with get(array)

Jeronim
Light Poster
25 posts since Jan 2010
Reputation Points: 2
Solved Threads: 0
 

i know where the problem is in the abov code..the whole code is correct..and it is working fine for 1 digit numbers. the problem is in this line

if(array[i]>=0 && array[i]<10)

.. this condition checks for numebrs from 0-9.. i.e. 1 digit numbers only..
i cant figure out what to write to fix the problem..i tried alot right now

hydersha
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

Hint, don't take the digits in as numbers. Hold them in a char array or a string. Otherwise you can't hold spaces in an int array anyway.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
#include <iostream>
#include<stdio.h>
using namespace std;

main(){     
            
       int array[10]; 
       char count [10]={0};
       
       //loading digits
       for(int i=0; i<10; ++i)
               cin>>array[i];
          
       //counting        
       for(int i=0; i<10; ++i){
               
               if(array[i]>=0 && array[i]<10)
               count[array[i]]=count[array[i]]+1;               
       } 
       
       //output
       for(int i=0; i<10; ++i)
              
               printf("\nnumber of %d= %d",i,count[i]);
       system("pause");   

}


this is my code.. i tried storing in char array and print in integer but no use.. help!

hydersha
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

cin>>array[i]; remember that '\n' (like when you hit enter) is a character too. Look into using cin.getline() for this purpose to get the entire line of digits (up to the size of your char array).
-or-
Look into using getline(cin, ) with a std::string and you can avoid the size limitation.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
.. this condition checks for numebrs from 0-9.. i.e. 1 digit numbers only.. i cant figure out what to write to fix the problem..i tried alot right now

I thought that is what you need.

I have modified the code now it count all difits

Code:

#include <iostream>

using namespace std;

main(){     
            
       int array[20];
       int dif_numbers[10]={0}; 
       int count [10]={0}; 
       int dif=0;
       for(int i=0; i<10; ++i)
               cin>>array[i];
                 
       
       bool was_before=false; 
       for(int i=0; i<10; ++i){
               //reset
               was_before=false;
               //was_before ???
               for(int n=0; n<dif+1; ++n){
                              if(dif_numbers[n]==array[i])
                                 was_before=true;
               }
               //if wasn add   
               if(!was_before){
                                   dif_numbers[dif]=array[i];
                                   dif++;                
               }
               
               
               for(int j=i; j<10; ++j){  
                       if(was_before);
                       else if (dif_numbers[dif-1]==array[j])
                                count[dif-1]++;       
               }
                             
       } 
       
       //output
       for(int i=0; i<dif; ++i)
               cout<<"Number of"<<dif_numbers[i]<<":"<<count[i]<<endl;
               
       system("pause");   

}
Jeronim
Light Poster
25 posts since Jan 2010
Reputation Points: 2
Solved Threads: 0
 

Perhaps you did not get the message up there but please do NOT give the OP a full set of code. Help him or her through it but don't give the whole thing. This doesn't help him or her learn a thing.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 

s/he is using printf with using namespace std it just proves that s/he is beginner in these and s/he need to learn how to think in c/c++ to be able to solve task like these one and s/he wont learn that by reading some text but looking in code.

If s/he really want to understand it s/he will have to go through whole code add cout notes to understand what is relay happening there and that will help him/her understand how to solve task like these.

If i tell you to mix two colours and you will get third you will be able to do that but your will never be as good as real one if somebody dont show you how to make it.

Jeronim
Light Poster
25 posts since Jan 2010
Reputation Points: 2
Solved Threads: 0
 

Regardless, the general philosophy of this board is "we only give homework help to those who show effort" (meaning that the poster themselves should show the effort and receive help in proportion to their input).
Rather than writing the solution on your local machine and guiding this poster through it step by step you have handed him/her something that he/she could conceivably hand in for a grade. Trust me, darn near any experienced person could have done what you did for her and we didn't. There's a reason for that. The fact that you wanted to do it a second time after folks had expressed displeasure about it is kind of insulting.

Using your argument, I should never try to mix the colors myself because there is no value in that learning process. In doing so, again and again, you'll have to zip me through all the stages to get to the final product and then we can compare colors. I may know what the color looks like but I cannot get there on my own.

jonsca
Quantitative Phrenologist
Team Colleague
5,621 posts since Sep 2009
Reputation Points: 1,165
Solved Threads: 581
 
s/he is using printf with using namespace std it just proves that s/he is beginner in these and s/he need to learn how to think in c/c++ to be able to solve task like these one and s/he wont learn that by reading some text but looking in code.

Jonsca is correct. You are mistaken.

If you want to show everyone your prowess at coding, give an illustrative piece of code they can learn from. Do NOT write the code for them.

After a whopping 10 posts, you do not yet have the creds to argue with a lowly "Nearly a Posting Virtuoso" (no disrespect intended, Jonsca).

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 
the general philosophy of this board is "we only give homework help to those who show effort"

Why do you think it's homework may be s/he is just trying to learn something but don't have idea how to solve it.

Try to find some beginners c++ book you will find for loop explained you will also find arrays explained but in 90% of all books you will also find extra lesson popularly called sorting numbers, If it's all as you said why do we need it, it's just 2 for loops and one array we don't need it we could figure it out ourselves and yes we could but these is much faster and learn us how to think programming is not about learning language but about learning how to think. If you have learned to think you can learn any programming language in less than month.

These forum have many experienced programmers above all with lot of examples and i wont try to change something that you guys been creating for years giving your best and i respect philosophy you have chosen but sometimes you first need to see something from someone and than try yourself, at last it's just my opinion it doesn't have to be yours.

Jeronim
Light Poster
25 posts since Jan 2010
Reputation Points: 2
Solved Threads: 0
 
Why do you think it's homework may be s/he is just trying to learn something but don't have idea how to solve it.
  • Instructors do not write the answers to homework problems on the board before giving out an assignment. Only after the assignment is done.
  • Instructors frown upon copying answers from classmates preferring you to do your own work.
  • Instructors frown upon students asking other students to do homework for them.
  • Instructors generally don't mind if students help each other, short of turning in the exact same work
  • Students are expected to do their own work and ask questions that help them understand the material.
  • Plus more....

We are not their classmates. We therefore wish to abide by these expectations. They have been around for centuries. Who are you to say it's wrong?

... but sometimes you first need to see something from someone and than try yourself, at last it's just my opinion it doesn't have to be yours.

So give themsomething, just not the complete answer.

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

>s/he is using printf with using namespace std it just proves that s/he is beginner in these
There are experienced C++ programmers who prefer the stdio library over iostreams for various reasons.

>Why do you think it's homework may be s/he is just trying to
>learn something but don't have idea how to solve it.

That's certainly possible. However, due to the overwhelming number of students looking for a free ride, we prefer to assume that the question is homework and adjust our answers accordingly.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
 
View similar articles that have also been tagged: