Benford’s Law C++

How to write a code for Benfor's Law.
program that loop through the list of numbers and count how many times 1 is the
first digit, 2 is the first digit, etc. For each digit, output the percentage it appears as the first digit.
The trivial thing is to divide an integer number by 10 until the number is less than 10. Floating point values less than 1 require multiplication in similar manner.
You can also use sprintf, and check here is the sample program

1
2
3
4
5
6
7
8
9
10
11
12
// couter for each digit
int digit_counter[9] = { 0th will have no of 1's }

// some random num from the list
num = 10;

// char buffer for 
char buffer[16];

sprintf(buffer, "%d", num);

++digit_counter[buffer[0] - '0']; 


I have not tested but it should work
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const int base = 10;

int all_digits[base] = {};

while ( true )
{
   std::cout << "Enter a number (0 - exit): ";

   int number = 0;
   std::cin >> number;

   if ( !number ) break;

   int digit;
   do { digit = number % base } while ( number /= base );

   all_digits[digit]++;
}
Thanks for the code, but I am just new to C++ and you two explain more, I will appreciate it.
Here is more details. I know it's not hard for some people but to me, it takes a lot of my time writting code(1-10hrs) I am new to C++ and I am new comer, Please Help. Here is more details:

It might seem like the leading digit of each number in the list should

be 1–9 with an equally likely probability. However, Benford’s Law states that

the leading digit is 1 about 30% of the time and drops with larger digits. The

leading digit is 9 only about 5% of the time. Write a program that tests

Benford’s Law. Collect a list of at least one hundred numbers from some real-life data source and enter them into a text file.

Your program should loop through the list of numbers and count how many times 1 is the

first digit, 2 is the first digit, etc. For each digit, output the percentage it appears

as the first digit. If you read a number into the string variable named strNum then you can access

the first digit as a char by using strNum[0] .


Thanks for help, I am very appreciated for helping me a step for.
A programming course homework? You do learn by doing.

Now you have an excellent chance to learn. Please explain each line in vlad's code sample (which is not a complete program) as well as you can. Yes, you are not familiar with all the details yet, but if you don't try, you will never be.
I would use std::stringstream and output the integer to it. Then take the std::string returned by the std::stringstream::str method and look at the first character. If the first character is a negative sign, it would be skipped. If the first character was a number (after optional skipping), then I would have an std::map<int, int> be increased by one corresponding to the number digit.
The percentages would all be calculable at that point. I will go ahead and type some of this code to help you, but it is a homework assignment so do not expect too much free code.

Here is a link to ideone where I typed out a program that can count all the first digits from all the numbers in a vector.
http://ideone.com/0l9oks

It would be easy to modify it to have string variables instead of integer variables.
Last edited on
Topic archived. No new replies allowed.