Thread: code not working... help?

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    12

    code not working... help?

    First off I must stress that I am an absolute beginner. My friend and I were working on a piece of code that will sort an array using a "MAX" checker (i.e going through the array and looking for the highest value and comparing the rest of the values to it. every iteration the max value is set as the last in the array and the next iteration checks all values except for the last (of the previous iteration).
    we aren't done yet - I know this. But we cannot figure out why the very first iteration isn't working. We worked on this for hours!
    Please - all I want to know is why it isn't actually working. I ask that you not suggest any advanced options because we are just now learning the basics. If you can find 1-4 lines that are the problem I would be happy for the assistance!
    P.S - I know we used both array placings and pointers to describe the same things - it was an attempt to see if using the "arr[i]" format will correct the problem caused possibly by wrong pointer usage. We gave up in the middle...

    Code:
    #include <stdio.h>#include <stdlib.h>
    
    
    
    
    void Sort_array(int *_arr, int _n)
    {
        int max, i, j, index, temp;
        for (i = 0; i < _n; i++)
        {
                printf("%d ", _arr[i-1]);
        }
        putchar('\n');
        for (i = _n - 1 ; i > 0; i--);
        {
        max = *_arr;
        index = 0;
            for (j = 1; j < i+1; j++);
            {
                if (*(_arr + j) > max) 
                {
                    max = *(_arr + j);
                    index = j;
                }
            }
            temp = _arr[i];
            _arr[i] = max;
            _arr[index] = temp;
            printf("temp %d arr[i]=%d arr[index]=%d i=%d \n", temp,*(_arr + i),*(_arr + index), i);
        } 
        
        putchar('\n');
        for (i = 0; i < _n; i++)
        {
                printf("%d ", _arr[i-1]);
        }
        putchar('\n');
        return;
    } 
    
    
    
    
    
    
    
    
    int main(int argc, char **argv)
    {
        int i, noItems = argc - 1;
        int arr[20];
        
        for (i = 1; i < argc; i++)
        {
            arr[i-1] = atoi(argv[i]);
        }
        
        Sort_array (&arr[0], noItems);
        printf("the sorted array is: \n");
        for (i = 1; i < argc; i++)
        {
                printf("%d ", arr[i-1]);
        }
        
        putchar ('\n');
        /*printf("Array of %d size is {", noItems);*/
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    If changing from array to poiinter syntax had fixed the problem, that would mean either that your compiler is broken (as the two are equivalent) or that you implemented the change wrong.

    Look carefully at line 14. There is a semi-colon there which shouldn't be. The loop therefore sets i to zero, and does nothing else. All of the code between curly braces from line 13 to 30 is NOT part of the loop (your indenting does not make it so).

    It doesn't help that your loops printing the arrays are printing invalid elements.

    Also, get out of the habit of using variable names beginning with underscores. Such identifiers are reserved by the standard for use by the implementation (there are exceptions to that statement, but beginners are better off avoiding such identifiers completely). That means any code using such identifiers may behave unpredictably.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Mar 2014
    Posts
    12
    Quote Originally Posted by grumpy View Post

    Look carefully at line 14. There is a semi-colon there which shouldn't be.
    Yup. Downed by a semicolon. Literally the first thing to check. A hard-learned and valuable lesson!
    The program now works perfectly. Thanks a million!

    Also, get out of the habit of using variable names beginning with underscores. Such identifiers are reserved by the standard for use by the implementation (there are exceptions to that statement, but beginners are better off avoiding such identifiers completely). That means any code using such identifiers may behave unpredictably.
    Unfortunately this is demanded by our lecturers. It annoys the hell out of me but they are adamant.
    Thanks again.

    -- can't figure out how to mark the thread as "Solved". I guess this option is reserved only for mods..
    Last edited by Amos Bordowitz; 03-29-2014 at 07:28 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. my code is not working
    By turbot in forum C Programming
    Replies: 3
    Last Post: 01-28-2009, 05:23 AM
  2. working set code
    By chinu in forum C++ Programming
    Replies: 6
    Last Post: 04-13-2008, 08:44 PM
  3. Non-Working Code
    By SheilahT in forum C++ Programming
    Replies: 9
    Last Post: 09-19-2006, 04:04 PM
  4. Code not working
    By 182 in forum C++ Programming
    Replies: 22
    Last Post: 02-17-2006, 02:25 PM
  5. C code not working
    By D3ciph3r in forum C Programming
    Replies: 2
    Last Post: 05-27-2005, 04:13 PM