Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Benford’s law states that positive integers exhibit a certain behavior, if the n

ID: 671253 • Letter: B

Question

Benford’s law states that positive integers exhibit a certain behavior, if the numbers are truly random. For example, suppose the following numbers are collected at random (the -1 at the end denotes the end of the input):

Benford's law states that approximately 31% of the numbers will start with the digit '1'. In the example above, there are a total of 9 inputs, and 3 start with a '1' --- so 33% start with a 1. You can read more about Benford's law on Wikipedia. A practical application of Benford's law is the detection offraud.

Write a complete C++ program that inputs a collection of positive integers, followed by a -1, and then outputs the percentages of numbers that start with '1', '2', '3', ..., '9'. For example, given the following input:

your program should produce the following output:

Hint: input each integer as a string, not as an integer. Then access the first character of the string (remember a string is an array of characters) and see if that character is a '1', or a '2', or a '3', etc. Count each starting digit.

Explanation / Answer

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {

string input = "";

int myNumber = 0;
//float percentage=0.0;
int total=0,no_of_one=0,no_of_two=0,no_of_three=0,no_of_four=0,no_of_five=0,no_of_six=0,no_of_seven=0,no_of_eight=0,no_of_nine=0;
while (true) {
cout << "Please enter a valid number: ";
getline(cin, input);

// This code converts from string to number safely.
stringstream myStream(input);
if ((myStream < 0 ) && (myStream !=-1))
{     
cout << "Invalid number, please try again" << endl;
break;
}
if(myStream ==-1)
break;

total++;
if(input[0] ==1)
    no_of_one++;
if(input[0] ==2)
   no_of_two++;
if(input[0] ==3)
no_of_three++;
if(input[0] ==4)
   no_of_four++;
if(input[0] ==5)
   no_of_five++;
if(input[0] ==6)
no_of_six++;
if(input[0] ==7)
no_of_seven++;
   if(input[0] ==8)
no_of_eight++;
   if(input[0] ==9)
no_of_nine++;

}

cout << "Percentage of 1: " << ((no_of_one*100)/total) << endl << endl;
cout << "Percentage of 2: " << ((no_of_two*100)/total) << endl << endl;
cout << "Percentage of 3: " << ((no_of_three*100)/total) << endl << endl;
cout << "Percentage of 4: " << ((no_of_four*100)/total) << endl << endl;
cout << "Percentage of 5: " << ((no_of_five*100)/total) << endl << endl;
cout << "Percentage of 6: " << ((no_of_six*100)/total) << endl << endl;
cout << "Percentage of 7: " << ((no_of_seven*100)/total) << endl << endl;
cout << "Percentage of 8: " << ((no_of_eight*100)/total) << endl << endl;
cout << "Percentage of 9: " << ((no_of_nine*100)/total) << endl << endl;


return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote