Home > other >  C output producing random long integers
C output producing random long integers

Time:07-02

I recently coded a question, and I have debugged and figured out where the problem is but cannot quite place a finger on it. My program aims to merge groups of sectors if they overlap or are next to each other, but the last section is scrambling the output.

#include <cstdio>
using namespace std;

int main() {
freopen("beachin.txt", "r", stdin);
freopen("beachout.txt", "w", stdout);

int n, u, k, x;
scanf("%d %d %d %d", &n, &u, &k, &x);
int umbrellas[u][2];
for (int i = 0; i < u; i  ) {
    scanf("%d %d", &umbrellas[i][0], &umbrellas[i][1]);
}

int groups[u][2];
int len = 0;
for (int i = 0; i < u; i  ) {
    int s1 = umbrellas[i][0];
    int e1 = umbrellas[i][1];
    int found = false;
    for (int a = 0; a < len; a  ) {
        int s2 = groups[a][0];
        int e2 = groups[a][1];
        if ((s1 <= e2 && s1 >= s2) || (e1 >= s2 && e1 <= e2) || (s1 <= s2 && e1 >= e2)) {
            int start, end;
            if (s1 < s2) start = s1;
            else start = s2;
            if (e1 > e2) end = e1;
            else end = e2;
            groups[a][0] = start;
            groups[a][1] = end;
            found = true;
        }
    }
    if (found == false) {
        len  ;
        groups[len][0] = s1;
        groups[len][1] = e1;
    }
}

int largest = 0; // scramble bit here from for loop
for (int i = 0; i < len; i  ) {
    int current = groups[i][1]-groups[i][0] 1;
    if (current >= largest) largest = current;
}


printf("%d\n", largest);
return 0;
}

My input for this is

7 2 5 2
4 5
5 6

The expected answer is 2, but the output varies between 2 and random long numbers like 1550870207.

Does anyone know why this is happening?

Note: If it's any help, problem statement is here

CodePudding user response:

Pointing out some issues with the code above, many of which you can get reported by using compiler options:

  • umbrellas[u][2] and groups[u][2]: these are variable length arrays; they're not standard C .
  • n, k, and x are declared, read from, but never used later on.
  • groups is 1) not initialized, 2) filled only for groups[1], so that groups[0] values remain undefined (at if (found == false) block), and 3) accessed both for groups[0] and groups[1] (at bottom for loop).
  • len is incremented before being used; in the second iteration of the top for loop, the if (found == false) loop is executed twice, so the second time you access groups[2]; that's an out of bounds access.

Check it here. I've modified your code initializing umbrellas at the point of declaration, and using fixed length arrays for umbrellas and groups.

#include <fmt/core.h>

int main() {
    int umbrellas[2][2]{ { 4, 5 }, { 5, 6 } };

    int groups[2][2]{};
    int len{};
    for (int i = 0; i < 2; i  ) {
        fmt::print("*** i = {}\n", i);
        int s1 = umbrellas[i][0];
        int e1 = umbrellas[i][1];
        bool found{ false };
        for (int a = 0; a < len; a  ) {
            int s2 = groups[a][0];
            int e2 = groups[a][1];

            if ((s1 <= e2 && s1 >= s2) ||
                (e1 >= s2 && e1 <= e2) ||
                (s1 <= s2 && e1 >= e2)) {

                int start{ (s1 < s2) ? s1 : s2 };
                int end{ (e1 > e2) ? e1 : e2 };
                groups[a][0] = start;
                groups[a][1] = end;
                found = true;
            }
        }
        if (not found) {
            len  ;
            fmt::print("\tAccessing groups[{}]\n", len);
            groups[len][0] = s1;
            groups[len][1] = e1;
        }
    }

    int largest = 0;  // scramble bit here from for loop
    for (int i = 0; i < len; i  ) {
        int current = groups[i][1] - groups[i][0]   1;
        if (current >= largest) {
            largest = current;
        }
    }
    fmt::print("==> largest = {}\n", largest);
}

// Outputs:
//
//   *** i = 0
//      Accessing groups[1]
//   *** i = 1
//      Accessing groups[2]
//   ==> largest = 2
  •  Tags:  
  • c
  • Related