Question

In: Computer Science

The accompanying 2 files (boynames.txt and girlnames.txt) contain the results of a recent census of boy...

The accompanying 2 files (boynames.txt and girlnames.txt) contain the results of a recent census of boy and girl births in the last year. Each record of the files contain a given name and the number of new born children receiving that name. E.g.,

          Matthew 23567          or            Alison 17658

Each name is separated from the number by a blank space.

There are also some common names given to both boys and girls, e.g., Riley. They will appear in both the boy file and the girl file.

Write a PHP script that finds answers to the following questions.

  1. How many names are common to both boys and girls?
  2. List the common names with how many boys and how many girls have that name. Use an HTML table to display this data.
  3. How many total boys and how many total girls were born last year according to the census?

Solutions

Expert Solution

The code for above question is:

<?php 
// saving the file pointer of the girls
$girlnames = fopen("girlnames.txt", "r") or die("Unable to open file!"); 
// saving the file pointer of the boys
$boynames = fopen("boynames.txt", "r") or die("Unable to open file!");
$total_girls = 0;
while(!feof($girlnames)) {
        // fgets() get input line by for the file
        // explode splits the input in array of the form ['name', 'count']
        $single_girl = explode(" ", fgets($girlnames));
        // storing as a key value pair where $girl['name'] = 'count'; 
        $girl[$single_girl[0]] = $single_girl[1]; 
        // get the total number of girls by adding the count
        $total_girls = $total_girls + (int)($single_girl[1]); 
}
$total_boys = 0;
while(!feof($boynames)) {
        // fgets() get input line by for the file
        // explode splits the input in array of the form ['name', 'count']
        $single_boy = explode(" ", fgets($boynames));
        // storing as a key value pair where $boy['name'] = 'count'; 
        $boy[$single_boy[0]] = $single_boy[1];
        // get the total number of boys by adding the count
        $total_boys = $total_boys + (int)($single_boy[1]);
}

$count = 0;
// getting the table ready for the output
$data = "<table><tr><th>Name</th><th>Girl count</th><th>Boy count</th></tr>";
foreach ($girl as $key => $value) {
        // name present in both girl and boy
        if (array_key_exists($key, $boy)) { 
                $count++; // count of common names
                // apending the result for the table data
                $data = $data."<tr><td>".$key."</td><td>".$value."</td><td>".$boy[$key]."</td></tr>";
        }
}
// ending with /table
$data = $data."</table>"; 
// printing output as required
echo("The total common names between boys and girls are : <b>".$count."</b><br>");
echo($data);
echo("The total number of girls born last year : <b>".$total_girls."</b><br>");
echo("The total number of boys born last year : <b>".$total_boys."</b><br>");
?>

girlnames.txt:

Reny 235222
Martha 123222
Riley 122212
Aarnav 12332

girlnames.txt(image):

boynames.txt:

Alison 17623
Aarnav 23433
Riley 12423

boynames.txt(image):

Output for the given boynames.txt and girlnames.txt:


Related Solutions

The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. DebugBox.java public class DebugBox { private int width; private int length; private int height; public DebugBox() { length = 1; width = 1; height = 1; } public DebugBox(int width, int length, height) { width = width; length = length; height =...
The files provided in the code editor to the right contain syntax and/or logic errors. In...
The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Please Fix code and make Copy/Paste avaliable // Application allows user to enter a series of words // and displays them in reverse order import java.util.*; public class DebugEight4 {    public static void main(String[] args)    {       Scanner input = new Scanner(System.in);       int...
The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that...
The files SpeedControl.java and SpeedControlPanel.java contain a program (and its associated panel) with a circle that moves on the panel and rebounds from the edges. The Circle class is in the file Circle.java. Save the program to your directory and run it to see how it works. In this lab exercise you will add to the panel a slider that controls the speed of the animation. 1. Set up a JSlider object. You need to: a) Declare it. b) Instantiate...
2. (25) My sons (“Boy 1” and “Boy 2”) are negotiating over how to divide a...
2. (25) My sons (“Boy 1” and “Boy 2”) are negotiating over how to divide a pile of 20 chocolates. Boy 1 will engage Boy 2 in up to three rounds of negotiations. The order of events is: FIRST ROUND: Boy 1 makes Boy 2 an initial offer. Boy 2 accepts or rejects. If he accepts, the game ends and the two boys get their chocolates. If Boy 2 rejects, I punish them for not working together by eating 5...
According to recent census data, What is the predominant family structure in Canada?
According to recent census data, What is the predominant family structure in Canada?
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain...
Please use C++. You will be provided with two files. The first file (accounts.txt) will contain account numbers (10 digits) along with the account type (L:Loan and S:Savings) and their current balance. The second file (transactions.txt) will contain transactions on the accounts. It will specifically include the account number, an indication if it is a withdrawal/deposit and an amount. Both files will be pipe delimited (|) and their format will be the following: accounts.txt : File with bank account info...
Use the data in the accompanying table that summarizes results from a clinical trial of a...
Use the data in the accompanying table that summarizes results from a clinical trial of a drug. Let A = Drug Treatment, B = Placebo headache No Headache total Drug Treatment (A) 122 585 707 Placebo ( B) 28 672 700 Total 150 1257 1407 Answer the following: 1. Find the probability that patient under treatment p(A) 2. Find p(B) 3. Find the probability a patient has a headache 4. Find the probability a patient has a headache and he...
A recent census asked respondents to state the highest level of education that they had received....
A recent census asked respondents to state the highest level of education that they had received. The responses were 39% High school, 45% TAFE or Undergraduate Degree, 13% Higher Degree and 3% Other. The mayor of a small country town held a similar survey. Below are observed counts for each of the education levels for 250 respondents from the town: Highest Education Level Highest education level Observed count High school 115 Tafe or uni 105 Higher degree 20 Other 10...
A recent census report indicated the following percentages for methods of commuting to work for workers...
A recent census report indicated the following percentages for methods of commuting to work for workers over 15 years of age: 76.6% drive alone, 9.7% carpool, 4.9% use public transportation, 2.8% walk, 1.7% use other forms of transportation, 4.3% work at home. A random sample of workers yielded the following table of observed methods of commuting to work. Method Alone Carpool Public Walk Other Home Observed 355 65 30 20 10 20 Is there sufficient evidence to conclude that the...
Suppose​ that, according to a recent​ census, the income per capita measured in U.S. dollars was...
Suppose​ that, according to a recent​ census, the income per capita measured in U.S. dollars was ​$42 comma 793 in country A and ​$46 comma 210 in country B. Assume that income per capita is Normally distributed with a standard deviation equal to 29​% of the mean for each country. A random sample of eight people in country A and eight people in country B is selected. a) What is the probability that the mean income of the sample from...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT