Why this code is not working?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why this code is not working?
# 1  
Old 07-30-2016
Why this code is not working?

Hello,

I'm working with factorial code, and thought to do multiple parenthesis arithmetic expression.

So, I got the code working with the following code:
Code:
read -p "Enter number: " n
d=1 cnt1=$n cnt2=$cnt1
while [ $cnt2 -gt 1 ]
do
 cnt2=$(($cnt2 - 1)) #eq1
 cnt1=$(($cnt1 * ($cnt2))) #eq2
done
echo $cnt1

And when I want to include eq1 in second argument of eq2 it doesn't work!

Why?

Code:
 read -p "Enter number: " n
cnt=$n
cnt1=$cnt
while [ $cnt1 -gt 1 ]
do
cnt=$(($cnt * ($cnt1 - 1)))
done
echo result= $cnt cnt1=$cnt1

# 2  
Old 07-31-2016
in the second code sample you never decrease the loop variable cnt1 so the loop will never finish
# 3  
Old 07-31-2016
how?

Is not the second part of the equation does the decrementing?

Code:
cnt=$(($cnt * ($cnt1 - 1)))

I think the part on the right does the same decrementing process as in:

Code:
cnt2=$(($cnt2 - 1)) #eq1
 cnt1=$(($cnt1 * ($cnt2))) #eq2

so what I wanted to do is to include eq1 in eq2.

I don't see much difference, only combining 2 equations in one.

What I'm missing here?
# 4  
Old 07-31-2016
These are not equations, but assignments. In the first example, the value is never assigned to cnt1 (only to variable cnt), whereas in the 2nd example, it is..
# 5  
Old 07-31-2016
In the first code the loop counter which is cnt2 and assigned by cnt1
In the second code the loop counter is cnt1 and assigned by cnt

I have three variables in both codes

So In the first code decrementing
Code:
The first code: cnt2=$(($cnt2 - 1))

is before the main arithmetic expression
Code:
The first code: cnt1=$(($cnt1 * ($cnt2)))

In the second code I want to combine decrementing and main expressions in one arithmetic expression
Code:
The second code: cnt=$(($cnt * ($cnt1 - 1)))

The part on the right of the second expression should do the same operation as in the decrementing line of the first code; I think they are the same.

---------- Post updated at 03:38 AM ---------- Previous update was at 01:41 AM ----------

OK I got it!!

You're right the internal variable cnt1 is not assigned to be updated, so I read in a website about assigning variables.

Code:
ead -p "Enter number: " n
  2 
  3 cnt=$n
  4 cnt1=$cnt
  5 while [ $cnt1 -gt 1 ]
  6 do
  7 cnt=$(((cnt * (cnt1 -= 1 ))))
  8 done
  9 
 10 echo result= $cnt cnt1=$cnt1

This is the result to get the correct answer.

Thank you,

Last edited by wolfrose; 07-31-2016 at 03:47 AM..
# 6  
Old 07-31-2016
Quote:
Originally Posted by wolfrose
In the first code the loop counter which is cnt2 and assigned by cnt1
In the second code the loop counter is cnt1 and assigned by cnt

I have three variables in both codes

So In the first code decrementing
Code:
The first code: cnt2=$(($cnt2 - 1))

is before the main arithmetic expression
Code:
The first code: cnt1=$(($cnt1 * ($cnt2)))

In the second code I want to combine decrementing and main expressions in one arithmetic expression
Code:
The second code: cnt=$(($cnt * ($cnt1 - 1)))

The part on the right of the second expression should do the same operation as in the decrementing line of the first code; I think they are the same.

---------- Post updated at 03:38 AM ---------- Previous update was at 01:41 AM ----------

OK I got it!!

You're right the internal variable cnt1 is not assigned to be updated, so I read in a website about assigning variables.

Code:
ead -p "Enter number: " n
  2 
  3 cnt=$n
  4 cnt1=$cnt
  5 while [ $cnt1 -gt 1 ]
  6 do
  7 cnt=$(((cnt * (cnt1 -= 1 ))))
  8 done
  9 
 10 echo result= $cnt cnt1=$cnt1

This is the result to get the correct answer.

