Trying to implement count_collatz_step function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trying to implement count_collatz_step function
# 8  
Old 10-05-2015
Hi,
Your function in bash return always 1 (loop while is missing),here a corrected version:
Code:
count_collatz_step() {
   step=0
   num=$1
   while [ $num -ne 1 ]
   do
    if [ $((num % 2)) -eq 0 ]
    then
        num=$((num/2))
        step=$((step + 1))
    else
        num=$((3*num+1))
        step=$((step + 1))
    fi
   done
   echo $step
}

echo "Input start point: "
read start
echo "Input end point: "
read end

count=0

while [ $start -lt $end ]
do
    echo $start + ":" + $(count_collatz_step $start)
    if [ $((count % 7)) -eq 0 ]
    then
        echo -e "\n"
    fi
    start=$((start+1))
    count=$((count+1))
done

Regards.
# 9  
Old 10-05-2015
Assigning the parameter to a (local) variable in the function is the right thing to do.
But - you can't use return in bash like you do in e.g. C as it will return an exit status into the $? special parameter, not a value to be used by the caller. Print the value instead.
On the caller's side, you need to execute the function, by either running it on the command line or by using the command substitution mechanism, which also allows to intercept the stdout and use it for e.g. an assignment. Try echo $start + ":" + $(count_collatz_step $start)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Implement the '&&' function in a shell

Hello, I'm currently implementing the && function in a shell using C. For example, if we input cmd1 && cmd2, then cmd2 executes only when cmd1 exits successfully. I'm thinking about: int main() { int i; char **args; while(1) { printf("yongfeng's shell:~$ "); args =... (5 Replies)
Discussion started by: Yongfeng
5 Replies

2. Programming

Implement ps command in C

Hello, could anybody explain how the ps command works? I know something about the proc file system. But I'm still not sure about how it exactly works. Like ps without any option will print out the current user's processes, but it never displays my web browsers such as firefox or my LibreOffice... (3 Replies)
Discussion started by: freedombird9
3 Replies

3. Shell Programming and Scripting

How to implement scenario?

hi, i am having three files which is having following data file1: field1 field2 field3 1 A B 2 C D 3 E F file2: 4 G H 1 I J 5 K L file3: 4 M N (3 Replies)
Discussion started by: angel12345
3 Replies

4. Shell Programming and Scripting

How to implement this?

hi i have a file like 1,"A","B" 2,"C","D" 1,"E","F" 3,"G","H" in output i need like 3,"G","H" 1,"E","F" 2,"C","D" 1,"A","B" (12 Replies)
Discussion started by: angel12345
12 Replies

5. Shell Programming and Scripting

Want to implement VLOOKUP (Excel function) in Unix

Dear All, i want to implement vookup function which is there in excel into Unix. Suppose i have 2 files. The files are given below. File1: MSC Cell SDCA Patna-1 12 Bihar Patna-2 45 Ranchi Bhopal-1 85 Raigarh Bhopal-2 ... (8 Replies)
Discussion started by: pravani1
8 Replies

6. Shell Programming and Scripting

how to implement this

Hi all, could any of you please help me on my problem.. we are doing FTP (one report out put) from one server to another server through unix shell script program. Due to the network issues, some times FTP process is hanging. So we planned to modify the existing program with the following... (2 Replies)
Discussion started by: kishore_jasthi
2 Replies

7. Programming

How to implement polling for a function using timer in C?

Hi, Can you please help me in implementing a timer based polling for function in C? ie. the function should be called in say 30secs(when 30secs has lapsed). Thanks (7 Replies)
Discussion started by: naan
7 Replies

8. AIX

how to implement timer

anyone can help me how to implement the timer on AIX? I tried with 'setitimer' and its related functions, but it does not work correctly,the program exited each time. thanks (2 Replies)
Discussion started by: Frank2004
2 Replies

9. Programming

how does va_arg implement ?

1 . How does va_arg implemented by system? (2 Replies)
Discussion started by: chenhao_no1
2 Replies

10. UNIX for Advanced & Expert Users

how can i implement rlogin

how can i use a rlogin with out entered a password, someone tell me about configure the next files /.rhosts /etc/hosts.equiv and /etc/hosts but i not sure about that, or there are not enough could you tell me how to do that? (3 Replies)
Discussion started by: jav_v
3 Replies
Login or Register to Ask a Question