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
# 1  
Old 10-04-2015
Linux Trying to implement count_collatz_step function

Can anyone give me a clue why this code doesn't work as expected? The function count_collatz_step() take one parameter which a number that need to calculate collatz until it reaches 1. The func should return steps it takes.

Code:
# 
# Count steps of collatz conjecture takes, until the number reach 1.
#

count_collatz_step() {
   step=0
    if [ $1 % 2 == 0 ]
    then 
        $1=$(($1/2))
        step=$((step + 1))
    else
        $1=$((3*$n+1))
        step=$((step + 1))
    fi
    echo $step
}

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

count=0

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

# 2  
Old 10-04-2015
I am assuming this is homework but IF NOT then:-

Take a look at using $1 in your script, (assuming the shell is bash).
# 3  
Old 10-04-2015
Quote:
Originally Posted by wisecracker
I am assuming this is homework but IF NOT then:-

Take a look at using $1 in your script, (assuming the shell is bash).
This is not homework. I actually implemented this in C and want to make it work on Bash. I just started to learn Bash a few days ago.
# 4  
Old 10-04-2015
Hi Bunching,
Please show us your working C code. I'm guessing that you have misunderstood how echo and read work in bash and that your function might want to return a value rather than print a value, but without a description of what you're trying to do (rather than just a statement that your shell script isn't working), it is hard to guess how it should be corrected. But, one clear thing is that there is nothing in your code that invokes your function. (You print the name of the function as one of the arguments to one of your echo statements, but you do not call the function.)

Wisecracker,
Note that in a bash function, $1 is the 1st command line argument passed to the function; not a command line argument passed to the script containing the function definition.
# 5  
Old 10-05-2015
Quote:
Originally Posted by Don Cragun
Wisecracker,
Note that in a bash function, $1 is the 1st command line argument passed to the function; not a command line argument passed to the script containing the function definition.
Yes but would this part still work, inside and/or outside of a function?
Code:
 $1=$(($1/2))

# 6  
Old 10-05-2015
No, because you cannot assign a value to a positional parameter this way. What you can do is
Code:
set $(($1/2))

but only if you can accept to loose the current value of $1 and all other positional parameters ($2, $3, ...), if present.
# 7  
Old 10-05-2015
Hello folks, this is my working C code. I'd like to translate this code into bash.

Code:
#include <stdio.h>

int count_collatz_steps (int number) {
	
	int step = 0;
	
	while (number != 1) {
		if (number % 2 == 0) {
			number = number/2;
			++step;
		} else {
			number = 3*number + 1;
			++step;
		}
	}
	
	return step;
}

int main(int argc, char *argv[]) {
	
	int start;
	int	end;
	int column = 0;
	int i;
	int count = 1;
	
	printf("Enter a starting point: ");
	scanf("%d", &start);
	printf("Enter an ending point: ");
	scanf("%d", &end);
	
	while (start < end && start > 1 && end < 1000 && end > start && end < 10000) {
		printf("%2d:%d \t", start, count_collatz_steps(start));
		if ( count % 7 == 0) {
			printf("\n");
		}
		++count;
		++start;
	}
	
	return 0;
}


Bash:
The only thing that bug is how do I call a function with argument in Bash?
Code:
count_collatz_step() {
   step=0
   num=$1
    if [ $num % 2 == 0 ]
    then
        $num=$(($num/2))
        $step=$((step + 1))
    else
        $num=$((3*$num+1))
        $step=$((step + 1))
    fi
    return $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 "\n"
    fi
    start=$((start+1))
    count=$((count+1))
done

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