Thank you,
do you find the following code (which also works for the case where n is 1) easier to understand:
Code:
#!/bin/bash
read -p "Enter number: " n
fact=1
i=1
while [ $i -lt $n ]
do	fact=$((fact * (i += 1)))
done
echo "$n! is $fact"

I assume that you're using bash because read -p isn't in the standards and is not accepted by ksh (which would do this with read n?'Enter number: ').

Note, however, that if you want to compute factorials larger than 25! on a machine with 64-bit long ints, you'll need to use something with arbitrary precision arithmetic (such as bc or dc) instead of relying on shell arithmetic.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

"SQLPLUS -S " is not working in one environment where same code is working in another

"SQLPLUS -S " is not working in one environment where same code is working in another getting below error =================================== SQL*Plus: Release 11.2.0.3.0 Production Copyright (c) 1982, 2011, Oracle. All rights reserved. Use SQL*Plus to execute SQL, PL/SQL and SQL*Plus... (1 Reply)
Discussion started by: yogendra.barode
1 Replies

2. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

3. Shell Programming and Scripting

How to make this code working?

Hi Gurus, I wrote a simple code, but it doesn't work, can body help me to fix the issue. awk -F',' 'BEGIN{n=0}{ NR == FNR {fname;next} { if ($3==fname) n=1 } END{if n==0} }' tmpsrc srcfile.txt Thanks in advance (4 Replies)
Discussion started by: ken6503
4 Replies

4. Programming

MySQL LIKE code not working in safari browser

I am looking for database PHP Code: SELECT * FROM table_name WHERE event LIKE '%" . $search . "%' OR date LIKE '%”. $search . "%' This works fine in Firefox and in IE, but when i try it in safari, it seems to pull up the right results but then straight away changes and... (1 Reply)
Discussion started by: AimyThomas
1 Replies

5. Shell Programming and Scripting

Code to remove files when corresponding file doesnt exist isnt working.

I am trying to add some code to the begging of a script so that it will remove all the .transcript files, when their is no coressponding .wav file. But it doesnt work. This is the code I have added: for transcriptfile in `$voicemaildir/*.transcript`; do wavfile=`echo $transcriptfile | cut -d'.'... (2 Replies)
Discussion started by: ghurty
2 Replies

6. Shell Programming and Scripting

grep in perl code not working

I am creting a script to delete files from /tmp directory. I have following code but fails to find file name start with id 2754. What is wrong in : grep { /^(new_grp*|^$fleidb\_*/)/ } readdir(DIR); #!/usr/bin/perl my $dir = '/tmp'; my $fleidb = "2754"; print "$fleidb\n";... (1 Reply)
Discussion started by: dynamax
1 Replies

7. Shell Programming and Scripting

awk code not working in some HP versions

Dear all, Some one please help in solving this awk issue in HP. The below code was working fine in HP version B.11.11 U 9000/800 but when the same was run B.11.31 U ia64 it failed. :o. if awk 'BEGIN{if ('$var1' > 80); else exit 1}' then echo "Greater" fi (6 Replies)
Discussion started by: engineer
6 Replies

8. UNIX Desktop Questions & Answers

ftp return code not working

below is my code , but for some reason the return part is not working, only file transfer is happening and no exit status is checked .please me help me to fix this code #!/bin/sh #set -vx ftp -nv sitelocation << ! user username password lcd localdir cd /remote dir mget *.* ... (4 Replies)
Discussion started by: gwrm
4 Replies

9. Shell Programming and Scripting

Prime Generation Code Not Working Properly

My code for the generation of prime numbers from 2 to a value(given at run time) as fallows is not working. Can you assist me? clear echo "Enter the Maximum Number" read max for(( i=2; i <= $max; i++ )) do if then break else for(( j=i; j <= $max/2; j++ )) do if then break else echo... (1 Reply)
Discussion started by: ash.g
1 Replies

10. Programming

Code working AIX 5.2 and not in Solaris 5.9

Hi, When i run the below code in AIX it runs and solaris not ... why ??? #include <stdio.h> #include <string.h> #define MAX 1 int main () { char str ="1,2,3,4,5"; char * pch,b; int a; printf ("Enter the int to be searched ",str); scanf("%d",&a); sprintf(b,"%d",a); ... (2 Replies)
Discussion started by: vijaysabari
2 Replies
Login or Register to Ask a Question