Arithmetic Problem with shell script programming.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting 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.

Code:
General Considerations

1)  Think about the structure of your program before you write any code.

2)  Write your program in small parts.  Test each part before you add the next.

3)  Write your source code in plain ASCII (especially not RTF or DOC).

4)  Include comments with:
      name, assignment, and date (at the top)
      a description of what your program does and how to use it (at the top)
      the logic of your program (if needed, distributed throughout)

5)  Use descriptive names for your variables, functions, etc.

6)  Prompt for interactive input (if applicable).

7)  Test your program with different inputs.


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.

Due midnight 30 November.

and my script

Code:
# A small script that can add 5 numbers from the script arguments
# and 5 numbers that can be entered as a user input.

# Usage: Run the program and if no arguments you will be prompted to enter
# 5 numbers from standard input otherwise you can add the 5 arguments and
# then you will be prompted to enter 5 more numbers.

# When there are no arguments the user can enter the numbers
# from standard input and then the addition will be done.
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

# When the user enters the numbers as arguments to the script he will
# be prompted to enter numbers as standard input as well and have the
# the addition done.
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

# 2  
Old 11-30-2010
Do not post classroom or homework problems in the main forums. Homework and coursework questions can only be posted in this forum under special homework rules.

Please review the rules, which you agreed to when you registered, if you have not already done so.

More-than-likely, posting homework in the main forums has resulting in a forum infraction. If you did not post homework, please explain the company you work for and the nature of the problem you are working on.

If you did post homework in the main forums, please review the guidelines for posting homework and repost.

Thank You.

The UNIX and Linux Forums.
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. Homework & Coursework Questions

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... (8 Replies)
Discussion started by: Florinel76
8 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