# Need some help

Discussion in 'Mac Programming' started by DoodleNoodle, Nov 15, 2011.

1. Nov 15, 2011
Last edited by a moderator: Nov 15, 2011

### DoodleNoodle macrumors newbie

Joined:
Oct 25, 2011
#1
Hey guys, rookie programmer here, need some help for a program due. Excuse me for the triviality of this program, remember we all have to start somewhere.

The topic: Write a program that asks the user to enter a fraction of two nature numbers, and then
reduces the fraction to lowest terms:
Enter a fraction of natural numbers: 18/6
The lowest term is: 3/1
Note: Youre required to check the validity of the input, i.e. the input should be non-negative
and the denominator should ≠ 0. If not, print a hint and let the user re-input.
Hints: 1. Use a loop to check the numbers until they are valid.
2. To reduce a fraction, first compute the greatest common divisor (GCD). A classic
algorithm for computing GCD is the Euclidean algorithm. It goes as follows: Let and be the
two numbers, . Compute the remainder when is divided by . Copy into and
copy the remainder into . If is 0, then stop and contains the GCD. Otherwise, repeat this
process. For detail, please refer to http://en.wikipedia.org/wiki/Euclidean_algorithm

I've never used a loop before, but right now here's what i got. :

Code:
```#include <stdio.h>

main(void)
{

int a,b,c,d,e,f,g;

scanf_s("%d/n",&a);
scanf_s("%d/n",&b);

while(c!=0)
{

c=a%b;
c=a%c;
```
Kind of get lost here. I know what I want to do, just not entirely sure on how to get there. Right now I'm just trying to calculate the GCD, not really working on anything else till i get that. I have c=a%b, this is the remainder, then c=a%c, so something like 10/6, c=4, then c=2, then if i did a/c, would give me 5, meaning c is zero, then breaking the loop? excuse me once more, some help is needed, just a general push in the right direction would be appreciated!

EDIT: now i'm thinking it could go something like:

Code:
```	do{
c=a%b;
a=b;
b=c;}
while(c>0);
```
This did not work, but am I headed in the right track?

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#2
In cases like this the best way (IMHO) to debug is to either use the debugger and step and inspect the variables as you go along or alternatively liberally insert printfs throughout to trace the flow of information.

Plug in some concrete values of a and b and see what you get.

You could try some of the examples from the wikipedia article you linked.

B

3. ### DoodleNoodle thread starter macrumors newbie

Joined:
Oct 25, 2011
#3
not sure what you mean by that. I understand how I can get the GCD, but I cannot figure out how write a program for it. Right now I'm looking at,

c=a%b;
while(c!=0){ //while c is not equal to zero
a=b;
b=c;
c=a%b;
}

I'm not sure how to make my loop work to how i want it.

for something like 9/6, 9=a, 6=b. c would equal 3(not zero), then would go to the loop body, a is now 6, b is now 3, c is zero, so now c is equal to zero, thus leaving the loop. how do i leave the loop with c being equal to 3?

### Staff Member

Joined:
Aug 16, 2005
Location:
New England
#4
Does c ever contain the gcd?

Try it with another pair of numbers and see what you get...

B

5. ### DoodleNoodle thread starter macrumors newbie

Joined:
Oct 25, 2011
#5
a hint was posted by the TA:

1. Initialize integer m and n
2. Do: m / n , and save remainder
3. Do: m = n, n = remainder
4. If n = 0, end and return m as the GCD
5. Else: repeat step 2 ~ 4

so now i changed to:

while(b!=0){
c=a%b;
a=b;
b=c;
}

so for something like a=24, b=10 i want the loop to go like this:
loop1: b!=0
c=14
a=10
b=14
loop2: since b!= 0
c=10
a=14
b=10
loop3:
c=4
a=10
b=4
loop4:
c=2
a=4
b=2
loop5: now b=0, thus exit from loop, leaving b as the GCD equaling to 2.
c=0
a=2
b=0

when i did this, then tried compiling, gave me b = 0

6. ### DoodleNoodle thread starter macrumors newbie

Joined:
Oct 25, 2011
#6
while(b!=0){
c=a%b;
a=b;
b=c;
}
printf("%d",a);

wooooh, a is the GCD!

7. ### DoodleNoodle thread starter macrumors newbie

Joined:
Oct 25, 2011
#7
so now my problem is that it alters the values of my orginal a and b, this is a problem since the reduced fraction would be a(orginal)/a(gcd). how can i make this happen? i thought i read that you can define variables that are used just inside the loop?

8. ### DoodleNoodle thread starter macrumors newbie

Joined:
Oct 25, 2011
#8
Right now I'm at,

#include <stdio.h>

main(void)
{

int numerator, denominator, remainder, numerator2, denominator2, reduced_numerator, reduced_denominator;

printf("Enter a fraction of natural numbers (such as 18/6): ");
scanf_s("%d/%d", &numerator,&denominator);

if(denominator!=0){

if(numerator/denominator>=0){

numerator2=numerator;
denominator2=denominator;

while(denominator2!=0){
remainder=numerator2%denominator2;
numerator2=denominator2;
denominator2=remainder;
}

reduced_numerator=numerator/numerator2;
reduced_denominator=denominator/numerator2;

printf("The lowest term is: %d/%d", reduced_numerator,reduced_denominator);
}
else { printf("Please enter a positive fraction");
}
}
else{ printf("Invalid denominator, please enter a non-zero denominator");}
return 0;

}

everything works perfectly, but in the assignment, there is a "Note: Youre required to check the validity of the input, i.e. the input should be non-negative and the denominator should ≠ 0. If not, print a hint and let the user re-input"

how do i do this? the chapter this assignment is based off includes break, continue, goto, and null statements.