GCD Homework. Untraced error with an "if"


 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions GCD Homework. Untraced error with an "if"
# 1  
Old 10-11-2011
Java GCD Homework. Untraced error with an "if"

1. The problem statement, all variables and given/known data:

Making a small GCD calculator. It's fully written, but i'm getting an error that i can't trace.

2. Relevant commands, code, scripts, algorithms:

I'm relatively new to both UNIX and Linux and slightly less new at programming. But I can't figure out why this won't run properly.

It's meant to calculate the GCD of two numbers (simple enough, you'd think). And I designed it myself and it looks good to me and my instructor won't respond. If anyone could help me out, it would be appreciated.


Code:
#! /bin/bash

#Takes arguments to determin the GCD

errorNum="Too many or too few arguments passed."
errorUse="Usage: $0 [decimal] [decimal]"
errorNeg="Cannot use a negative number."

checkIfZero ()
{
if [ $a=0 ]
then
drop=1
elif [ $b=0 ]
then
drop=1
else
drop=0
fi
exit 0
}

if [ $1 < 1 -o $2 < 1 ]
then
echo $errorNeg
echo $errorUse
exit 0
fi

if [ $# -le 1 -o $# -gt 2 ]
then
echo $errorNum
echo $errorUse
exit 0
elif [ $1 -ge 0 ] && [ $2 -ge 0 ]
then
a=$1
b=$2
drop=0
while [ $a -gt 0 ] || [ $b -gt 0 ]
do
if [ $a -gt $b ]
then
if [ drop -gt 0 ]
then
checkIfZero
c=`expr $a % $b`
a=$c
fi
elif [ $b -gt $a ]
then
if [ drop -gt 0 ]
checkIfZero
c=`expr $b % $a`
b=$c
fi
fi
done

if [ $a -gt $b ]
then
echo "The GCD is $a."
else
echo "The GCD is $b."
fi
fi

3. The attempts at a solution (include all code and scripts):

tried set -x and various echo commands to find the problem

[b]4.
AACC, Arnold Maryland, USA, Brandon Brisbon, CSI-135-401:I tried to post the url to the class, but it said i need to post 5 times before i could (sorry)

Moderator's Comments:
Mod Comment Video tutorial on how to use code tags

Last edited by pludi; 10-11-2011 at 06:46 PM..
# 2  
Old 10-11-2011
1) indent your code please
2) use $drop instead of drop in if condition
3) space (_) matter in if [ ..._=_... ] condition
# 3  
Old 02-13-2012
Adding to the above corrections, use "-lt" instead of "<".
Also the script you wrote will be in an infinite loop. There will be no changes to $a and $b since $drop is always equal to zero. Restructuring yours to work would be impossible, hence i wrote a new one::

Code:
if [ $# -lt 2 ]
then
           echo "Wrong usage"
           exit -1
elif [ $1 -le 0 -o $2 -le 0 ]
then
           echo "Invalid args"
           exit -1
fi

#get the smallest of 2 args and store it in $div

a=$1
b=$2
if [ $a -lt $b ]
then
           div=$a
elif [ $b -lt $a ]
then
           div=$b
else
           div=$a
fi
#loop from $div to 1 till $div divides both $a and $b completly

while [ $div -gt 0 ]
do
           remA=`expr $a % $div`
           remB=`expr $b % $div`
           if [ $remA -eq 0 -a $remB -eq 0 ]
           then
                      echo "GCD of $a and $b is $div"
                      exit 0
           fi
           div=`expr $div - 1`
done
echo "GCD of $a and $b is 1"
exit 0


Last edited by george88; 02-13-2012 at 01:39 PM..
# 4  
Old 02-14-2012
A humble bash code. And much quicker too.

Code:
#! /bin/bash
y=$1; r=$2
while [ $r -ne 0 ]
do
    gcd=$r
    r=$(($y % $r))
    y=$gcd
done
echo $gcd

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

5. UNIX for Dummies Questions & Answers

Unix "look" Command "File too large" Error Message

I am trying to find lines in a text file larger than 3 Gb that start with a given string. My command looks like this: $ look "string" "/home/patrick/filename.txt" However, this gives me the following message: "look: /home/patrick/filename.txt: File too large" So, I have two... (14 Replies)
Discussion started by: shishong
14 Replies

6. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

7. Shell Programming and Scripting

cat $como_file | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g'

hi All, cat file_name | awk /^~/'{print $1","$2","$3","$4}' | sed -e 's/~//g' Can this be done by using sed or awk alone (4 Replies)
Discussion started by: harshakusam
4 Replies

8. HP-UX

ERROR: more than one instance of overloaded function "vprintf" has "C" linkage

Hi people! I've got this own library: -------------------------------------------- Personal.h -------------------------------------------- #ifdef __cplusplus extern "C" { #endif #include <stdio.h> #include <stdarg.h> #include <string.h> ... (0 Replies)
Discussion started by: donatoll
0 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question