Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Sunday, 28 April 2019

IOPP Program No.5.B

Program No.5.B 

Write a Perl script to find the average marks of each subject. The marks of the different subject along with the names of the students are maintained in a “result.txt” file.

Solution:

#!/usr/bin/perl

$filename="result.txt";
open(filedesc,$filename) or die "Can't open file.";

$avg=0;
$n=0;
$phytotal=0;
$chemtotal=0;
$mathtotal=0;

while(<filedesc>)
{
        @fields=split(':',$_);
        $physics=$fields[1];
        $chemistry=$fields[2];
        $maths=$fields[3];

        $phytotal=$phytotal+$physics;
        $chemtotal=$chemtotal+$chemistry;
        $mathtotal=$mathtotal+$maths;

        $n=$n+1;
}
$phyavg=$phytotal/$n;
print "Physics average=$phyavg\n";
$chemavg=$chemtotal/$n;
print "Chemistry average=$chemavg\n";
$mathavg=$mathtotal/$n;

print "Maths average=$mathavg\n";

0 comments:

Post a Comment