Help with this code please.

Hello,
I haven't used this nano editor very often so i'm not very familiar with it.
So I've done this code for FCFS or FIFO algorithm in another language and tried to "Translate it into the nano editor.
Thing is I don't really know what's wrong with my "translation right now" so help would be much appreciated . cheers.

Here's the code:

#!/bin/bash

a=0; b=0; g=0; w=0;
echo "Enter Number of Processes:"
read n

echo "Enter Burst Time:"
for (( i=0; i<n; i++ ))
do
read b
done

echo "Enter Arrival Time:"
for (( i=0; i<n; i++ ))
do
read a
done

g[0]=0
for (( i=0; i<n; i++ ))
do
w=g-a
t=g+1-a
awt=awt+w
at=at+t
done

awt=awt/n
at=at/n

echo " Waiting time:"
for (( i=0; i<n; i++ ))
do
echo $w
done

echo "Turnaround time:"
for (( i=0; i<n; i++ ))
do
echo $t
done

echo " The average waiting time is ",$awt
echo " The average turnaround time is ",$at

You're not "programming in nano language", you're programming BASH. You can edit C, shell, c-shell, and many other kinds of code in nano.

#!/bin/bash

# what is i supposed to be here?  zero?
i=0
a[$i]=0; b[$i]=0; g[$i]=0; w[$i]=0;
echo "Enter Number of Processes:"
read n

echo "Enter Burst Time:"
for (( i=0; i<n; i++ ))
do
        read b[$i]
done

echo "Enter Arrival Time:"
for (( i=0; i<n; i++ ))
do
        read a[$i]
done

Mostly, so far so good...

g[0]=0
for (( i=0; i<n; i++ ))
do
        # Expressions don't work that way in BASH.
        #w=g-a
        #t=g+1-a
        #awt=awt+w
        #at=at+t
        # Is this a[1] supposed to be a ?
        ((w=g+1-a[1]))
        ((t=g+1-a))
        ((awt=awt+w))
        ((at=at+t))
done

# Beware that BASH only supports integers!
# You won't get decimal points here.
((awt=awt/n))
((at=at/n))

# You don't need to loop through an array to print it.
# echo " Waiting time:"
#for (( i=0; i<n; i++ ))
#do
#        echo $w
#done
echo "Waiting time:  ${w[*]}"

#echo "Turnaround time:"
#for (( i=0; i<n; i++ ))
#do
#echo $t
#done

echo "Turnaround time: ${t[*]}"

# You don't need , to separate arguments in shell.
# you can put arguments right inside strings, too.
#echo " The average waiting time is ",$awt
#echo " The average turnaround time is ",$at
echo "The average waiting time is $awt"
echo "The average turnaround time is $at"

I've done all what you said and still no luck .. get syntax errors that aren't very specific like unexpected end of line ...

Here's my current code again

#!/bin/bash
i=0
a[$i]=0; b[$i]=0; g[$i]=0; w[$i]=0;

echo "Enter Number of Processes:"
read n

echo "Enter Burst Time:"
for (( i=0; i<n; i++ ))
do
read b[$i]
done

echo "Enter Arrival Time:"
for (( i=0; i<n; i++ ))
do
read a[$i]
done

g[0]=0
for (( i=0; i<n; i++ ))
do
((w=g-a))
((t=g[i+1]-a))
((awt=awt+w))
((at=at+t))
done

((awt=awt/n))
((at=at/n))
echo " The waiting time is ${w[*]}"
echo " The turnaround time is ${t[*]}"
echo " The Average waiting time is $awt"
echo " The Average Turnaround time is $at"

---------- Post updated at 12:30 AM ---------- Previous update was at 12:29 AM ----------

Thank you for the help anyway, much appreciated .

---------- Post updated at 03:31 AM ---------- Previous update was at 12:30 AM ----------

Still can't figure out what's wrong and I think it's to do with the "g[i\+1] -a[i]"
I just get silly results when it shows it...

I didn't get any syntax error with the code you have pasted. May be you can paste the error you are getting.
Also, check your formula for awt and at. When it enters the calcaulation loop for the first time, g[i], g[i\+1] is zero/null.

regards,
Ahamed

Yeah I get no syntax error now ... but when it displays the result they are just not right...
The formula is the same as the one i used in the other language... but this one just doesn't want to work properly :confused:
not sure what i missed out.

I warned you that BASH doesn't support floating point numbers... You never get decimal points. Does your math use, or result in, numbers with decimal points?