You are on page 1of 3

3. Assume you need to generate sufficient data to test your design.

Write a data generator in


Perl. It should take the description of the desired data format as its input and generate a set of
random data conforming to that format.

CODE:
print "How many i/p does your Design have: ";
chomp ($ip_no=<STDIN>);
print "How many set of data you want to genarate: ";
chomp ($set_no=<STDIN>);

for ($i=1;$i<=$ip_no;$i++)
{
print "\nPlease enter the no. $i input's name: ";
chomp ($ip_name[$i]=<STDIN>);
print "\nPlease enter the no. $i input's size(In Bits): ";
chomp ($ip_size[$i]=<STDIN>);
}

print "\nRandom value are:\n";


for ($j=1;$j<=$#ip_size;$j++)
{
$temp = ( 2 ** $ip_size[$j] );
for ($k=0;$k<=$set_no;$k++)
{
$random_number = rand($temp);
printf "@ip_name[$j]\[@ip_size[$j]\]: %b\n", $random_number;
}
}

OUTPUT:
4. Write a Perl program so that when it is executed, it should get the information like names and marks of "N"
number of students from the user through the standard input. Value of N (Number of students) should be passed
through command line argument. Make an associative array that has a list of all the names of the students and their
marks. Also write a subroutine so that it calculates the average marks of all the entries in the associative array.

CODE:

($in)=@ARGV[0];
for ($i=0;$i<$in;$i=$i+1)
{
$no=$i+1;
print "Please enter No.$no student's name: ";
chomp($name=<STDIN>);
print "Please enter $name 's Mark: ";
chomp($mark=<STDIN>);
$students{$name}=$mark;
}

@names=keys%students;
@marks=values%students;

print "\n\nNAMES\t\tMARKS\n---------------------\n";

for ($j=0;$j<$in;$j=$j+1)
{
print "@names[$j]\t\t@marks[$j]\n";
}
print "\n";
avg_marks();
print "\n";

sub avg_marks
{
$sum=0;
for ($i=0;$i<$in;$i=$i+1)
{
$value = @marks[$i];
$sum=$sum+$value;
}
$avg=$sum/$in;
printf "Avg marks of all students is: %.2f\n",$avg;
}

OUTPUT:

You might also like