Home > Software design >  Why I get a wrong result in my C code and what can be the fault in my code?
Why I get a wrong result in my C code and what can be the fault in my code?

Time:10-19

I have got a fault in my code, but I do not know what it is. I go through my list and I want to calculate my maximum, but a bad result comes out. When the years are consecutive with a difference of 1 year and the height to the year is higher or equal than the year before, I increase the value of maxdb by one. So I would like to choose the maximum of this sequence when this happen.

But something went wrong because I get the 1 number for my result and you can see that the result have to be 2, because of these : 1992 230, 1993 232, 1994 232

What can be the fault in my code?

Input   (year,heigh)               
8                     
1960 208
1960 210
1973 210
1992 230
1993 232
1994 232
2010 238
2011 239
#include <iostream>

using namespace std;

int main()
{
    int N;
    cin >> N;

    int T[N];
    int Mag[N];

    for (int i = 0; i < N; i  )
    {
        cin >> T[i];
        cin >> Mag[i];
    }

    int maxdb = 0;

    for (int i = 0; i < N - 1; i  )
    {
        if (T[i   1] == T[i] && Mag[i] <= Mag[i   1])
        {
            maxdb  ;
        }
           
        cout << maxdb;
    }

    return 0;
}

CodePudding user response:

According to your input only one case will get if statement true. The first one and the second one both 1960 are equal and second condition 208 <= 210. Only that will work so it will increment from 0 to 1.

CodePudding user response:

It looks like you're looking for the longest sequence of consecutive years.

Let's clean your code up a bit by growing your two data points into a single chunk of data.

struct Info {
    int year;
    int height;
};

But let's also overload the << and >> operators to make reading in and printing the data easier.

struct Info {
    int year;
    int height;

    friend std::istream& operator>>(std::istream& in, Info& i) {
        in >> i.year >> i.height;
        return in;
    }

    friend std::ostream& operator<<(std::ostream& out, Info& i) {
        out << i.year << " " << i.height;
        return out;
    }
};

Now, in main we'll read an int describing how many data points there will be, and create a std::vector<Info> to hold those values.

int main() {
    int n;

    std::cout << "Number of lines of info: ";
    std::cin >> n;

    std::vector<Info> v(n);
}

We'll then use a loop to read in the info we need.

    for (auto &i : v) {
        std::cin >> i;
    }

Since your code works beest when years are sorted, let's do that using std::sort and a lambda.

    std::sort(v.begin(), v.end(), [](auto &a, auto &b) { return a.year < b.year; });

We can use another loop to print all of the data, and ensure it sorted.

    for (auto &i : v) {
        std::cout << i << endl;
    }

But now, the real meat of your problem. As I understand it, you wish to find the longest run of consecutive years. Well, let's keep track of the current run of consecutive years. We'll loop over the vector using an iterator and in this case only go to the next to last item, since we're comparing one Info struct against the next one.

If the current year is one less than the next year, we'll increment current_run, otherwise we'll zero it out.

    int current_run = 0;

    for (auto iter = v.begin(); iter != v.end() - 1; iter  ) {
        Info cur = *iter;
        Info next = *(iter   1);

        if (cur.year == next.year - 1) {
            current_run  ;
        }
        else {
            current_run = 0;
        }
    }

With your sample input, current_run will end up being 1. We need some way to store the current_run value before we wipe it out when a streak ends.

So we'll create a max_run variable initialized to 0.

    int current_run = 0;
    int max_run = 0;

    for (auto iter = v.begin(); iter != v.end() - 1; iter  ) {
        Info cur = *iter;
        Info next = *(iter   1);

        if (cur.year == next.year - 1) {
            current_run  ;
        }
        else if (current_run > max_run) {
            max_run = current_run;
            current_run = 0;
        }
        else {
            current_run = 0;
        }
    }

Now, with your sample input, the result in max_run is 2. Putting it all together:

#include <iostream>
#include <vector>
#include <algorithm>

struct Info {
    int year;
    int height;

    friend std::istream& operator>>(std::istream& in, Info& i) {
        in >> i.year >> i.height;
        return in;
    }

    friend std::ostream& operator<<(std::ostream& out, Info& i) {
        out << i.year << " " << i.height;
        return out;
    }
};

int main() {
    int n;

    std::cout << "Number of lines of info: ";
    std::cin >> n;

    std::vector<Info> v(n);

    for (auto &i : v) {
        std::cin >> i;
    }

    std::sort(v.begin(), v.end(), [](auto &a, auto &b) { return a.year < b.year; });

    for (auto &i : v) {
        std::cout << i << std::endl;
    }

    int current_run = 0;
    int max_run = 0;

    for (auto iter = v.begin(); iter != v.end() - 1; iter  ) {
        Info cur = *iter;
        Info next = *(iter   1);

        if (cur.year == next.year - 1) {
            current_run  ;
        }
        else if (current_run > max_run) {
            max_run = current_run;
            current_run = 0;
        }
        else {
            current_run = 0;
        }
    }

    std::cout << max_run << std::endl;
}
  • Related