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.

>please help how can i do this
Two easy ways:

  1. Throw all of the numbers into a better suited data structure (such as an std::map).
  2. Sort the array and do some counting.

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

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.

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

}
commented: Thanks for doing the OP's homework for him. -4

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

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...

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)

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>=0 && array<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

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.

#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!

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.

.. 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");   

}

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.

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.

commented: Don't justify giving code with stupidity. If you really believe that, give illustrative code, not ANSWERS -2
commented: G.A.Y -2

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.

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).

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.

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.

  1. Instructors do not write the answers to homework problems on the board before giving out an assignment. Only after the assignment is done.
  2. Instructors frown upon copying answers from classmates preferring you to do your own work.
  3. Instructors frown upon students asking other students to do homework for them.
  4. Instructors generally don't mind if students help each other, short of turning in the exact same work
  5. Students are expected to do their own work and ask questions that help them understand the material.
  6. 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 them something, just not the complete answer.

>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.

#include <iostream>
#include <string>
using namespace std;

int main()
{
	char str[100];
	int digits[10]={0};

	cout<<"Enter a number or a string of numbers seperated by a space"<<endl;
	gets(str);

	for(int i=0;i<strlen(str);++i)
		if(str[i]!=' ')
			digits[str[i]-'0']++;

       cout<<endl;
	for(int i=0;i<10;++i)
		cout<<"Digit: "<<i<<"\tCount: "<<digits[i]<<endl;

	return 0;
}
commented: Read the post before responding +0

@masterovpuppetz: Did you happen to catch any of the discussion above??

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.