0

I have this error when debugging my code, the problem is to print out the biggest fraction number:

For example:

1/2 5/8 12/9  

Expect:

12/9  

Reality: My code prints out all of the fraction numbers that I enter

1/2 5/8 12/9  

I just can't quite figure out what went wrong. How can I debug this?

The struct concept is just so confusing.

Here is my code:

#include <iostream>
#include <cmath>
using namespace std;
struct FractionNumber
{
    int Numerator;
    int Denominator;
    float FractionNumber;
};
void TypeIn(FractionNumber a[] ,int n)
{
    for (int i=0; i<n; i++)
    {
        cout << "Enter Numerator " << i+1 << ": ";
        cin >> a[i].Numerator;
        cout << "Enter Denominator " << i+1 << ": ";
        cin >> a[i].Denominator;
        a[i].FractionNumber=a[i].Numerator/a[i].Denominator;
    }
}
int FindBiggest(FractionNumber a[], int n)
{
    FractionNumber Biggest=a[0];
    for (int i=0; i<n; i++)
    {
        if(a[i].FractionNumber > Biggest.FractionNumber)
        {
            Biggest.FractionNumber = a[i].FractionNumber;
        }
    }
    return Biggest.FractionNumber;
}
void PrintBiggest(FractionNumber a[], int n)
{
    for (int i=0; i<n; i++)
    {
        if(a[i].FractionNumber = FindBiggest(a,n))
        {
            cout << "biggest FractionNumber: " << a[i].Numerator << "/" << a[i].Denominator << endl;
        }
    }
}
int main()
{
    int n;
    FractionNumber a[100];
    cin >> n;
    TypeIn(a,n);
    PrintBiggest(a,n);


return 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • struct name and member variable name same. Correct that. – seccpur Mar 17 '19 at 07:55
  • Hi there. A couple issues I'm seeing: (1) your FractionNumber member is a `float` but your return value for FindBiggest is `int`. (2) I don't understand the purpose of the loop in PrintBiggest (please clarify?). You can do without the loop. – TrebledJ Mar 17 '19 at 08:02
  • Please learn about formatting here: https://stackoverflow.com/editing-help Also please remember a few things you learned about English, e.g. capitalisation, making complete sentences, punctuation... . StackOverflow does not require perfect English (or I myself would be pretty lost), but you can do better than that, can't you? – Yunnosch Mar 17 '19 at 08:05
  • @seccpur Please explain why, that is not obvious. – Yunnosch Mar 17 '19 at 08:11
  • 1
    `a[i].FractionNumber = FindBiggest(a,n)` should read `a[i].FractionNumber == FindBiggest(a,n)` – kvantour Mar 17 '19 at 08:26
  • @kvantour You are right about the operator, but the whole construct is unneeded. Compare the decent answer below. – Yunnosch Mar 17 '19 at 08:29
  • @Yunnosch the answer below is good but does not answer the question of the OP _I just can't quite figur out what went wrong_. This is the main thing that went wrong. The answer below changes the complete program structure which does not help the OP with understanding his mistake, but only shows that it can be done differently leaving the OP with an unanswered question. And if you demonstrate that you can do it differently, why only so slightly different. Would it then not be more beneficial to write the best code you can to solve this problem, and not just a tiny hack? – kvantour Mar 17 '19 at 08:38
  • @kvantour I think (did not test) that 3) solves the problem (at least the one OP noticed...). I try to get the answerer to actually explain it... ;-) – Yunnosch Mar 17 '19 at 10:02
  • Possible duplicate of [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Raedwald Mar 18 '19 at 12:30

1 Answers1

0

This answer is pointing straight to accomplishing the goal, i.e. get the biggest number. I am not sure if you have tried this or not. Or you are just playing with structures, to understand it more.

Anyway, let's get into it & make a few changes...

You can check the difference between struct and typedef struct here

typedef struct 
{
    int Numerator;
    int Denominator;
    float FractionNumber;
}FractionNumber;

Make sure the denominator is not 0 (zero) (Such flows can be handled with try-catch blocks, but you can read on it yourself as these can cause the execution to terminate)

cout << "Enter Denominator " << i+1 << ": ";
bool check = false;
do {
   cin >> a[i].Denominator;
   if (0 == a[i]) {
      check = true;
   }else{
      check = false;
   }
} while (check);

The division operator should give out float

a[i].FractionNumber=static_cast<float>(a[i].Numerator)/static_cast<float>(a[i].Denominator);

The FindBiggest function is not needed for a simple comparison which I am assuming that you are trying to do.

The PrintBiggest can simply check for the Franction numbers to compare the biggest value and return its index

int PrintBiggest(FractionNumber *a, int n) {
    int index_of_biggest = 0;
    for (int i=0; i<n; i++) {
        if(a[i].FractionNumber > a[index_of_biggest].FractionNumber) {
            index_of_biggest = i;
        }
    }
    return(index_of_biggest);
}

And finally get this index to print your biggest number

int x = PrintBiggest(a,n);
cout << "Biggest is: " << a[x].Numerator << "/" << a[x].Denominator;
RC0993
  • 898
  • 1
  • 13
  • 44