Arithmetic Problem with shell script programming.

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Arithmetic Problem with shell script programming.
# 1  
Old 11-30-2010
Arithmetic Problem with shell script programming.

Hello everybody,

I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input.

I admit I am really bad at understanding his teaching, for one. He teaches this class as you had previous experience with programming and when i ask him questions he is very evasive with his answers. My problem is that he didn't have a lecture about flowcharts and i don't understand how to make it work for unlimited numbers introduced.

I was able to make a small script that can add 5 numbers from arg and 5 from input but that is all. he told me to use whiles and fors but i cant seem to grasp the logic so now I am asking you for my help.

I will post here his requirements for the script and also the code for the script that i wrote hoping that somebody can help me.
I appreciate all the help and i will check later with you.

Thank you.
1. The problem statement, all variables and given/known data

Write a bash program to:

a) Input zero or more integers from standard input (one per line)

b) Input zero or more integers from command line arguments

c) Output the sum of all input numbers on standard output


It is not necessary to check for invalid input or to format the output.




2. Relevant commands, code, scripts, algorithms:

Free to use any of if statements, elif statements, while loops, for loops to achieve desired result.

3. The attempts at a solution (include all code and scripts):
Unfortunately script accepts only 5 numbers and the professor wants unlimited input.

if [ $# -lt 1 ]
then
echo Please enter you numbers \:
read num1
read num2
read num3
read num4
read num5
sum=$(( $num1 + $num2 + $num3 + $num4 + $num5 ))
echo The total is $sum


elif [ $# -le 5 ]
then
read num1
read num2
read num3
read num4
read num5
sum=$(( $num1 + $num2 + $num3 + $num4 + $num5 ))
echo The grand total is $(( $sum + $1 + $2 + $3 + $4 + $5 ))
fi

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Diablo Valley College, PleasatHill, Ca, Stuart Fogg, Comsc117 I cant add URL if I don't have 5 posts, sorry.

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 11-30-2010
the synthax of a while loop is for example :

Code:
while [ condition ]
do
<some command>
done

by the way, you should also take a look at the += operator so that you can pass the read value as the incremental value of the sum that would be updated "on the fly"

You also could consider the
Code:
while :
do
...
done

the colon means /bin/true (this is for making endless loop).
to go out of the while loop, you could use break (exit would take you out of the script)

You can also have an eye to the trap command so you could handle a scenario like :
the script always ask number (infinite loop)
when the user want to display the sum, he press <Cntrl>+<C> key.
That will raise a INT signal that can be trapped (see trap command) to get out of the wile loop and display the sum.

I could write it but that wouldn't help you to learn, so now i think you have enough clue to go on Smilie

Last edited by ctsgnb; 11-30-2010 at 03:37 PM..
This User Gave Thanks to ctsgnb For This Post:
# 3  
Old 11-30-2010
your prof wants a loop I think, for the command line part.
There is a problem with arguments you need to know
you can reference $1 .. $9 the way I show. More than that and it becomes ${10} ${11} and so on. I would suggest using the shift command to move each argument into the $1 variable.

you need a loop for the arguments as well

Code:
#!/bin/ksh
total=0
while :
do
     echo "enter number, return to stop: \c"
     read num
     if [ -z "$num" ] ; then
           break
     fi
     total=$(( $total + num )) 
done
echo "Total = $total"

This User Gave Thanks to jim mcnamara For This Post:
# 4  
Old 11-30-2010
@jim
I have just been told by bakunin not to provide full solution to people in the course/education section ... Smilie
This User Gave Thanks to ctsgnb For This Post:
# 5  
Old 11-30-2010
You know I don't want to create any problems to anybody so i do not want the solution what i want is to figure it out how to create unlimited input. I know it may be limited to the operating system limit so I thank everybody for helping.

I will talk to my professor as well, no doubt.
# 6  
Old 11-30-2010
You can do an "unlimited" input just the way jim demonstrate i.e. using an "endless" loop :

Code:
while :
do
...
done

but of course you need to foresee some conditions that will break the loop so the user can display the sum of what he has entered.

In Jim code for example, if the user doesn't give an entry and press <enter> then the variable will be empty and the loop breaked.
# 7  
Old 12-01-2010
Thank you guys for all the help,
with all the information that you provided plus my discussion with the professor
I was able to finish the script close to what the professor required.


here is the code for the script

Code:
#!/bin/bash
if [[ $# -le 0 ]]
then
echo Please enter you numbers \:
sum=0
while true
  do
  if read num1
    then
    sum=$(( $sum + $num1 ))
  else
    break
  fi
done
echo $sum
fi

if [[ $# -ge 1 ]]
then
count=1
arg=$#
sum=0
while [ $count -le $arg ]
  do
    num1=$1
    sum=$(( $sum + $num1 ))
    shift
    count=$(( $count +1 ))
  done
echo $sum
fi

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script to read lines in a text file and filter user data Shell Programming and Scripting

sxsaaas (3 Replies)
Discussion started by: VikrantD
3 Replies

2. Shell Programming and Scripting

Help with Arithmetic calculations in Shell script

Hi, I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712 I want to perform ($a/$b)*100 with two decimals in the result. I tried with following: res=$((100*a/b)) res=`echo "scale=2; $a / $b" | bc` But I am not getting the decimal values.... (4 Replies)
Discussion started by: karumudi7
4 Replies

3. Shell Programming and Scripting

how to do decimal arithmetic in shell script

hi, I have a file with decimal/non-decimal values $ cat b22 373 164 92 62 20 131 94 12 129 111 95 154 37 15 447 25 7.4 135 77 122 32 92 70 57 37 42 72 17 13 97 40 41 53 22 80 71 29 87 23 31 273 6.2 12K 43 44 45 22 11 7.7 13 18 173 36 20 18 13 56 67 104 53 5.4 241 19 13 3.8 38 14 31 329 16 155... (8 Replies)
Discussion started by: sam05121988
8 Replies

4. Shell Programming and Scripting

Arithmetic Problem with shell script programming.

Hello everybody, I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input. I... (1 Reply)
Discussion started by: Florinel76
1 Replies

5. Shell Programming and Scripting

Arithmetic calculation on real numbers in Bourne Shell Script

I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings. I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers. Could anyone look at the script... (5 Replies)
Discussion started by: Tirmazi
5 Replies

6. Shell Programming and Scripting

problem in string comparison in shell programming

Hello, was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using if and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance :) (9 Replies)
Discussion started by: salman4u
9 Replies

7. Shell Programming and Scripting

Shell script programming help

I'm new to this and I need help with writing a script. The following assignment is as follows... Create a shell script named Project3-8 located in a directory supported by the File System Hierarchy Standard. The script needs to perform the following pseudocode in the order shown: -Display... (1 Reply)
Discussion started by: sdpinoy
1 Replies

8. Shell Programming and Scripting

Need help : Shell Script Programming

I have to complete my assignment and i need help... These are two simple shell script programs : 1.) Write a shell script to display files in the current directory in the following format : FileName Size Date Protection Owner _______ ____ ___ ________ _____ 2.) Write a... (1 Reply)
Discussion started by: harshthegreat89
1 Replies

9. UNIX for Dummies Questions & Answers

problem in script involving month arithmetic

advance happy new year to all, i am having a script.The purpose of the scripts is as follows.If the current month is march,june,september or december ,inc_flg should be set to '1' otherwise inc_flg should be set to '2' month= date +"%m" if || || || ; then inc_flg = 1 else ... (6 Replies)
Discussion started by: rajarp
6 Replies

10. Shell Programming and Scripting

Shell Script Programming

The problem is to : develop a shell script which allow patty and mick( two login names) to execute a program (3 Replies)
Discussion started by: abhishek0216
3 Replies
Login or Register to Ask a Question