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

In this assignment you’re going to write a complete C++ program that inputs a fi

ID: 3740005 • Letter: I

Question

In this assignment you’re going to write a complete C++ program that inputs a filename, opens this file, inputs the values, and analyzes these values to see if they follow Benford’s law. DO NOT USE ARRAYS. In particular, your program analyzes the first digit of each input value, and outputs:

1. The total # of input values N

2. The counts for each digit: # of values that start with 0, # of values that start with 1, etc.

3. A histogram of the counts for digits 1..9

4. Whether the input data follows Benford’s law (Yes or No)

The input file will contain at most 10,000 positive integers, one per line. For the histogram output of *’s, note that the # of *’s is the floor of the percentage; e.g. the percentage 16.6667% yields 16 *’s. The best way to compute the floor of a real number is to use the built-in floor function available by #include <cmath>.

For the purposes of this assignment, assume that a set of values adheres to Benford’s law if all of the following are true:

a. The % of 1’s falls in the range 28.0 .. 38.0, inclusive

b. The % of 2’s falls in the range 15.0 .. 21.0, inclusive

c. The % of 3’s falls in the range 10.0 .. 13.0, inclusive

d. The % of 4’s is strictly less than the % of 3’s

e. The % of 5’s is strictly less than the % of 4’s

f. The % of 6’s is strictly less than the % of 5’s

g. The % of 7’s is strictly less than the % of 6’s

h. The % of 8’s is strictly less than the % of 7’s

i. The % of 9’s is strictly less than the % of 8’s

The file contains the following numbers:

34

0123

22184

123456

9811

7812

123

12345

51613

2239

31998

43

The output of the data should be:

N: 12

Counts: 1,3,2,2,1,1,0,1,0,1

1: ************************* (25%)

2: **************** (16.6667%)

3: **************** (16.6667%)

4: ******** (8.33333%)

5: ******** (8.33333%)

6: (0%)

7: ******** (8.33333%)

8: (0%)

9: ******** (8.33333%)

Beneford's law? No

Explanation / Answer

#include <iostream>
#include <fstream>
#include <windows.h>
#include <cmath>
using namespace std;
int analyzeData(string fname);
int first(int num);
int main()
{
    fstream file;
    string filename;
    filename = "benford.txt";
   if(analyzeData(filename) != 0)
        cout << "Error reading benford.txt ";
    system("PAUSE");
    return EXIT_SUCCESS;
    return 0;
}
int analyzeData(string fname)
{
    fstream infile;
    infile.open(fname.c_str());
    int i;
    int tmp,count1 = 0,count2 = 0,count3 = 0,count4 = 0,count5 = 0,count6 = 0,count7 = 0,count8 = 0,count9 = 0,count0=0,count=0;
float per1,per2,per3,per4,per5,per6,per7,per8,per9;
    if(!infile.good())
        return 1;
    while(!infile.eof())
    {
        count++;
        infile >> tmp;
        tmp = first(tmp);

        if(tmp==1)
        {
          count1++;
          cout<<"*";
        }

        else if (tmp==2)
        {

           count2++;
           cout<<"*";
        }
        else if (tmp==3)
        {

            count3++;
            cout<<"*";
        }
        else if (tmp==4)
        {

           count4++;
           cout<<"*";
        }
        else if (tmp==5)
        {

           count5++;
           cout<<"*";
        }
        else if (tmp==6)
        {

            count6++;
            cout<<"*";
        }
        else if (tmp==7)
        {

             count7++;
             cout<<"*";
        }
        else if (tmp==8)
        {

        count8++;
        cout<<"*";
        }
        else if (tmp==9)
        {
            count9++;
            cout<<"*";
        }
        else
            count0++;

    }
per1=(float)count1/count*100;
per2=(float)count2/count*100;
per3=(float)count3/count*100;
per4=(float)count4/count*100;
per5=(float)count5/count*100;
per6=(float)count6/count*100;
per7=(float)count7/count*100;
per8=(float)count8/count*100;
per9=(float)count9/count*100;

    cout<<" Total count = "<<count<<endl;

    cout<<" 1 : ";
    for(i=1;i<floor(per1);i++)
        cout<<"*";
    cout<<"("<<per1<<"%)"<<endl;
    cout<<" 2 : ";
    for(i=1;i<floor(per2);i++)
        cout<<"*";
    cout<<"("<<per2<<"%)"<<endl;
    cout<<" 3 : ";
    for(i=1;i<floor(per3);i++)
        cout<<"*";
    cout<<"("<<per3<<"%)"<<endl;
    cout<<" 4 : ";
    for(i=1;i<floor(per4);i++)
        cout<<"*";
    cout<<"("<<per4<<"%)"<<endl;
    cout<<" 5 : ";
    for(i=1;i<floor(per5);i++)
        cout<<"*";
    cout<<"("<<per5<<"%)"<<endl;
    cout<<" 6 : ";
    for(i=1;i<floor(per6);i++)
        cout<<"*";
    cout<<"("<<per6<<"%)"<<endl;
    cout<<" 7 : ";
    for(i=1;i<floor(per7);i++)
        cout<<"*";
   cout<<"("<<per7<<"%)"<<endl;
    cout<<" 8 : ";
    for(i=1;i<floor(per8);i++)
        cout<<"*";
    cout<<"("<<per8<<"%)"<<endl;
    cout<<" 9 : ";
    for(i=1;i<floor(per9);i++)
        cout<<"*";
    cout<<"("<<per9<<"%)"<<endl;

   
      return 0;
}

int first(int num)
{
    int rt;
    if(num == 0)
       return -1;
    while( 1 )
    {
        if(num / 10 == 0)
        {
            rt = num;
            return rt;
        }
        num /= 10;
    }
}