Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Test scores into Letter Grade

Status
Not open for further replies.

Tamrak

MIS
Jan 18, 2001
213
US
Hello,

I am very new to Unix and need to work on this project. I am working in the training department. Our employees have to perform four tests to qualify the placement.

I have a flat file that contains the following format:
employee id:lastname: firstname:test1:test2:test3:test4.
Data with colon delimeters will look like the following -> 1001:SMITH:JOHN:86:78:80:89. There are several lines in this text file. Each test score is based on 100 points each.

I need to compute the average score and letter grade on each employee.

Average score formula should be: 50% of test1 + 20% of test2 + 10% of test3 + 20% of test4. The letter grade will be A/B/C/D, which is corresponding to 90/80/70/60 scale.

From the example above, John Smith will have the average score of : (50% * 86) + (20% * 78) + (10% *80) + (20% *89) which is equivalent to 84.4. That will be translated into a "B" grade.

My questions are:

1. How does Unix know to stop processing at the end of file since Unix does not have the end of file command? One recommendation that my friend has is to use the tail command and extract the last record into a temporary file. Then, run a comparison between this huge file and that temporary file. How can I accomplish this task? Do you have any suggestions?

When he talked about file comparison, is this what the diff command come in? However, how can I do it record by record? I am concerned because I might have over 1,000 records.

2. How can I do record count in Unix? Will wc -l work? Right now, I have one record per line.


The ultimate goal of this program is to run an output, using the awk command that will display -> student id, student name, course average and letter grade.

I am stuck on this one. I will be very much appreciated if you can enlighten me. Thank you.
 
Tamrak:

I'll help you get started:

>1. How does Unix know to stop processing at the end of >file since Unix does not have the end of file command?

In Unix, there's more than one way of doing things: The following code reads your data file one line or record at a time quitting when the end of file is reached:

#!/bin/ksh

rec_counter=0
while read line
do
echo $line # display the line.
done < file

There's no reason I see to get the last record in the file and compare against that.

The diff file is used to compare files, but it's not needed to solve this problem.

>2. How can I do record count in Unix? Will wc -l work? >Right now, I have one record per line.

Yes, you can get the total record count this way:

rec_count=`wc -l file|awk ' { print $1 } ' `

but, only do this if you need the total record count before you start processing. This stub prints out each record and gives you a count:

#!/bin/ksh

rec_counter=0
while read line
do
## rec_counter=`expr $rec_counter + 1`
rec_counter=$((rec_counter + 1))
echo $line|awk ' { print $0 } '
done < file
echo $rec_counter

I'm using the Korn Shell, if you're using Bourne, use the expr method for doing arithmetic.

I echo'd the input to awk just to get you started. If you've never used awk, the learning curve may be daunting. If you want more help, just ask.

Regards,


Ed
 
Here's a start for the awk program you need.

BEGIN {FS=&quot;:&quot;}
{
ngrade = .5*$1 + .2*$2 + .1*$3 + .2*$4
if (ngrade >= 90) lgrade=&quot;A&quot;
else if (ngrade >= 80) lgrade=&quot;B&quot;
else if (ngrade >= 70) lgrade=&quot;C&quot;
else if (ngrade >= 60) lgrade=&quot;D&quot;
else lgrade=&quot;F&quot;
print $2 &quot; &quot; $3 &quot; &quot; lgrade
}

One way to run it is to save tis into a file grade.awk say and enter

awk -f grade.awk input

This is all untested, so good luck.
CaKiwi
 
Hello all,

I will try with the module on Monday. I use Bash shell. I believe that it will work the same. Thanks again for your post.
 
Hello again,

Let's say the file name is Employees.

My intentions to receive the output, after executing this program, are:

Employee ID, Employee's FIRST and LAST NAME, Average Score and Letter Grade. The awk command should justify the printout. For example:

ID# Name Last Name Avg Score Grade # heading

1001 JOHN SMITH 84 B
1002 SUZANNE WHITE 90 A

.. and so on..

Awk command will take care of this printout.

Problems that I still have:

Average score must be computed based on the formula given.

Average score formula should be: 50% of test1 + 20% of test2 + 10% of test3 + 20% of test4. The letter grade will be A/B/C/D, which is corresponding to 90/80/70/60 scale.

I tried to run the test program that CaKiwi provided and received a Parse error.

The flat file that I have contains colon delimited, total of 7 fields. They are: employee ID: Employee Last Name: Employee First Name: Test 1 score : Test 2 score: Test 3 score : Test 4 score. Computation will be done on column 4, 5, 6 and 7, which are corresponding to test score#1,#2,#3 and #4.

I still having problems on making the program displayed employee, record by record until it reaches the end of file. I realize that I need a do while loop to calculate, record by record.

The user will continue adding the record, so, the total record count will be different.

My co-workers told me that Bash shell won't accept decimal value. I am not sure on this one.

Thanks again for your time. I hope it won't give you much of the headaches. Right now, I need the Tylenol. Any inputs related to these matters will be very much appreciated. Thanks.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top