digit counter

Nov 28, 2010 at 6:35pm
hi everyone im new to c++ and sorry for my english.i want to make a basic program which count digits in number.after program starts we enter 5 number.after then program counts digits in number and shows digits used how many times in number.
dos screen must seem lik this:

enter integers between 1-5:
1
12
123
1234
12345
//we enter these numbers after program start

1:5 times
2:4 times
3:3 times
4:2 times
5:1 times

enter integers between 1-5:
_

i would be delighted if you help me.
thanks in advance:)



Nov 28, 2010 at 10:41pm
Please share with us after you try to write code.

As an idea:

From 0 up to 9. Create an array of numbers.
You can count the number that you enter the digits.
Nov 29, 2010 at 11:20am
i thought like this firix.but the program doesnt work.i dont understand my fault. i will be glad if you help me

#include <iostream.h>

#include <stdlib.h>

using namespace std;
main()

{

int array[10]={0,1,2,3,4,5,6,7,8,9};
int left=0,right=9,middle;
int number,i;
bool sw=false;


cout<<"ARRAY: ";

for (i=0;i<=9;i++)
{
cout<<array[i]<<";";
}
cout<<" \n"<<"Number to find: ";
cin>>number;
while (sw == false && left <= right)
{
middle=(left+right)/2;
if (number == array[middle])
{
sw=true;
cout<<"number found.\n";
}
else
{
if (number < array[middle]) right=middle-1;
if (number > array[middle]) left=middle+1;
}
}
if (sw == false) cout<<"number not found.\n";
system("Pause");
}
Nov 29, 2010 at 1:20pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int main()
{
	map<int, int> imap;
	int result;
	int value = 49549995;


	imap[0] = 0;
	imap[1] = 0;
	imap[3] = 0;
	imap[4] = 0;
	imap[5] = 0;
	imap[6] = 0;
	imap[7] = 0;
	imap[8] = 0;
	imap[9] = 0;

	while (value / 10) {
		result = value % 10;
		value /= 10;
		for (map<int, int>::iterator iter = imap.begin(); iter != imap.end(); iter++)
			if (iter->first == result)
				iter->second++;
	}


	for (map<int, int>::iterator iter = imap.begin(); iter != imap.end(); iter++)
		if(value == iter->first)
			iter->second++;

	

	for (map<int, int>::iterator iter = imap.begin(); iter != imap.end(); iter++)
		cout << iter->first << ":" << iter->second << endl; 
	


	return 0;
}



I hope this helps you.
Nov 29, 2010 at 2:55pm
If you are going to use a map:
1
2
3
4
5
/* traverse a map/set is O(n log n) (or at least O(n) )
for (map<int, int>::iterator iter = imap.begin(); iter != imap.end(); iter++)
			if (iter->first == result)
				iter->second++;*/
imap[result]++; // binary search O(log n)  


i want to make a basic program which count digits in number
But the code you post is binay search
Nov 29, 2010 at 5:07pm
@ne555

please
you type in this code.
Nov 29, 2010 at 8:16pm
My try :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
using namespace std;

int main()
{
	
	int num[5]={0,0,0,0,0};
	int y=0;
	char x;

	do { //Loops while x is equal or higher then 1 and equal or lower then 5
		cin >> x;
/*		x == '1'?num[0]++:num[0]==num[0];//if x = 1 num[0]adds 1
		x == '2'?num[1]++:num[1]==num[1];//if x = 2 num[1]adds 1
		x == '3'?num[2]++:num[2]==num[2];//if x = 3 num[2]adds 1
		x == '4'?num[3]++:num[3]==num[3];//if x = 4 num[3]adds 1
		x == '5'?num[4]++:num[4]==num[4];//if x = 5 num[4]adds 1	
can obviously be simplified as ne555 says in the folowing post as:*/	
		num[ x-'1' ]++;
			}
	while ( x>='1',x<='5');	

	cout << "calculating ...\n";//Loop ended, displays the amount of each num occured
	cout << "1 occured " << num[0] << " times.\n";
	cout << "2 occured " << num[1] << " times.\n";
	cout << "3 occured " << num[2] << " times.\n";
	cout << "4 occured " << num[3] << " times.\n";
	cout << "5 occured " << num[4] << " times.\n";	

	cin.get();
	cin.get();//pause
return 0;
}


Hope this helped^^

btw if someone knows a alternative for the 0,0,0,0,0 in this line: int num[5]={0,0,0,0,0}; pls tell me :)
seems like I get a error if i didn't type it
kinda want to assign every variable in the array to 0 :)
Last edited on Nov 29, 2010 at 10:28pm
Nov 29, 2010 at 8:30pm
fantastic :)
Nov 29, 2010 at 10:11pm
too obfuscated
x == '1'? num[0]++: num[0]==num[0] /*statement has no effect */;
1
2
cin >> x;
num[ x-'1' ]++;
Nov 29, 2010 at 10:30pm
Thanks for pointing me to that ^^, would have took me a while to figure that out myselve :)
edited post with your code
Nov 30, 2010 at 7:47am
It does not matter ^^
Topic archived. No new replies allowed.