Arrays and functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Arrays and functions
# 1  
Old 07-29-2012
Arrays and functions

Hi Guys! I need to solve this.
I want an array to be created by a certain calculation for which I created a function. Now this array is not getting created. See script below I want array b to be the factorial value of array element a. Help is needed. Thanks!

Code:
#!/bin/bash

echo "Number of factorials:"
read h

for (( i = 0; $i < h; i++ ))
do
echo "Enter the factor $i:"
read d
a[$i]=$d
b[$i]=$(fun $d)
done
echo ${a[*]}
echo ${b[*]}


fun()
{
j=`expr 1 + $1`
z=1
f=0

for (( i = 1; $i < $j; i++ ))
do
f=`expr $j - $i`
z=`expr $z \* $f`
done
return $z
}

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by ambijat; 07-30-2012 at 11:08 AM..
# 2  
Old 07-29-2012
Couple of things:

1. define function before you use it (probably best at top of script)
2. result should be echoed not returned (ie change return $z to echo $z)
# 3  
Old 07-29-2012
also, you are using bash. no need for ancient var=`expr $i - $j` etc. There is now var=$(( i - j )). $ is not required in arithmetic context (your for loop, a[i]=$d...).
# 4  
Old 07-30-2012
Quote:
Originally Posted by Chubler_XL
Couple of things:

1. define function before you use it (probably best at top of script)
2. result should be echoed not returned (ie change return $z to echo $z)
Well I take that as good practice but I really did not find program behaving any different.
My problem is with this part below.
b[$i]=$(fun $d)
I want that function to be used for creating values that is fed into array. It simply does not work.
Any help!
# 5  
Old 07-30-2012
There was help. Did you try the suggestions? If you did, it'd look something like this:

Code:
#!/bin/bash
fun() {
        # makes these local to the function.
        declare -i j z f i n=$1

        j=$(( 1 + n ))
        z=1
        f=0

        for (( i = 1; i < j; i++ ))
        do
                f=$(( j - i ))
                z=$(( z * f ))
        done

        echo "$z" # THIS WAS YOUR PRIMARY FAULT!
}

echo "Number of factorials:"
read h

for (( i = 0; i < h; i++ ))
do
        echo "Enter the factor $i:"
        read d
        a[i]=$d
        b[i]=$(fun $d)
done

echo ${a[*]}
echo ${b[*]}

# 6  
Old 07-30-2012
Well I re-wrote the whole thing as follows and it neatly gives me the required results. I look forward to improvements from you. Thanks.

Code:
#!/bin/bash

fun()
{
j=`expr 1 + $1`
z=1
f=0
for (( i = 1; $i < $j; i++ ))
do
f=`expr $j - $i`
z=`expr $z \* $f`
done
echo $z
}

echo -n "Enter the number of factors: "
read b
i=0

while [ $i -lt $b ]
do
echo -n "Enter the factor: "
read d
a=("${a[@]}" "$(fun $d)") #a=($(fun $d)). the previous wrong code, now corrected.
echo "The factorial for $d is  ${a[$i]}"
i=$(( i + 1 ))
done

echo ""
echo "Total elements in the arrary A are ${#a[*]}"
echo ""

for (( i = 0; i < ${#a[*]}; i++ ))
do
echo "a[$i] = ${a[$i]}"
done
echo ""

# 7  
Old 08-09-2012
cannot locate the mistake!

Hello friends! this is again some work on creating factorial. I am unable to locate the mistake, it gives very unique output. Kindly, have a look and sort it for me.

Code:
#!/bin/bash

fator(){
j=$(( $1 + 1 ))
z=1
f=0
# echo $j
for (( p = 1; $p < $j; p++ ))
do
f=`expr $j - $p`
z=`expr $z \* $f`
done
echo "$z"
return
}


k=("$@")
echo ${k[*]}
echo "No of arguments: $#"
echo "length of array is: ${#k[*]}"

i=0

while [ $i -lt ${#k[*]} ]
do
echo "k[$i] = ${k[$i]}"
a=("${a[$i]}" "$(fator ${k[$i]})")
echo "a[$i] = ${a[$i]}"

i=$(( $i + 1 ))

done
echo ""

I used the command
Code:
./fac3b 7 6 8 9

Just try yourself. It gives output for only one argument. I want for all of them. fac3b is a kind of filename, I give to revisions that I perform. Help is most solicited.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

2. Programming

question about int arrays and file pointer arrays

if i declare both but don't input any variables what values will the int array and file pointer array have on default, and if i want to reset any of the elements of both arrays to default, should i just set it to 0 or NULL or what? (1 Reply)
Discussion started by: omega666
1 Replies

3. Shell Programming and Scripting

i think i need functions ?

Hi, im making a little script but need some help Code i have so far is read -p 'Bot Nickname:' ecnick read -p 'Bot Username:' ecusername read -p 'Bot Realname:' ecrealname read -p 'Your Email:' ecemail echo '' echo Your bots nickname is set to $ecnick echo Your bots username is set to... (2 Replies)
Discussion started by: Gemster
2 Replies

4. UNIX for Dummies Questions & Answers

Help with functions

Hi, I am exploring with defining functions in my BASH shell scripts. However, I am bit confused about how to pass parameters to my functions. I was under the impression that you must do something like the following: Define a function called "sample_function": function sample_function {... (3 Replies)
Discussion started by: msb65
3 Replies

5. Shell Programming and Scripting

Need a little help with functions

I'm semi new to unix/linux and am trying to convert a program I wrote in C++ to a bash script. It's a program that prints Fibonacci's series. I have found scripts that will do it, but I'm trying persistently to get this one to work. The problem occurs when I try to return a value from the function.... (3 Replies)
Discussion started by: Glowworm
3 Replies

6. Shell Programming and Scripting

functions

I have korn shells where I want to create a function passing $1 to a function , determine my $STAT_ENV value, set the paths and return the paths for STATSH,STATPRM,STATSQR,STATSQL,STATCTL TO BE USED IN THE UNIX SCRIPT THE CALLED THE fucnction in the first place. Can someone tell me the best... (2 Replies)
Discussion started by: TimHortons
2 Replies

7. Web Development

PHP arrays in arrays

PHP question... I have an SQL query that's pulled back user IDs as a set of columns. Rather than IDs, I want to use their names. So I have an array of columns $col with values 1,7,3,12 etc and I've got an array $person with values "Fred", "Bert", "Tom" etc So what I want to do is display the... (3 Replies)
Discussion started by: JerryHone
3 Replies

8. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

9. Shell Programming and Scripting

perl functions and arrays

Hi, First I will tell my objective of this function (function one). I have a table for ex: id passwd name -- ------ ----- 1 fdhgfs werwer 2 fsdfs sdfsdf 3 sdfs sdfsdf 4 fdsfs dssdf . . . . . . The id, passwd and name are the arguments for another function say two. (1 Reply)
Discussion started by: mercuryshipzz
1 Replies

10. Shell Programming and Scripting

Use of functions

Hi my shell is tcsh can I have functions in my shell scripting? Is the below shell script correct. Can I have two functions and call one of them as required. ---------- echo "functions" f1 f1 () { echo "hello" } f2 () (1 Reply)
Discussion started by: amitrajvarma
1 Replies
Login or Register to Ask a Question