Question

In this assignment you’re going to write a complete C program that opens a file “digits.txt”, inputs the values, and analyzes these values to see if they follow Benford’s law. 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 1, # of values that start with 2, etc. 3. A histogram of the counts for digits 1..9 4. Whether the input data follows Benford’s law (Yes or No) Here’s an example screenshot of your program output for the input shown earlier, assumed to be in the file digits.txt:

Number of inputs: 12 Counts: 3,2,2,1,1,0,1,1,1 1. 2. 3. 4. 5. 6: 7. 8. 9: Benfords law? No 25.00% 16.67% 16.67% 8.33% 8.33%

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.

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

Benford’s law is based on the first digit of each value. The easiest way to obtain the first digit is input the value as a string, and use [0] to obtain the first digit. This implies the digit will be a character, e.g. ‘2’, and not an integer 2.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

#include <iostream>

#include <cstdlib>

#include <string>

#include <fstream>

#include <cmath>

using namespace std;

int main()

{

string numbers[10000];

int n = 0, i, j;

string filename;

cout << "Enter filename: ";

cin >> filename;

ifstream in(filename.c_str());

if (in.fail())

{

cout << "Unable to open file!" << endl;

exit(0);

}

int count[10] = {0};

while (!in.eof())

{

in >> numbers[n];

int first_dig = numbers[n][0] - '0';

count[first_dig]++;

n++;

}

i = 0;

cout << "N: " << n << endl;

cout << "Counts: ";

while (i < 10)

{

cout << count[i];

if (i != 9)

cout << ",";

i++;

}

cout << endl;

i = 1;

bool law = true;

double prev_perc = 0;

while (i < 10)

{

cout << i << ": ";

j = 0;

double perc = ((double)count[i] / n) * 100;

while (j < (int)floor(perc))

{

cout << "*";

j++;

}

if (i == 0 && (perc < 28.0 || perc > 38.0))

law = false;

else if (i == 2 && (perc < 15.0 || perc > 21.0))

law = false;

else if (i == 3 && (perc < 10.0 || perc > 13.0))

{

law = false;

}

else if (prev_perc < perc)

{

law = false;

}

prev_perc = perc;

cout << "(" << perc << "%)" << endl;

i++;

}

cout << "Benfords's law? " << (law ? "Yes" : "No") << endl;

return 0;

}

Add a comment
Know the answer?
Add Answer to:
In this assignment you’re going to write a complete C program that opens a file “digits.txt”,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C Program Question: Write a program that reads all integers that are in the range of...

    C Program Question: Write a program that reads all integers that are in the range of 0 to 100, inclusive from an input file named: a.txt and counts how many occurrences of each are in the file. After all input has been processed, display all the values with the number of occurrences that were in are in the input file. Note: The program ignores any number less than 0 or greater than 100. Note: Do not display zero if a...

  • write a program in c++ Read an infix expression from an input file and convert to...

    write a program in c++ Read an infix expression from an input file and convert to postfix. Instead of displaying directly on the screen, first place in a queue, and then display the contents of the queue on the screen. Precondition: The expression will be read from a file (input.txt) that contains a single line. There will be no spaces between the operands and the operators. The following operators are allowed: ( ) + - * / The normal rules...

  • Write a C++ program that reads in a text file and writes the histogram of character counts sorted in alphabetical order to an output file. Eg, if the input file is “to be or not to be”, then the output should be: b **2 e **2 n *1 o ****4

    Write a C++ program that reads in a text file andwrites the histogram of character counts sorted inalphabetical order to an output file. Eg, if theinput file is “to be or not to be”, then the outputshould be:b   **2e   **2n   *1o   ****4r    *1t    ***3

  • 1. Write a C++ program that reads sides of a triangle a, b and c from a file and computes the are...

    1. Write a C++ program that reads sides of a triangle a, b and c from a file and computes the area of a triangle and counts total number of areas program has computed Program should have two functions sfun(): sfun receives the values of a, b and c then computes and returns the value ofs as areafun 0: areafun receives the value of s and computes and return the area of a triangle to main program using below formulae...

  • Write a program that reads a file containing an arbitrary number of text integers that are...

    Write a program that reads a file containing an arbitrary number of text integers that are in the range 0 to 99 and counts how many fall into each of eleven ranges. The ranges are 0-9, 10-19, 20-29,..., 90-99 and an eleventh range for "out of range." Each range will correspond to a cell of an array of ints. Find the correct cell for each input integer using integer division. After reading in the file and counting the integers, write...

  • The lab exercise this week (Week 03) was to write a program to compute the wing...

    The lab exercise this week (Week 03) was to write a program to compute the wing loading value (WLV), defined as the weight of an airplane (in kg) divided by its wing area (in m^2). Airplanes with low wing loading values are easy to maneuver but uncomfortable. Airplanes with high wing loading values are more comfortable, but less maneuverable. In this exercise, an engineer is trying to design an aircraft with a WLV in the range 290.0 - 310.0, inclusive....

  • C++ program Please only (if) (else) statements No while statements use Write a program that takes...

    C++ program Please only (if) (else) statements No while statements use Write a program that takes rate and outputs the employee's wages for the week input employee's hours worked and regular pay as an Calculate employee's wages as follows: She receives her regular rate for the an first 40 hours, 1.5 times her regular rate for each hour between 40 and 50 hours, and double her regular rate for each hour over 50. For invalid inputs, in addition to outputting...

  • Linux & Unix Write a bash program to indent the code in a bash source file....

    Linux & Unix Write a bash program to indent the code in a bash source file. Conditions:     The source file will contain only printing characters, spaces, and newlines. It is not necessary to check for invalid input.     The source file will not contain comments (words beginning with #). Requirements:     Read from standard input, write to standard output.     Code inside while statements should be indented 2 spaces. Be sure your program includes all of the following:    ...

  • please write C++ code to finish R0,R1,R2 Introduction In this assignment, you are going to develop...

    please write C++ code to finish R0,R1,R2 Introduction In this assignment, you are going to develop a "Schedule Book System that runs in the command line environment. The system stores the schedule of events input by user and allows users to add view or delete events. You can assume that the system can at most store 100 events Each group is required to write a Win32 Console Application program called ScheduleBook.cpp The requirements are listed below. RO When the program...

  • Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that...

    Overview: file you have to complete is WordTree.h, WordTree.cpp, main.cpp Write a program in C++ that reads an input text file and counts the occurrence of individual words in the file. You will see a binary tree to keep track of words and their counts. Project description: The program should open and read an input file (named input.txt) in turn, and build a binary search tree of the words and their counts. The words will be stored in alphabetical order...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT