LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-17-2013, 04:39 PM   #1
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Rep: Reputation: 0
Arithmetic operation between 2 Arrays to generate a new Array


Hi, I have defined to different arrays, ARRAY1 and ARRAY2. Each of them has 20 elements (integer numbers).

Code:
#!/bin/bash

ARRAY1=(0 3 7 3 9 10 6 8 8 8 7 3 8 4 4 7 1 6 7 6)

ARRAY2=(1 4 4 2 1 5 4 6 1 3 5 5 3 6 1 1 6 1 2 2)

for ((i; i < 20; i++))
do

   if [ ${ARRAY1[i]} -ge ${ARRAY2[i-1]} ]
    then
     ARRAY3+=(${ARRAY2[i]})
    else
     ARRAY3+=$(( (${ARRAY2[i-1]}) - (${ARRAY1[i]}) + (${ARRAY2[i]}) ))

        
#Double brackets in each side to do with arithmetic operation
    fi
done

echo ${ARRAY3[@]}
I have to compare each element in the position "i" in ARRAY1 to the element in position "i-1" in ARRAY2. If the element in ARRAY1 is either equal or greater than the element in ARRAY2, this element in ARRAY2 is added to a new ARRAY3. Otherwise I add to ARRAY 3 (element i-1 in ARRAY2 - element i in Array1 + element i in ARRAY3)

As an example.
ARRAY1[1]=3
ARRAY2[1-1]=1
ARRAY2[1]=4

Then element in ARRAY3 = ARRAY2[1] = 4

If if only test one set of elements,
Code:
  if [ ${ARRAY1[12]} -ge ${ARRAY2[11]} ]
    then
        ARRAY3+=(${ARRAY2[12]})
    else
        ARRAY3+=$(( (${ARRAY2[11]}) - (${ARRAY1[12]}) + (${ARRAY2[12]}) ))
   fi

the code function as expected, because it gives 3 as solution.
But when put the code inside of the for loop it doesn't work.
I think that the problem lays on how I put [i-1] inside the if clause.
Another doubt that I have is how is going to behave the code in the first iteration, because there is no [i-1] in first element of ARRAY2.

I hope you can shade some light in this issue.
Thanks!!
 
Old 10-17-2013, 04:54 PM   #2
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Why are you calling 3 an array? The way you're using it in your code it's just a number.

Anyway, you're not initializing "i" to anything, so of course it's not working. You'd probably want to start it at 1, since there is no "-1" element in array2.

Last edited by suicidaleggroll; 10-17-2013 at 04:59 PM.
 
Old 10-17-2013, 07:48 PM   #3
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by suicidaleggroll View Post
Why are you calling 3 an array? The way you're using it in your code it's just a number.

Anyway, you're not initializing "i" to anything, so of course it's not working. You'd probably want to start it at 1, since there is no "-1" element in array2.
3 is the result of the example iteration. In that case it acts as a simple variable, but it is just and example.
What I need is to generate an array, that's way I put the code inside a for loop.

suicidaleggroll, can you explain to me how do I correctly initialize "i"?
Thanks!
 
Old 10-17-2013, 08:10 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by astalavista2000 View Post
3 is the result of the example iteration. In that case it acts as a simple variable, but it is just and example.
What I need is to generate an array, that's way I put the code inside a for loop.
Yet you're still treating "ARRAY3" as a scalar. Where's the array indexing that lets the program know in which index it needs to place the result of your calculation?

Quote:
Originally Posted by astalavista2000 View Post
suicidaleggroll, can you explain to me how do I correctly initialize "i"?
By using the correct syntax in your for loop:
http://www.freeos.com/guides/lsst/ch03sec06.html

The way you're using the for loop in your code is literally no different than the way you do a for loop in any other language. There are three things you need to supply to any for loop:
1) Where to start
2) Where to stop
3) How to increment

You have #2 and #3 down, but not #1. In other words, you've told it to stop when i>=20, and to increment by 1 (i++), but you never told it where to start.

Last edited by suicidaleggroll; 10-17-2013 at 08:14 PM.
 
Old 10-17-2013, 09:07 PM   #5
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by suicidaleggroll View Post
Yet you're still treating "ARRAY3" as a scalar. Where's the array indexing that lets the program know in which index it needs to place the result of your calculation?


By using the correct syntax in your for loop:
http://www.freeos.com/guides/lsst/ch03sec06.html

The way you're using the for loop in your code is literally no different than the way you do a for loop in any other language. There are three things you need to supply to any for loop:
1) Where to start
2) Where to stop
3) How to increment

You have #2 and #3 down, but not #1. In other words, you've told it to stop when i>=20, and to increment by 1 (i++), but you never told it where to start.
OK. Now I understand what you are saying. I thought that there is no need to specify the index because it puts the result of the calculation in the "i" position determined by the for loop. And each time it iterates, there is a new available position.
Can you explain to me how do I treat ARRAY3 as an array, and how do I correct this line?
I really appreciate the time that you're taking to help me!

This is how I corrected the for loop. The code is almost ready.

Code:
for ((i=0; i<=20; i++))
 do

   if [ ${ARRAY1[i]} -ge ${ARRAY2[i-1]} ]
    then
        ARRAY3+=(${ARRAY2[i]})
    else
        ARRAY3+=$(( (${ARRAY2[i-1]}) - (${ARRAY1[i]}) + (${ARRAY2[i]}) ))
   fi
done
PD. If I put i=1 instead of i=0 in the for loop I will avoid the situation that I described in which there is no i-1, won't I?

Last edited by astalavista2000; 10-17-2013 at 09:15 PM. Reason: corrections
 
