Runtime error sum of fractions

I have to sum two fractions and simplificate them. It works for some cases, but the system gives me a "Runtime error" when I submit. Any help?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

void somafrac(int na, int da, int nb, int db, int &nr, int &dr){
    
    //Calculo do mmc
    int a=da, b=db, r, mdc, mmc;

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

    mdc=a;
    mmc=(da*db)/mdc;
    
    //Valores não simplificados de nr e dr
    dr=mmc;
    nr=((mmc/da)*na+(mmc/db)*nb);
    
    //Simplificação de nr e dr
    a=nr;
    b=dr;

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

    mdcr=a;
    
    //dr e nr simplificados
    dr=dr/mdcr;
    nr=nr/mdcr;
}

int main (){

    int a, b, c, d, nr, dr;
    cin >> a >> b >> c >> d;

    somafrac(a, b, c, d, nr, dr);

    cout << a << "/" << b << "+" << c << "/" << d << "=" << nr << "/" << dr << endl;

    return 0;
}
So what cases does it crash for you? Why force us to guess? You will tend to get the fastest replies if you precisely explain how to reproduce your problem.

Please refrain from using so many 1 to 2-letter variable names, it makes your code, despite being relatively simple, harder to follow.
Instead of "a", how about "num" or "numerator1", for example.

The easiest way to make your program crash is to enter a 0 as the denominator, because it causes you divide by 0. Does that help?

PS: Prefer to declare variables as close to where you need them as possible.
for example, in
1
2
3
4
5
6
7
8
    int mdcr;
    while (b!=0){
       r = a%b;
       a = b;
       b = r;
    }

    mdcr=a;

It is a bit misleading to declare mdcr before a loop that has nothing to do with mdcr.
You can simplify your code to

1
2
3
4
5
6
7
8

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

    int mdcr=a;
Last edited on
Floating point exception (core dumped)


division por cero!


verificar las variables despues de la entrada.
Last edited on
Thanks for the advices Ganado. I can't provide the input that crashes because I really don't know. It's an automatic system that corrects. The imput cases I've tested are: 1/2+1/3=5/6; 1/10+1/10=1/5; 0/5+100/300=1/3; 5/4+6/8=2/1; All of them resulted the right output provided on the example.


The easiest way to make your program crash is to enter a 0 as the denominator, because it causes you divide by 0. Does that help?


I forgot to mention that is said the numerators are 0≤ numerator ≤1000, and the denominators are 1 ≤ denominator ≤1000. This is guaranteed on the description. So the program crashing by a 0 as denominator is not the problem.

I'll try to improve my variables declaration.
Last edited on
Give it some extra junk or a decimal and it bombs though, with a division by zero error.


$ ./fraction.exe
4.4
Floating point exception (core dumped)


$ ./fraction.exe
0
1
2
3
0/1+2/3=2/3

$ ./fraction.exe
3
2
1
0
Floating point exception (core dumped)


and below, why not to use cin and ints for user input:


$ ./fraction.exe
mudkip
0/-13232+1/-2145047904=0/1


anything that isn't a number effects all the inputs, not just one.
Last edited on
Ugh, I hate those automatic grader things... how it works is that another program's output is redirected to your program as input, and then your program's output is redirected to a file or stream that the grader program can then interpret.

The input cases I've tested are: 1/2+1/3=5/6; 1/10+1/10=1/5; 0/5+100/300=1/3; 5/4+6/8=2/1;


Are you positive you are receiving the exact input in the exact correct way?
i.e is the input "1 2 1 3" or is the input "1/2+1/3" or "1/2+1/3;"?

Are you positive you are printing your output in the exact correct way?
Can you give more specific directions as to what you should be outputting each time?
For example,
- should there be spaces between the = signs or + signs?
- do those semi-colons actually do anything?
- should the program be able to take in multiple inputs, separated by semi-colons?

If the program can take in multiple inputs, it would require a loop to work. But that's just a guess, I don't know what the exact requirements are.
Last edited on
Zaphraud, the imput for numerator is a natural number, 0≤numerator≤1000, and the imput for denominator is a natural number, 1≤denominator≤1000
Topic archived. No new replies allowed.