Sponsored Content
Top Forums Shell Programming and Scripting Trying to implement count_collatz_step function Post 302956890 by Bunchhieng on Monday 5th of October 2015 07:00:03 AM
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

 

10 More Discussions You Might Find Interesting

1. 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

2. Programming

how does va_arg implement ?

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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
DIO_SEEK(3)								 1							       DIO_SEEK(3)

dio_seek - Seeks to pos on fd from whence

SYNOPSIS
int dio_seek (resource $fd, int $pos, [int $whence = SEEK_SET]) DESCRIPTION
The function dio_seek(3) is used to change the file position of the given file descriptor. PARAMETERS
o $fd - The file descriptor returned by dio_open(3). o $pos - The new position. o $whence - Specifies how the position $pos should be interpreted: o SEEK_SET (default) - specifies that $pos is specified from the beginning of the file. o SEEK_CUR - Specifies that $pos is a count of characters from the current file position. This count may be positive or nega- tive. o SEEK_END - Specifies that $pos is a count of characters from the end of the file. A negative count specifies a position within the current extent of the file; a positive count specifies a position past the current end. If you set the position past the current end, and actually write data, you will extend the file with zeros up to that position. RETURN VALUES
EXAMPLES
Example #1 Positioning in a file <?php $fd = dio_open('/dev/ttyS0', O_RDWR); dio_seek($fd, 10, SEEK_SET); // position is now at 10 characters from the start of the file dio_seek($fd, -2, SEEK_CUR); // position is now at 8 characters from the start of the file dio_seek($fd, -5, SEEK_END); // position is now at 5 characters from the end of the file dio_seek($fd, 10, SEEK_END); // position is now at 10 characters past the end of the file. // The 10 characters between the end of the file and the current // position are filled with zeros. dio_close($fd); ?> PHP Documentation Group DIO_SEEK(3)
All times are GMT -4. The time now is 04:10 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy