Post: Suggestions on my homework [C programming]
03-09-2013, 03:38 AM #1
xd366
find me on twitter now
(adsbygoogle = window.adsbygoogle || []).push({}); So i have a homework due next week and i havent started it, so i wanted some suggestions on how to even start at it.
Basically what I have to do is:

    Read a fraction of the form a/b from the keyboard, reduce it to its lowest form, and print the answers. user enters 2/4 program prints 1/2

Details:
Prompt for a fraction, and then read the input from the keyboard.
Validate input, print a error message if invalid & allow for fraction to be re-entered.
Numerator & denominator are 32-bit signed integers.
The output must be a fraction of the form a/b. 6/3 will be printed as 2/1.


    Sample Program:

enter a fraction to reduce: this isnt a fraction
Invalid Input bro.

enter a fraction to reduce: 2/10

Here is your fraction: 1/5


notes: the teacher said to use this algorithim:
    int GCD(int a, int b) {
if(b==0)
return a;
else
return GCD(b, a % b);
}

its called the Euclid algorithim. its everywhere online, so that shouldnt be the hard part.

anyway, any help?
---------------------------
now i need my program to just check for valid inputs and i'm done. any help?

    
#include <stdio.h>
int getfraction(int n, int m)
{
int getfraction, remainder;
while (n != 0)
{
remainder = m % n;
m = n;
n = remainder;
}
getfraction = m;
return getfraction;
}

int main (int argc, const char * argv[]) {
int num1, num2;
int newNum1, newNum2;
char yesno;

for(;Winky Winky
{
printf("Enter a fraction you wish to reduce: ");
scanf("%d/%d", &num1, &num2);
newNum1 = num1 / getfraction(num1, num2);
newNum2 = num2 / getfraction(num1, num2);
printf("Your Fraction Reduces to: %d/%d \n", newNum1, newNum2);
printf("Do you want to reduce another fraction?(y/n): \t ");
scanf("%s", &yesno);
if (yesno == 'n'Winky Winky
{break;}
}}
(adsbygoogle = window.adsbygoogle || []).push({});
03-09-2013, 04:46 AM #2
Why do professors start with C? They should drop straight into C++ or start with a high level language such as Python.
03-09-2013, 09:26 AM #3
xd366
find me on twitter now
Originally posted by Macdaddy4sure View Post
Why do professors start with C? They should drop straight into C++ or start with a high level language such as Python.

idk my college starts with java then assembly language, now i'm taking a course that includes C and c++.
03-09-2013, 07:19 PM #4
aerosoul94
smooth like butter
why not just do:
int num, den;
scanf("%d /%d", &num, &den);

scanf returns the number of arguments it filled in. so all you have to do is check if it's less than 2 to see if it's bad input.
03-14-2013, 12:13 AM #5
xd366
find me on twitter now
updated my program. any help? Smile
03-14-2013, 02:58 AM #6
Originally posted by Macdaddy4sure View Post
Why do professors start with C? They should drop straight into C++ or start with a high level language such as Python.


YES! You know whats up!

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo