Here is the description:
Consider lists of numbers from real-life data
sources, for example, a list containing the number of students enrolled in different
course sections, the number of comments posted for different Facebook status
updates, the number of books in different library holdings, the number of votes per
precinct, etc. 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] .
And Here is how I got so far:
Programming Language C++ (I just take this class this F2013)
#include
#include
#include
#include
#include
#include
int main(int argc, char** [0])
{
std::string strNum
//this just a random # I put in here, I want digit 1-9 NOT ZERO
("15157,83049,1193049090,1289727392,9075,54984,5419981,231540,1498056");
std::map digit_frequency;
for(int i = 0; i < strNum.size(); ++i)
{
std::stringstream ss;
ss << strNum[i];
std::string str = ss.str();
if(isdigit(str[0]))
{
++digit_frequency[str[0]];
}
else if(isdigit(str[1]))
{
++digit_frequency[str[1]];
}
}
std::map::iterator it;
for(it = digit_frequency.begin(); it != digit_frequency.end(); it++)
{
std::cout << "Leading Digit " << it->first << ": repeated " << it->second << " time(s).\n";
}
return 0;
}
output:
click the link to see the output.
http://www.facebook.com/#!/photo.php?fbid=10200531325265379&set=a.1645092726942.2077529.1225598765&type=1&theater
KingLord