How to write Benford’s Law in C++ Need Help
-
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
-
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
-
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
Hi, I suppose that you didn't expect the result you've got. Your program doesn't check the frequency of the first digit of the number but all the digits in the stream. You have to split your data set into numbers first and then take only the first digit of that number. Now there are several ways to split a number but the easiest is probably to use the getline function of a stream. The get line default uses the endline to seperate the lines but you can provide your own delimiter.
std::string sNumber;
std::stringstream numberStream = std::stringstream(strNum);
while (std::getline(numberStream , sNumber, ','))
{
++digit_frequency[sNumber[0]];
}This reduces the complexity of your program, which is good in industrial code but isn't when someone still needs to learn coding algorithms. I think your aim should be to write your own string split function/algorithm. regards
Learn from the mistakes of others, you may not live long enough to make them all yourself.
-
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
Create a "number box" that has 10 compartments, one for each digit 0-9.
For each number in your list
Check the first digit.
If it is a 0, increment the "0" compartment in the above-mentioned box.
If it is a 1, increment the "1" compartment in the above-mentioned box.
If it is a 2, increment the "2" compartment in the above-mentioned box.
...
Next// output statistics here
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous