Question

In: Computer Science

Convert the following code from awk to Perl: (please don't use a2p (awk to perl) command)...

Convert the following code from awk to Perl: (please don't use a2p (awk to perl) command)

Here was the original problem:

Calculate the grade for each student that appears in the data file. You may calculate the grade based on total earned points, divided by total possible points. Or, for 10 points extra credit; Use the weighted totals for this course; available in the syllabus and on the course home page.

Output a list of students, their grade as a percentage and a letter grade. For simplicity; use this letter grade scale;

  • A = 90 >= grade <= 100
  • B = 80 >= grade < 90
  • C = 70 >= grade < 80
  • D = 60 >= grade < 70
  • E = grade < 60

The following sample output has been provided as a guide. Your solution must be based on the data in the Lab03-data.csv file. No points will be awarded to solutions that are based on copy-pasting the sample output and printing it with awk.

Name    Percent Letter
Andrew  75.21   C
Chelsey 92.21   A
Shane   77.64   C
Ava     79.76   C
Sam     62.34   D

Here is some of the sample data from the file (Lab03-data.csv)

Student Catehory Assignment Score Possible
Sam Quiz Q07 68 100
Sam Final FINAL 58 100
Sam Survey WS 5 5
Andrew Homework H01 25

100

Here is the AWK code that needs to be converted to PERL

BEGIN{
FS = ","
print "Name\tPercent\tLetter"
a=0
}

{

if(a==0){
   a+=1
}

else{
   sum[$1$2] += $4
   total[$1$2] += $5
   students[$1]++
   categories[$2]++

}
}

END{

#for(b in sum)
#   percent=(sum[b]/total[b])*100
for (b in students){
    Homework=(sum[b"Homework"]/total[b"Homework"])*0.10
        Lab=(sum[b"Lab"]/total[b"Lab"])*.30
        Final=(sum[b"Final"]/total[b"Final"])*.15
        Quiz=(sum[b"Quiz"]/total[b"Quiz"])*.40
        Survery=(sum[b"Survey"]/total[b"Survey"])*.05
        percent=(Homework+Lab+Final+Quiz+Survery)*100
   printf "%s\t%.2f\t",b,percent
   if(percent>=90 && percent<=100)
       print "A";

   else if(percent>=80 && percent<90)
       print "B";

   else if(percent>=70 && percent<80)
       print "C";

   else if(percent>=60 && percent<70)
       print "D";

   else
       print "E"

}
}

Solutions

Expert Solution

Answer:

Note:

  1. The code is designed by using online tools and the execution is also performed by using online tools.
  2. The .csv file contains 7-Homework scores, 7-Lab scores, 7-Quiz scores, 1-Final Score, and 1-WS score. The average is calculated in general format.
  3. For extra grade purpose, the weighted totals for the each test are not provided. So, simple average calculations were shown in the program code. If possible try to provide the weighted totals for extra credit points.

Program Code Screen Shot:

Sample Output:

Sample Input Data: Lab03-data.csv

Student,Catehory,Assignment,Score,Possible
Chelsey,Homework,H01,90,100
Chelsey,Homework,H02,89,100
Chelsey,Homework,H03,77,100
Chelsey,Homework,H04,80,100
Chelsey,Homework,H05,82,100
Chelsey,Homework,H06,84,100
Chelsey,Homework,H07,86,100
Chelsey,Lab,L01,91,100
Chelsey,Lab,L02,100,100
Chelsey,Lab,L03,100,100
Chelsey,Lab,L04,100,100
Chelsey,Lab,L05,96,100
Chelsey,Lab,L06,80,100
Chelsey,Lab,L07,81,100
Chelsey,Quiz,Q01,100,100
Chelsey,Quiz,Q02,100,100
Chelsey,Quiz,Q03,98,100
Chelsey,Quiz,Q04,93,100
Chelsey,Quiz,Q05,99,100
Chelsey,Quiz,Q06,88,100
Chelsey,Quiz,Q07,100,100
Chelsey,Final,FINAL,82,100
Chelsey,Survey,WS,5,5
Sam,Homework,H01,19,100
Sam,Homework,H02,82,100
Sam,Homework,H03,95,100
Sam,Homework,H04,46,100
Sam,Homework,H05,82,100
Sam,Homework,H06,97,100
Sam,Homework,H07,52,100
Sam,Lab,L01,41,100
Sam,Lab,L02,85,100
Sam,Lab,L03,99,100
Sam,Lab,L04,99,100
Sam,Lab,L05,0,100
Sam,Lab,L06,0,100
Sam,Lab,L07,0,100
Sam,Quiz,Q01,91,100
Sam,Quiz,Q02,85,100
Sam,Quiz,Q03,33,100
Sam,Quiz,Q04,64,100
Sam,Quiz,Q05,54,100
Sam,Quiz,Q06,95,100
Sam,Quiz,Q07,68,100
Sam,Final,FINAL,58,100
Sam,Survey,WS,5,5

Program Code to Copy:

#!/usr/bin/perl
use strict;
use warnings;

# open the csv file from the command line
open (my $inFile, '<', $ARGV[0]) or die $!;

# get the header of the file
my $header=<$inFile>;   #First line is read here

# declare and initialize the iterative variable
my $i =0;

# define the hash variable
my %data = ();

# define an array to hold the assignment names
my @testNames;

# loop through each line in the file
while (my $line = <$inFile>)
{
    # record separator
    chomp $line;
  
    # split the line at delimiter ','
    # and set the values into the array called
    # fields
    my @fields = split "," , $line;
  
    # from the array of fields, get the assignment Name
    # set it to array testNames at index i
    $testNames[$i] = $fields[0];
  
    # push the values of the respective score and possible scores
    # as list into hash data
    push (@{$data{$testNames[$i]}}, ($fields[3], $fields[4]));
  
    # increment the iterative variable
    $i = $i + 1;
}

# close the file
close $inFile;

# print the header
printf("%-8s %8s \t %s \n", "Name", "Percent", "Letter");

# loop through each key and its respective data
foreach my $key ( sort keys %data )
{
   # get the values at each key(Assignment)
    my @eachListValues = @{ $data{$key}};

    # set the total to 0
    my $total = 0;
  
    # set the $possibleTotal to 0
    my $possibleTotal = 0;
  
    # set the average value to 0
    my $average = 0;
  
    # initialize to zero
    $i = 0;
  
    # loop through each value in the array list
    # of each assignment
    foreach my $val (@eachListValues)
    {
        # since there are two values stored at a time
        # that is score and its respective possible score
        # score values, so, in the even positions, the score
        # is stored and at odd positions, the possible scores
        # are stored.
      
        # if i is even, then add val to the total
        if($i % 2 == 0)
        {
             # total the $val
            $total += $val;
        }
      
        # if i is odd, then add val to the possibleTotal
        else
        {
            $possibleTotal += $val;
        }
     
        # increment the i variable
        $i += 1;
    }
  
    # find the average of each student
    $average = ($total / $possibleTotal) * 100;
  
    # define the grade variable to hold the
    # grade value
    my $grade="";
  
    # conditions to check if the average is
    # within 90-100 set the grade to "A"
    if( $average >= 90 && $average <= 100)
    {
        $grade = "A";
    }
  
    # conditions to check if the average is
    # within 80-90 set the grade to "B"
    elsif( $average >= 80 && $average < 90)
    {
        $grade = "B";
    }
  
    # conditions to check if the average is
    # within 70-80 set the grade to "C"
    elsif( $average >= 70 && $average < 80)
    {
        $grade = "C";
    }
  
    # conditions to check if the average is
    # within 60-70 set the grade to "D"
    elsif( $average >= 60 && $average <70)
    {
        $grade = "D";
    }
  
    # Otherwise set the grade to "E"
    else
    {
        $grade = "E";
    }
  
    # display the values
    printf("%-8s %8.2f \t %4s\n", $key, $average, $grade);
}


Related Solutions

**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command...
**Need to use awk command in putty (should be a ONE LINE COMMAND) Write the command that would find all lines that have an email address and place a label email = before the line in the file longfile output will multiple lines similar to this one : using a good awk command the output would be something like this email = From: "Linder, Jann/WDC" <[email protected]> email = To: Mr Arlington Hewes <[email protected]> email = > From: Mr Arlington Hewes...
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple...
make multiply function with ‘add’ command, Convert to mips code Don’t use ‘mult’ Use 'add' multiple times Get input from key-board and display the result in the console window
Please look at the following code. When I run it from the command line, I am...
Please look at the following code. When I run it from the command line, I am supposed to get the following results: 1: I am 1: I am I need to fix it so that the functions 'print_i' and 'print_j' print all of their lines. What do I need to add? Thank you. C source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <pthread.h> #include <unistd.h> // These two functions will run concurrently void* print_i(void *ptr) { printf("1: I am...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads...
JAVA CODE FOR BEGINNERS!! DON'T USE FOR OR WHILE METHODS PLEASE! Write a program that reads three strings from the keyboard. Although the strings are in no particular order, display the string that would be second if they were arranged lexicographically.
Download the Perl code of "Example 1: Conceptual Translation" from "Resources for Undergraduate Bioinformatics Instruction" at...
Download the Perl code of "Example 1: Conceptual Translation" from "Resources for Undergraduate Bioinformatics Instruction" at Wright State University http://birg.cs.wright.edu/resources/index.shtml. The program performs the conceptual translation on three forward reading frames from mRNA nucleotides to amino acids. The mRNA sequence is provided as the command line argument. Modify it to add the following features: a. (5pts) If no argument is provided in the command line, prompt the user to input a nucleotide sequence. Note that the modified code should still...
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Please write code in SCALA Thanks in advance Use curried representation and write system which convert...
Please write code in SCALA Thanks in advance Use curried representation and write system which convert pressure units. Input: pressure in atm (atmosphere) Outputs: PSI, bar, torr 1 atm = 14.6956 psi = 760 torr = 101325 Pa = 1.01325 bar.
** please don't copy and paste ** please don't use handwriting Q1: As the project sponsor,...
** please don't copy and paste ** please don't use handwriting Q1: As the project sponsor, you suggested that your company that runs multiple local supermarkets should provide an online shopping service to increase sales during COVID-19 pandemic. Write a system request to propose this project. System request Project Sponsor Business Need Business Requirements Business Value Special Issues or Constraints
please don't use handwriting .. use your own words i need unique answer .. please don't...
please don't use handwriting .. use your own words i need unique answer .. please don't copy and paste Q1.Dala Corporation is considering a project, which will involve the following cash inflows and (out) flows Details Amount Initial Outlay SAR (400000) After 1 Year SAR 40000 After 2 Years SAR 300000 After 3Years SAR 300000 What will be the NPV of this project if a discount rate of 15% is used? _________________ Q2.Merck Inc. is about to undertake a project...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT