help with my assignment

hello again!
i want to write a program that gets a number (int) from the user and brings back the summary of digits that are in the number. for expamle:
if the user insert 23901
he will get back
0: 1 times
1: 1 times
2: 1 times
3: 1 times
9: 1 times.

so if the digit '8' does not appear in the number, it won't be in the list...
please tell me what should i add to my code in order that it won't show the summary of digits that aren't is the number :-)

for the begining, i wrote my code to check only for zero:
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
#include <stdio.h>

int function2 (int num)
{
	int counter=0, result;
	for (result=0; num>0;)
	{
		result = num % 10;
		num = num/10;
			if (result == 0)
				counter = counter + 1;
	}
	return (counter > 0 ? counter :0);
	
}		


int main ()
{
	int  num;
	printf("please enter a number :\n");
	scanf("%d", &num);

	printf("%d\n", function2(num));





	return 0;
}
One thing I did when writing a program to keep track of lottery numbers was use an array to keep track of the number of times a value appeared. Every time it was read in I incremented the element that corresponded to that digit by one. Now it's true that that could waste a bit of space, it was also the simplest solution that I could see. So you could create an array of size 10 and each element would represent a different digit. Then, instead of the if statement, you could simply use something like this:

1
2
3
4
5
for (result=0; num>0;)
{
	result = num % 10;
	array[result]++;
}


Then, to output skipping 0's you could do something like this:

1
2
3
4
5
6
7
for (int i = 0, i < 10, i++)
{
    if (array[i] == 0)
        continue;
    else
        cout << i << ": " << array[i] << " times."
}
Last edited on
i have no idea what the array is, and i can't really use it in my assignment .
do you have another solution (it can be long...)
You are using functions but don't know what arrays are? That seems kind of odd. Only other way really is to use 10 different variables then, which really is what an array is, except arrays basically keep the variables in consecutive memory and give you an easier way to access the data.
Topic archived. No new replies allowed.