1

This two code I wrote should output the same thing, but I don't know why, when I use Function to write it, I have to put a totalDays[0] -= 1; in the line 13, and when I use Class to write it, it just works as I intended. (This problem only occurs with vscode, when I use Dev c++, it just works fine without line 13)

Sample input:
20101010
20101015
First code output: 4 (without line 13)
Second code output: 5 (correct output)

First code :

#include <iostream>
using namespace std;

const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

int countDays(int d[], int m[], int y[]) {
    int totalDays[2];
    for (int i = 0; i < 2; i++) {
        totalDays[i] += d[i] + 365 * y[i];
        for (int j = 0; j < m[i] - 1; j++)
            totalDays[i] += monthDays[j];
    }
    totalDays[0] -= 1;
    if (totalDays[0] > totalDays[1]) {
        return totalDays[0] - totalDays[1];
    } else {
        return totalDays[1] - totalDays[0];
    }
}

int main() {
    int d[2], m[2], y[2], yyyymmdd;
    for(int i = 0; i < 2; i++){
        cin >> yyyymmdd;
        d[i] = yyyymmdd % 100;
        m[i] = (yyyymmdd / 100) % 100;
        y[i] = yyyymmdd / 10000;
    }

    cout << countDays(d, m, y);

    return 0;
}

Second code :

#include <iostream>
using namespace std;

class Solution {
   public:
    const int monthDays[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    int countDays(int d[], int m[], int y[], int totalDays[]) {
        for (int i = 0; i < 2; i++) {
            totalDays[i] += d[i] + 365 * y[i];
            for (int j = 0; j < m[i] - 1; j++)
                totalDays[i] += monthDays[j];
        }

        if (totalDays[0] > totalDays[1]) {
            return totalDays[0] - totalDays[1];
        } else {
            return totalDays[1] - totalDays[0];
        }
    }
};

int main() {
    int d[2], m[2], y[2], yyyymmdd, totalDays[2];
    for(int i = 0; i < 2; i++){
        cin >> yyyymmdd;
        d[i] = yyyymmdd % 100;
        m[i] = (yyyymmdd / 100) % 100;
        y[i] = yyyymmdd / 10000;
        totalDays[i] = 0;
    }

    Solution ob;
    cout << ob.countDays(d, m, y, totalDays);

    return 0;
}
2
  • 1
    Afaik, c local variables might or might not be auto-initialized depending on the compiler and compiler options. You are defining "totalDays" without setting it to anything so a good chance you are getting random data there which you then compare. Commented Oct 9, 2021 at 7:00
  • 1
    Your code exhibits Undefined Behavior. int totalDays[2]; is an Uninitialized array when totalDays[i] += d[i] + 365 * y[i]; is called. At least do int totalDays[2] = {0}; Also compile with -Wshadow enabled, you shadow the array totalDays between main() and your function in the second example. Choose a new name for the parameter totalDays in the second case. Commented Oct 9, 2021 at 7:01

1 Answer 1

1

Answer from David C. Rankin 4 hours ago

Your code exhibits Undefined Behavior. int totalDays[2]; is an Uninitialized array when totalDays[i] += d[i] + 365 * y[i]; is called. At least do int totalDays[2] = {0}; Also compile with -Wshadow enabled, you shadow the array totalDays between main() and your function in the second example. Choose a new name for the parameter totalDays in the second case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.