Thread: code problem..

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    9

    code problem..

    can somebody tell me what is wrong here? tnQ

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
        int n;
        int niz[n];
        
        scanf("%d", &n); 
        
        for(int i = 0; i < n; i++) 
        scanf("%d", &niz[i]);
    
        
        for(int i = 0; i <n; i++)
        {
                int j = i + 1,k;
                if(niz[i] > niz[j])
                {
                k = niz[j];
                niz[j] = niz[i];
                niz[i] = k;        
                }
                }
    
        for(int i = 0; i < n; i++)
        printf("%d", niz[i]);
        
        getch();
        return 0;
        }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by broken_heart View Post
    can somebody tell me what is wrong here? tnQ
    int niz[n]

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You haven't given the size of the array niz. It must be hard-coded to some maximum number, or you have to allocate memory for it once you've determined how many elements you need.

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    so I just have to switch places of int niz[n] with scanf("%d", &n) Thanx and btw I have one more mistake which I've noticed moment ago sry.. this is remodeled code:

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void)
    {
        int n;
         scanf("%d", &n); 
        int niz[n];
        
       
        
        for(int i = 0; i < n; i++) 
        scanf("%d", &niz[i]);
    
        
        for(int i = 0; i <n; i++)
        {
                for(int j = i+1; j < n; j++)
                {
                int k;
                if(niz[i] > niz[j])
                {
                k = niz[j];
                niz[j] = niz[i];
                niz[i] = k;        
                }
                }
                }
    
        for(int i = 0; i < n; i++)
        printf("%d\t", niz[i]);
        
        getch();
        return 0;
        }
    tnQ you for respond
    Last edited by broken_heart; 09-18-2009 at 03:52 PM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    No it won't work. int niz[n] is is not legal no matter where it's placed.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nonoob View Post
    No it won't work. int niz[n] is is not legal no matter where it's placed.
    Except, of course, it is. (Look up: C Programming, not C++ Programming.)

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Declaring arrays with variable sizes (Variable Length Arrays) is part of the latest C standard, C99, as tabstop has indicated. But it's not part of the C89 standard, which most compilers still use (especially compilers which support getch()).

    I would suggest using dynamic memory allocation here. That's what it's for. For example:
    Code:
    #include <stdio.h>  /* for scanf(), etc. */
    #include <stdlib.h>  /* for malloc() and free() */
    
    int main() {
        int n;
        int *array;
        printf("How big do you want the array to be? ");
        scanf("%d", &n);
    
        array = malloc(sizeof(*array) * n);
    
        /* ... use array ... */
    
        free(array);
        return 0;
    }
    Regarding getch(): it's nonstandard and unportable. Don't use it, click here instead.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dwks View Post
    But it's not part of the C89 standard, which most compilers still use (especially compilers which support getch()).
    Don't know if I'd go that far, since getch could come from curses. More apt to say "compilers which use conio.h", I would guess.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You know what I mean.

    A suggestion to the OP: work on your indentation. If you write more than a few tens of lines with that sort of indentation, all you'll accomplish is to confuse yourself.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Variable length arrays are still stack-based and I didn't think they worked when they are attempted in main as in the example given. Only in functions.

    I could be wrong there too - as I completely forgot about variable-length-arrays. 1/2 point towards tabstop, voided by minus 1/2 point for his sarcasm.

  11. #11
    Registered User
    Join Date
    Sep 2009
    Posts
    9
    is it necessary to use free() function?
    Code:
    free(array);

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nope.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nonoob View Post

    I could be wrong there too - as I completely forgot about variable-length-arrays. 1/2 point towards tabstop, voided by minus 1/2 point for his sarcasm.
    Yay! Not negative!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code problem
    By sybariticak47 in forum C++ Programming
    Replies: 9
    Last Post: 02-28-2006, 11:50 AM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  4. Replies: 5
    Last Post: 12-03-2003, 05:47 PM
  5. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM