473,326 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Help with C programming code

Hi! I'm having a real difficult time trying to figure out what is
wrong with my program code. I'm trying to make a "calculator" in the C
language and I only know basic stuff. Here's my code:

#include <stdio.h>

/* global variables */

int e, f;

/* function declarations */

int reduceResultRational(int e, int f); /* change the global result
rational number to its reduced format */
void printRational(int e, int f); /* print a rational number */
void rAdd(int a, int b, int c, int d); /* do rational number addition
*/
void rSubtract(int a, int b, int c, int d); /* do rational number
subtraction */
void rMultiply(int a, int b, int c, int d); /* do rational number
multiplication */
void rDivide(int a, int b, int c, int d); /* do rational number
division */

/* function definition */

int reduceResultRational(int e, int f)
{
int a, b, tmp, r;
a = e;
b = f;

if (b a)
{
tmp = b;
b = a;
a = tmp;
}

r = a % b;

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

return b;
}

void printRational(int e, int f)
{
if( f < 0 )
{
e *= -1;
f *= -1;
printf("The current result is: %d / %d\n", e, f);
}
else if( e == 0 )
{
printf("The current result is: 0\n");
}
else if( f == 1)
{
printf("The current result is: %d\n", e);
}
else if( e == f)
{
printf("The current result is: 1\n");
}
else
{
int a, b, c, d;
a = e;
b = f;
c = a;
d = b;

c = (a / reduceResultRational(e, f));
d = (b / reduceResultRational(e, f));

printf("The current result is: %d / %d\n", c, d);
}
}

void rAdd(int a, int b, int c, int d)
{
e = (a*d) + (b*c);
f = b*d;
}

void rSubtract(int a, int b, int c, int d)
{
e = (a*d) - (b*c);
f = (b*d);
}

void rMultiply(int a, int b, int c, int d)
{
e = (a*c);
f = (b*d);
}

void rDivide(int a, int b, int c, int d)
{
e = (a*d);
f = (b*c);
}

int main()
{
int c, d, menu = 0; /* local variables */

printf("Welcome to my rational number calculator!\n");
printf("Please input a rational number.\n");
printf("Numerator: ");
scanf("%d", &e);
printf("Denominator: ");
scanf("%d", &f);

while(menu != 5)
{

printf("--------------------------------------------\n");
printRational(e, f);
printf("1. Add the current result with another rational
number;\n");
printf("2. Subtract the current result from another
rational number;\n");
printf("3. Multiply the current result with another
rational number;\n");
printf("4. Divide the current result by another
rational number;\n");
printf("5. Quit\n");
printf("\n");
printf("Please enter a selection: ");
scanf("%d", &menu);

switch( menu )
{
case 1:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);

rAdd(e, f, c, d);
break;
case 2:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);

rSubtract(e, f, c, d);
break;
case 3:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);

rMultiply(e, f, c, d);
break;
case 4:
printf("Please input a rational
number.\n");
printf("Numerator: ");
scanf("%d", &c);
printf("Denominator: ");
scanf("%d", &d);

rDivide(e, f, c, d);
break;
case 5:
default:
break;
}
}
return 0;
}
As you know in math, the numerator is negative to signify a fraction is
negative, not the denominator. On one of the outputs, the result is
outputted as the denominator as the negative number. But in my code, I
specified that if the denominator were to be negative, multiply the
numerator by -1 and the denominator by -1 to switch the negative signs.
Here's what is being outputted:

The current result is: 4 / 5
1. Add the current result with another rational number;
2. Subtract the current result from another rational number;
3. Multiply the current result with another rational number;
4. Divide the current result by another rational number;
5. Quit

Please enter a selection: 3
Please input a rational number.
Numerator: -9
Denominator: 14
--------------------------------------------
The current result is: 18 / -35
1. Add the current result with another rational number;
2. Subtract the current result from another rational number;
3. Multiply the current result with another rational number;
4. Divide the current result by another rational number;
5. Quit

if anyone can figure out what the error is in my code, PLEASE tell me?
I already wasted 3 hours trying to figure it out and I havent made any
progress.

Nov 13 '06 #1
1 1687
In article <11**********************@m73g2000cwd.googlegroups .com>,
KiMcHeE <ki************@gmail.comwrote:
>Hi! I'm having a real difficult time trying to figure out what is
wrong with my program code.
>int reduceResultRational(int e, int f)
{
int a, b, tmp, r;
a = e;
b = f;

if (b a)
{
tmp = b;
b = a;
a = tmp;
}

r = a % b;

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

return b;
}
>void printRational(int e, int f)
{
if( f < 0 )
{
e *= -1;
f *= -1;
printf("The current result is: %d / %d\n", e, f);
}
else if( e == 0 )
{
printf("The current result is: 0\n");
}
else if( f == 1)
{
printf("The current result is: %d\n", e);
}
else if( e == f)
{
printf("The current result is: 1\n");
}
else
{
int a, b, c, d;
a = e;
b = f;
c = a;
d = b;

c = (a / reduceResultRational(e, f));
d = (b / reduceResultRational(e, f));

printf("The current result is: %d / %d\n", c, d);
}
}
Suppose that when printRational() is called, that e < 0 and f 0.
Then you do -not- multiply the numerator and demoninator by -1 each.

Suppose you reach the calls to reduceResultRational(). In that
routine, if the first value (e) is negative and the second positive,
then the second will be greater than the first, so you are going to
exchange the values in a and b, leading to positive a and negative b.
You then proceed to r = a % b with b negative.

Now the question for you: what is the sign of a % b when b is negative?
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Nov 13 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: PHPkemon | last post by:
Hi there, A few weeks ago I made a post and got an answer which seemed very logical. Here's part of the post: PHPkemon wrote: > I think I've figured out how to do the main things like...
7
by: elena | last post by:
Apologies for this off-topic post. I'm a Java/C++ developer who is also studying psychology. I would really appreciate it if you would complete a survey that I'm using for a research project...
45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
10
by: Robert | last post by:
Where can i find a free web- based VC++ course? (also i've to dowanload it) I'm interested also in VB and C++ thanks, Robert
2
by: Jackson Yap | last post by:
can someone kind enough to help me look at the attached html and js file? Why is it that the javascript menu could not work at www.apchosting.net but could work at...
4
by: Weasel | last post by:
Hey everyone, My names John and i just recently joined this googl group, and I joined because i need some help. I'm currently a Sophomore in High school and i'm really interested in computer...
2
by: V e X y N | last post by:
OK, here's the deal, if anyone could help me with a link or two, I'd be delighted. I'm just learning C (I learned over the summer, so a few months), and I was wondering what (*free*) compilers are...
27
by: SK | last post by:
Hi I am trying to teach myself how to program in C. I am a physician hoping to be able to help restructure my office. Anyhow, I amhoping that the porblem I am having is simple to those much more...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
3
by: Ken Foskey | last post by:
I am a new VS and C# developer with 20 plus years programming experience and I am finding the database stuff incredibly frustrating. I have read programming c# 3.0 pretty much cover to cover, ...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.