LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-02-2016, 06:08 PM   #1
linuxnewbie2016
LQ Newbie
 
Registered: Aug 2016
Posts: 1

Rep: Reputation: Disabled
Red face Subtraction between numbers in an array


I'm using an array to try to subtraction. However, when my script just gives me the total of my array with a negative sign in front of it. Any suggestions?

elif [ "$OP" = "Subtraction" ]; then
echo "Please enter the number of values you would like to perform $OP"
read num
while [[ $num -gt $i ]]; do
echo "Enter your value"
read value
let total=$total-$value
let valuearr[$i]=$value
let i=$i+1
done
echo "You entered ${valuearr[*]}, and asked me to perform $OP. The answer is $total."
 
Old 08-02-2016, 10:49 PM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,424

Rep: Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823Reputation: 2823
Please use [code][/code] tags when showing code / data.

As for your question, I am not really following?? As you have not provided the details I do not know what value 'total' starts with, but if nothing then should the result not have a negative at the front?
Perhaps if you showed actual and desired output it would help with your explanation?
 
Old 08-04-2016, 10:04 PM   #3
JJJCR
Senior Member
 
Registered: Apr 2010
Posts: 1,122

Rep: Reputation: 190Reputation: 190
Question

Quote:
Originally Posted by linuxnewbie2016 View Post
I'm using an array to try to subtraction. However, when my script just gives me the total of my array with a negative sign in front of it. Any suggestions?

elif [ "$OP" = "Subtraction" ]; then
echo "Please enter the number of values you would like to perform $OP"
read num
while [[ $num -gt $i ]]; do
echo "Enter your value"
read value
let total=$total-$value
let valuearr[$i]=$value
let i=$i+1
done
echo "You entered ${valuearr[*]}, and asked me to perform $OP. The answer is $total."
is this school assignment?

anyway you showed some efforts

Quote:
while [[ $num -gt $i ]]; do
You have the code above while $num is greater than $i, but $i is not initialize. And what's the value for that?

Is your goal to subtract from the first difference all the way down to zero?

Last edited by JJJCR; 08-04-2016 at 11:53 PM. Reason: edit
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting numbers from "" quoted char array and save to different array zme2000 Programming 8 11-02-2012 03:28 PM
[SOLVED] date subtraction using numbers tjay1983 Programming 11 11-18-2010 04:57 PM
To store N numbers in an array som_kurian Programming 10 12-16-2007 04:31 AM
C++: Save a table of numbers from a file to an array geodpsa Linux - Newbie 1 12-10-2005 04:55 PM
Shell scripting - Adding many numbers in an array Stingreen Linux - General 2 10-25-2004 06:25 PM


All times are GMT -5. The time now is 03:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration