Post a New Question

programming

posted by on .

Daily Life Magazine wants an analysis of the demographic characteristics of its readers. The Marketing Department has collected reader survey records containing the age, gender, marital status, and annual income of readers. Design an application that accepts reader data and produces a count of readers by age groups as follows: under 20, 20-29, 30-39, 40-49, and 50+

  • programming - ,

    Here's a little perl program which does the job. You can format and sort the output as needed.


    my $agegroup;
    while ($_ = <DATA>) {
    print "Enter age, sex, status, income: $_";
    my $age = (split)[0];
    my $ag = int($age/10);
    $ag = 1 if $ag < 2; # count 0-10 as teens (under 20)
    push @{$agegroup->{$ag}}, $_;
    }
    close DATA;
    foreach my $ag (sort keys %$agegroup) {
    my $agk = ($ag<2) ? "Under 20" : sprintf("%d-%d",10*$ag,10*$ag+9);
    my $cnt = @{$agegroup->{$ag}};
    print "$agk: $cnt\n";
    foreach my $rec (@{$agegroup->{$ag}}) {
    print " $rec";
    }
    }


    Here is the output from a sample run:


    Enter age, sex, status, income: 46 M M 37000
    Enter age, sex, status, income: 17 M D 36000
    Enter age, sex, status, income: 31 M S 164000
    Enter age, sex, status, income: 41 F D 295000
    Enter age, sex, status, income: 51 M M 198000
    Enter age, sex, status, income: 15 M M 55000
    Enter age, sex, status, income: 66 M S 73000
    Enter age, sex, status, income: 50 M D 161000
    Enter age, sex, status, income: 62 M S 96000
    Enter age, sex, status, income: 28 M D 232000
    Enter age, sex, status, income: 46 M D 92000
    Enter age, sex, status, income: 48 M S 186000
    Enter age, sex, status, income: 37 M D 100000
    Enter age, sex, status, income: 51 F D 193000
    Enter age, sex, status, income: 20 F D 202000
    Enter age, sex, status, income: 66 F M 264000
    Enter age, sex, status, income: 28 F S 280000
    Enter age, sex, status, income: 20 M M 33000
    Enter age, sex, status, income: 39 F S 280000
    Enter age, sex, status, income: 27 F S 187000
    Enter age, sex, status, income: 61 F M 272000
    Enter age, sex, status, income: 46 F M 41000
    Enter age, sex, status, income: 31 M S 305000
    Enter age, sex, status, income: 31 M M 145000
    Enter age, sex, status, income: 67 M M 313000
    Enter age, sex, status, income: 35 F M 235000
    Enter age, sex, status, income: 34 M S 106000
    Enter age, sex, status, income: 47 M D 182000
    Enter age, sex, status, income: 65 M S 90000
    Enter age, sex, status, income: 48 M S 273000
    Under 20: 2
    17 M D 36000
    15 M M 55000
    20-29: 5
    28 M D 232000
    20 F D 202000
    28 F S 280000
    20 M M 33000
    27 F S 187000
    30-39: 7
    31 M S 164000
    37 M D 100000
    39 F S 280000
    31 M S 305000
    31 M M 145000
    35 F M 235000
    34 M S 106000
    40-49: 7
    46 M M 37000
    41 F D 295000
    46 M D 92000
    48 M S 186000
    46 F M 41000
    47 M D 182000
    48 M S 273000
    50-59: 3
    51 M M 198000
    50 M D 161000
    51 F D 193000
    60-69: 6
    66 M S 73000
    62 M S 96000
    66 F M 264000
    61 F M 272000
    67 M M 313000
    65 M S 90000

Answer This Question

First Name:
School Subject:
Answer:

Related Questions

More Related Questions

Post a New Question