Thread: Program not outputting to console

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    33

    Program not outputting to console

    I need some help... My code works completely fine in visual studio. It also is able to compile with no errors on the school unix system. However, it does not output anything to the school's terminal...

    I think it has to do with my sorting function, as when I comment it out, everything is outputted fine... Help please?

    Code:
    void sort(int* number, int n){
        ...
    
        // sort the first half
        for (int i = 0; i < split; i++) {
            for (int j = i + 1; j < split; j++) {
                if (firstHalf[i] > firstHalf[j]) {
                    int tmp = firstHalf[i];
                    firstHalf[i] = firstHalf[j];
                    firstHalf[j] = tmp;
                }
            }
        }
    
        // sort the second half
        for (int i = 0; i < split; i++) {
            for (int j = i + 1; j < split; j++) {
                if (secHalf[i] > secHalf[j]) {
                    int tmp = secHalf[i];
                    secHalf[i] = secHalf[j];
                    secHalf[j] = tmp;
                }
            }
        }
    
        // compare and combine
        int i = 0, j = 0, counter = 0;
        while (counter < n && (i <= split && j <= split)) {
            if (i == split) {
                while (j < split) {
                    number[counter] = secHalf[j];
                    j++;
                    counter++;
                }
            }
            else if (j == split) {
                while (i < split) {
                    number[counter] = firstHalf[i];
                    i++;
                    counter++;
                }
            }
            else if (firstHalf[i] < secHalf[j]) {
                // smaller value is from first half
                number[counter] = firstHalf[i];
                i++;
                counter++;
            }
            else if (secHalf[j] < firstHalf[i]) {
                // smaller value is from second half
                number[counter] = secHalf[j];
                j++;
                counter++;
            }
            else if (firstHalf[i] == secHalf[i]) {
                number[counter] = firstHalf[i];
                number[counter + 1] = secHalf[j];
                i++;
                j++;
                counter += 2;
            }
        }
    
        ...
    }
    
    int main(){
    
        ...
    
        /*It does not print out "Before sorting: ...". It will print it out if I use fflush(stdout)*/
        printf("Before sorting: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", number[i]);
        }
        
        /*It gets stuck here as it doesn't output the sorted results which is after this call*/
        sort(number, n);
    
        ...
    
        return 0;
    }
    Last edited by jjwooyoung; 10-10-2015 at 10:59 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    What was the point of editing out various bits of code?
    I mean, there are all sorts of ways of messing up declarations and initialisations.

    Post the WHOLE thing.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jul 2015
    Posts
    33
    I just thought those parts weren't important...

    Anyways, here's the entire sort function. Reminder: it compiled fine in both visual studio and on the school unix environment. Just doesn't output anything in the school env

    Code:
    void sort(int* number, int n){
        int split = n / 2;
        int* firstHalf = (int*) malloc(split * sizeof(int));
        int* secHalf = (int*) malloc(split * sizeof(int));
    
        for (int i = 0; i < split; i++) {
              firstHalf[i] = number[i];
        }
    
        for (int i = 0, j = split; j < n; i++, j++) {
             secHalf[i] = number[j];
        }
    
        // sort the first half
        for (int i = 0; i < split; i++) {
    
            for (int j = i + 1; j < split; j++) {
                if (firstHalf[i] > firstHalf[j]) {
                    int tmp = firstHalf[i];
                    firstHalf[i] = firstHalf[j];
                    firstHalf[j] = tmp;
                }
            }
        }
     
        // sort the second half
        for (int i = 0; i < split; i++) {
            for (int j = i + 1; j < split; j++) {
                if (secHalf[i] > secHalf[j]) {
                    int tmp = secHalf[i];
                    secHalf[i] = secHalf[j];
                    secHalf[j] = tmp;
                }
            }
        }
     
        // compare and combine
        int i = 0, j = 0, counter = 0;
        while (counter < n && (i <= split && j <= split)) {
            if (i == split) {
                while (j < split) {
                    number[counter] = secHalf[j];
                    j++;
                    counter++;
                }
            }
            else if (j == split) {
                while (i < split) {
                    number[counter] = firstHalf[i];
                    i++;
                    counter++;
                }
            }
            else if (firstHalf[i] < secHalf[j]) {
                // smaller value is from first half
                number[counter] = firstHalf[i];
                i++;
                counter++;
            }
            else if (secHalf[j] < firstHalf[i]) {
                // smaller value is from second half
                number[counter] = secHalf[j];
                j++;
                counter++;
            }
            else if (firstHalf[i] == secHalf[i]) {
                number[counter] = firstHalf[i];
                number[counter + 1] = secHalf[j];
                i++;
                j++;
                counter += 2;
            }
        }
     
        free(firstHalf);
        free(secHalf);
        firstHalf = NULL;
        secHalf = NULL;
    
    }
    As for the main function
    Code:
    int main(){
        int n = 20;
    
        int* number = (int*) malloc(n * sizeof(int));
    
        for (int i = 0; i < n; i++) {
            number[i] = (rand() % (20 - 0));
        }
    
       /*It does not print out "Before sorting: ...". It will print it out if I use fflush(stdout)*/
        printf("Before sorting: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", number[i]);
        }
    
       /*It gets stuck here as it doesn't output the sorted results which is after this call*/
        sort(number, n);
    
        printf("\nAfter sorting: ");
        for (int i = 0; i < n; i++) {
            printf("%d ", number[i]);
        }
    
        printf("\n");
    
        return 0;
    }
    Last edited by jjwooyoung; 10-10-2015 at 11:39 PM.

  4. #4
    Registered User
    Join Date
    Jul 2015
    Posts
    33
    I weird thing is, I copied that sorting algorithm I wrote into another program, and it compiled and output just fine on the school environment... It's the "compare and combine" section that's causing problem. If I copy it from the second program (the one that works) back into this one, it doesn't work...

    What is wrong???

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    A debug session
    Code:
    (gdb) print *number@20
    $3 = {0, 1, 2, 15, 13, 15, 6, 12, 9, 1, 2, 7, 10, 19, 3, 6, 0, 6, 12, 16}
    (gdb) print *firstHalf@10
    $4 = {1, 3, 6, 6, 9, 12, 13, 15, 15, 17}
    (gdb) print *secHalf@10
    $5 = {0, 2, 3, 6, 6, 7, 10, 12, 16, 19}
    (gdb) print i
    $6 = 1
    (gdb) print j
    $7 = 2
    (gdb) n
    43              if (i == split) {
    (gdb) 
    50              else if (j == split) {
    (gdb) 
    57              else if (firstHalf[i] < secHalf[j]) {
    (gdb) 
    63              else if (secHalf[j] < firstHalf[i]) {
    (gdb) 
    69              else if (firstHalf[i] == secHalf[i]) {
    (gdb) 
    42          while (counter < n && (i <= split && j <= split)) {
    (gdb) 
    43              if (i == split) {
    (gdb) 
    50              else if (j == split) {
    (gdb) 
    57              else if (firstHalf[i] < secHalf[j]) {
    (gdb) 
    63              else if (secHalf[j] < firstHalf[i]) {
    (gdb) 
    69              else if (firstHalf[i] == secHalf[i]) {
    (gdb) 
    42          while (counter < n && (i <= split && j <= split)) {
    The code just locks up in an infinite loop.



    > My code works completely fine in visual studio
    No, it just didn't run into this particular bug.
    Beware of declaring early victory in future.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Jul 2015
    Posts
    33
    Which condition caused it to lock up in an infinite loop? Is it that while loop?
    Last edited by jjwooyoung; 10-11-2015 at 10:16 AM.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    One problem that you have in debugging is that you use rand() to generate the input. This is fine when your code works, but having a different input each time means that you cannot say, take the input and try out your implementation manually to see where it went wrong.

    So, temporarily comment out the part that generates input and instead say, copy the input from Salem's post #5. Run your program with this input using the debugger and see what is the problem. If you don't quite get it, write down the input and sort it "by hand", following the code of your sort function line by line.

    EDIT:
    There is an alternative in that you could call srand() with a fixed seed, and looking at your code perhaps that is what you are doing (if you don't call srand it will be as if you called srand(1)), but because the implementation of srand and rand varies between standard library implementations, it would be better to use explicitly fixed input so that you test with the same input everywhere.
    Last edited by laserlight; 10-11-2015 at 10:33 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,667
    > Which condition caused it to lock up in an infinite loop? Is it that while loop?
    You have 5 conditions.
    When it locks up, all 5 conditions evaluate to false, so nothing actually happens to get the program out of that state.

    You might try adding a final
    Code:
    else {
        printf("Huh? how did this happen?\n");
    }
    FWIW, you have a typo - but I wasn't going to tell you exactly where it is in your code.
    That's for you to still stare at for a while.
    So you get the full impact of the "D'oh!!!!" moment when you finally realise it.

    There are enough clues in my debug log to help you figure it out.
    The rest is up to you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Jul 2015
    Posts
    33
    I think I found that typo Salem. It's in the last "else if" right? Gahh, it helped having a constant set of integers. As the ones that were generated in the ide worked but not the ones generated on the school environment.

    Thanks for the tip you two!
    Last edited by jjwooyoung; 10-11-2015 at 11:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to program mfc console app!
    By bos1234 in forum Windows Programming
    Replies: 0
    Last Post: 03-29-2011, 01:50 AM
  2. Flight program not outputting
    By bigmac(rexdale) in forum C++ Programming
    Replies: 1
    Last Post: 06-15-2008, 05:32 PM
  3. Help with console program
    By feso4 in forum C++ Programming
    Replies: 3
    Last Post: 10-22-2006, 11:53 AM
  4. Get the PID of a console program
    By BianConiglio in forum Windows Programming
    Replies: 6
    Last Post: 05-24-2004, 05:24 AM
  5. DOS program versus DOS console program
    By scromer in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 01-10-2002, 01:42 PM