Old 10-17-2013, 10:56 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
You simply need to assign the value to the correct index, and remove the "+=" since you're not incrementing anything, you're assigning:
Code:
ARRAY3[$i]=${ARRAY2[$i]}
etc.
 
Old 10-18-2013, 01:53 AM   #7
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by suicidaleggroll View Post
You simply need to assign the value to the correct index, and remove the "+=" since you're not incrementing anything, you're assigning:
Code:
ARRAY3[$i]=${ARRAY2[$i]}
etc.
I'm almost there.
Code:
 for ((i=1; i<=20; i++))
                do



[LINE 22]               if [ ${ARRAY1[i]} -ge ${ARRAY2[i-1]} ]
                        then
                        Vector[$i]=${ARRAY2[i]}
                        else
[LINE 26]               Vector[$i]=$(( (${ARRAY2[i-1]}) - (${ARRAY1[i]}) + (${ARRAY2[i]}) ))
                        fi


                done

#I add the first position, where i-1 doesn't exist
ARRAY3[0]=${ARRAY2[0]}




echo ${ARRAY3[*]}
This is works as expected but with some errors. I got the following result

Code:
linux:/home/admin/simulacion# ./comparaVec.sh 
./comparaVec.sh: line 22: [: -ge: unary operator expected
./comparaVec.sh: line 26: (2) - () + () : syntax error: operand expected (error token is ") + () ")
1 4 4 3 1 5 4 6 1 3 5 7 3 6 3 1 6 1 2 2
As you can see the ARRAY3 is showing the correct 20 numbers that are generated after the operations between ARRAY1 and ARRAY2.
I just need to correct the errors. Can you help me with that?
Thanks!
 
Old 10-18-2013, 02:49 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
What is the value in either array 1 or 2 when i = 20??
 
Old 10-18-2013, 05:29 AM   #9
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
What is the value in either array 1 or 2 when i = 20??
Grail, all the values of ARRAY1 and ARRAY2 are at the begining of the code.
Here is what you've asked:

ARRAY1[20]=6
ARRAY2[20]=2
ARRAY2[20-1]=2

if [ARRAY1[20] >= ARRAY2[20-1]]
Then
ARRAY3[20]=ARRAY2[20] that is "2"

You can see that the result of this operation is the same that the last element of ARRAY3 (I wrote the output in my previous post.
Thanks
 
Old 10-18-2013, 07:42 AM   #10
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by astalavista2000 View Post
Grail, all the values of ARRAY1 and ARRAY2 are at the begining of the code.
Here is what you've asked:

ARRAY1[20]=6
ARRAY2[20]=2
ARRAY2[20-1]=2

if [ARRAY1[20] >= ARRAY2[20-1]]
Then
ARRAY3[20]=ARRAY2[20] that is "2"

You can see that the result of this operation is the same that the last element of ARRAY3 (I wrote the output in my previous post.
Thanks
SOLVED!

I debug the code and re-execute the script.
The Error was this: "./comparaVec.sh: line 22: [: -ge: unary operator expected"
And the last iteration in the debug mode showed this: + '[' -ge 2 ']'
-ge is a binary operator and I need two arguments. But, when i=20 ARRAY1 is empty! So I simple replace the i<=20 with i<20.
Thanks to both of you, suicidaleggroll for your help!
 
Old 10-18-2013, 08:44 AM   #11
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
By the way, += does work to append to an array.

3.4 Shell Parameters:
Quote:
When ‘+=’ is applied to an array variable using compound assignment (see Arrays), the variable’s value is not unset (as it is when using ‘=’), and new values are appended to the array beginning at one greater than the array’s maximum index (for indexed arrays), or added as additional key-value pairs in an associative array.
 
Old 10-19-2013, 02:41 AM   #12
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ntubski View Post
By the way, += does work to append to an array.

3.4 Shell Parameters:
So ARRAY3[i]= and ARRAY3+= are the same, aren't they?
 
Old 10-19-2013, 07:49 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by astalavista2000 View Post
So ARRAY3[i]= and ARRAY3+= are the same, aren't they?
No they are not the same unless 'i' is exactly in order with single index spacing.

So if you compare adding the first element to an array:
Code:
ARRAY3[3]="foo" # this places the string in the fourth index position

ARRAY4+=( "foo" ) # this places the string at the first index position
 
Old 10-19-2013, 09:44 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142Reputation: 2142
Quote:
Originally Posted by ntubski View Post
By the way, += does work to append to an array.
I did not realize that, thanks.
 
Old 10-19-2013, 09:44 PM   #15
astalavista2000
Member
 
Registered: Apr 2010
Distribution: debian
Posts: 42

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by grail View Post
No they are not the same unless 'i' is exactly in order with single index spacing.

So if you compare adding the first element to an array:
Code:
ARRAY3[3]="foo" # this places the string in the fourth index position

ARRAY4+=( "foo" ) # this places the string at the first index position
Great, I see the difference now. Thanks for being so clear grail.
I'm marking this thread as solved.
Thanks to all of you for being so helpful.
 
  


Reply



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
arithmetic operation C++ very weird nushki Programming 19 06-03-2011 12:49 AM
[SOLVED] arithmetic operation in bash xeon123 Linux - Newbie 6 03-10-2011 07:12 PM
How to make arithmetic operation of data file with script file? mauludi Linux - Newbie 5 09-01-2010 05:26 AM
Arithmetic operation with awk fugitive569 Programming 2 12-04-2009 10:10 AM
display after arithmetic operation tostay2003 Programming 7 08-14-2008 11:51 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 PM.

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
Open Source Consulting | Domain Registration