Question
I am writing a program in awk that reads in numbers, and listing its number of items read in, sum, min, max and mean. For
I am writing a program in awk that reads in numbers, and listing its number of items read in, sum, min, max and mean. For validation we have to test from specific case text files, My Min value for 3 of the cases are incorrect and for the case with 0 numbers my program shouldnt go past the # items = 0 and Im having trouble with making a conditional statement for that. So far this is the code I have, along with the text file cases that I am getting incorrect. I really need help thanks.Source code: (Written in Unix, using awk)
#! /bin/awk -f
BEGIN{ items= 0 sum = 0 }
/[0-9]+/{ #if the line contains any numbers apply this whole instruction
# NF is the number of fields, $NF is the value of the last field # NR is the current line number
if ($0 != 0 && NF == 1){#if the line is not 0 and there's only 1 field
if ($0 !~ /[A-Za-z]/){#if it contains no letters (ideally we'd expand #this to include punctuation as well)
if(items ~ 0){ max = $0 min = $0 }
sum += $0 items++
if(max
if(min > $0){ min = $0 } }
} else if ($0 == 0){ exit(0)# exits without error }
}
END{ printf("# items: " items) printf(" Sum: %.3f ", sum) printf("Maximum: %.3f ", max) printf("Minimum: %.3f ", min) printf("Mean: %.3f ", sum / items) }
File Edit Options Buffers Tools AWK Help #!/bin/awk -f BEGIN{ items= 0 sum = 0 /[0-9]+/[ #if the line contains any numbers apply this whole instruction # NF is the number of fields, $NF is the value of the last field # NR is the current line number if ($0 != 0 && NF == 1) {#if the line is not 0 and there's only 1 field if ($0 !- /[A-Za-z]/) { if it contains no letters (ideally we'd expand #this to include punctuation as well) if(items - 0) { max = $0 min = $0 sum += $0 items++ if(max $0) { min = $0 else if ($0 == 0) { exit(0)# exits without error END { printf("# items: "items) printf(" Sum: 8.3f ", sum) printf("Maximum: $.3f ", max) printf("Minimum: 3.3f ", min) printf ("Mean: 8.3f ", sum / items) - D X - X - DX Testcase Notepad File Edit Format View Help Testcase1 - Notepad File Edit Format View Help - 1000000 -999999 -999998 -999997 -999996 -999995 -999994 -999993 -999992 999991 999990 Testcase2 - Notepad File Edit Format View Help 1000000 999999 999998 999997 999996 999995 999994 999993 999992 999991 999990 In 1, Col1 100% Windows (CRLF) UTF-8 In 1, Cal 1 100% Windows (CRLF) UTF-8 Ln 1. Col2 100% Windows (CRLF) UTF-8 File Edit Options Buffers Tools AWK Help #!/bin/awk -f BEGIN{ items= 0 sum = 0 /[0-9]+/[ #if the line contains any numbers apply this whole instruction # NF is the number of fields, $NF is the value of the last field # NR is the current line number if ($0 != 0 && NF == 1) {#if the line is not 0 and there's only 1 field if ($0 !- /[A-Za-z]/) { if it contains no letters (ideally we'd expand #this to include punctuation as well) if(items - 0) { max = $0 min = $0 sum += $0 items++ if(max $0) { min = $0 else if ($0 == 0) { exit(0)# exits without error END { printf("# items: "items) printf(" Sum: 8.3f ", sum) printf("Maximum: $.3f ", max) printf("Minimum: 3.3f ", min) printf ("Mean: 8.3f ", sum / items) - D X - X - DX Testcase Notepad File Edit Format View Help Testcase1 - Notepad File Edit Format View Help - 1000000 -999999 -999998 -999997 -999996 -999995 -999994 -999993 -999992 999991 999990 Testcase2 - Notepad File Edit Format View Help 1000000 999999 999998 999997 999996 999995 999994 999993 999992 999991 999990 In 1, Col1 100% Windows (CRLF) UTF-8 In 1, Cal 1 100% Windows (CRLF) UTF-8 Ln 1. Col2 100% Windows (CRLF) UTF-8Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started