#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2014
    Posts
    1
    Rep Power
    0

    Help me with this program :D


    Hi, I`m new here.. I saw that here people learn new staff from professional programmers.. So.. I`m working at the a program but I can`t finish him.. What i do wrong? It's countsort.

    #include <stdio.h>
    #include <stdlib.h>
    int sort(int,int);
    int main () {
    int a[50],b[50],c[50],i,m,n;
    printf("how many numbers? ");
    scanf("%d",&m);
    for(i=0;i<m;i++){
    printf("a[%d]= ",i);
    scanf("%d",&a[i]);
    }
    printf("the string is: %d", sort(*a,n));
    system("pause");
    return 0;

    }

    int sort(int *a,int m){
    int b[m],c[m],i,j;
    for(i=0;i<m;i++)
    c[i]=a[i];
    for(i=0;i<m;i++)
    for(j=i+1;j<m;j++)
    if(a[i]<a[j])
    b[j]++;
    else
    b[i]++;
    for(i=0;i<m;i++) {
    a[b[i]]=c[i]; }
    return a[b[i]];
    }

    Ty a lot! :chomp:
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    The interface to your sort function

    int sort(int*,int);

    means that the first argument is the address of an integer;
    and that the second argument is an integer.

    You've passed two integers as arguments to sort.
    sort(*a,n)
    a is an array of integers. An array in c is an address, making

    *a the same as a[0] the same as *(a+0)

    and that's why I can rewrite
    scanf("%d",&a[i]); as scanf("%d",a+i);


    Other problems with the program:
    Test the return code from scanf. Did you receive an integer?
    Was m positive and less than 51?

    Otherwise I can't tell what the program should do, aside from "count sort". You can (I can, anyway, if its there) find count sort implemented in many languages at rosettacode.org
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,253
    Rep Power
    2222
    For general procedures, your first big mistake was to not use code tags, which are absolutely needed to preserve your code listing's indentation -- there are a few variant tags that do the same thing. Without code tags, your code is unreadable. Why would you expect us to take valuable time away from our professional work to try to puzzle through unreadable code; in the professional world, proper and consistent formatting of code is required.

    Second, your formatting is crappy. You are being inconsistent in your use of K&R format, which absolutely depends on being used consistently! Use something like the Allman indent style, which is much more readable than K&R is even with done right.

    Here is your code with code tags (formatting retrieved via the Reply button):
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int sort(int,int);
    int main () {
        int a[50],b[50],c[50],i,m,n;
        printf("how many numbers? ");
        scanf("%d",&m);
        for(i=0;i<m;i++){
                         printf("a[%d]= ",i);
                         scanf("%d",&a[i]);
                         }
        printf("the string is: %d", sort(*a,n));
        system("pause");
        return 0;
        
    }
    
        int sort(int *a,int m){
             int b[m],c[m],i,j;
             for(i=0;i<m;i++)
                             c[i]=a[i];
             for(i=0;i<m;i++)
                        for(j=i+1;j<m;j++)
                                if(a[i]<a[j])
                                     b[j]++;
                                else
                                     b[i]++;
             for(i=0;i<m;i++) {
                             a[b[i]]=c[i]; }
        return a[b[i]];
                                          }
    Here is that unreadable mess properly formatted using Allman style:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int sort(int,int);
    
    int main () 
    {
        int a[50],b[50],c[50],i,m,n;
        
        printf("how many numbers? ");
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
            printf("a[%d]= ",i);
            scanf("%d",&a[i]);
        }
        printf("the string is: %d", sort(*a,n));
    
        system("pause");
        return 0;
    }
    
    int sort(int *a,int m)
    {
        int b[m],c[m],i,j;
        
        for(i=0;i<m;i++)
            c[i]=a[i];
        for(i=0;i<m;i++)
            for(j=i+1;j<m;j++)
                if(a[i]<a[j])
                    b[j]++;
                else
                    b[i]++;
        for(i=0;i<m;i++) 
        {
            a[b[i]]=c[i]; 
        }
        
        return a[b[i]];
    }
    Immensely better. You can even see the structure of your code, which was previously hidden.

    Proper and clear formatting is absolutely essential to programming because it makes your code readable. Why is that important? Because humans need to be able to read and understand it. The most important person who needs to be able to read and understand your code is you. You would not believe how many posts we have gotten caused by really stupid mistakes made because the programmer couldn't even read his own code.

    Your third mistake is that you ignored the warnings that your compiler was throwing at you. Never ignore warnings!

    Using MinGW gcc on WinXP Pro with all warnings turned on (note the -Wall option):
    C:TEST>gcc -Wall brutalheart01.c
    brutalheart01.c: In function `main':
    brutalheart01.c:8: warning: unused variable `c'
    brutalheart01.c:8: warning: unused variable `b'
    brutalheart01.c: At top level:
    brutalheart01.c:24: conflicting types for `sort'
    brutalheart01.c:4: previous declaration of `sort'

    C:TEST>
    Note in particular the warning on Line 24:
    brutalheart01.c:24: conflicting types for `sort'
    brutalheart01.c:4: previous declaration of `sort'


    Lines 23-24 is where you actually declare the function:
    Code:
    int sort(int *a,int m)
    {
    Line 4 is where you had prototyped it:
    Code:
    int sort(int,int);
    Obviously, the declaration does not match the prototype. And because of the messed up prototype, the compiler didn't flag your mistake in Line 17 where you call that function:
    Code:
        printf("the string is: %d", sort(*a,n));
    The compiler's warnings pointed you directly to the problem, but you to ignore them instead. So you wasted a lot of time and experienced a lot of frustration banging your head against the wall. All because you ignored what the compiler was telling you. Then you wasted even more time by posting a question on a forum and waiting for somebody to respond, when you already had the answer right there in front of you. So why did you ignore the compiler and waste all that time only for b49P23TIvg to come along and tell you the same thing that your compiler was telling you?

    Oh, didn't you turn warnings on? Well that is a completely idiotic and self-defeating practice! Always turn warnings on and up! Warnings are much more important then error messages! Never ignore warnings!
    Last edited by dwise1_aol; January 7th, 2014 at 11:50 AM.
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481

    Additionally


    a) you called sort with n. n is uninitialized. I'm sure you intended m.

    b) sort uses arrays b and c. Unless you're compiling with C++ in which case I don't know the answer, b and c start with whatever data happens to be on the stack. In other words, they are uninitialized. You did copy a to c. b remains uninitialized. You could initialize it with
    Code:
    int sort(int*a,int m) {
      int b[m],c[m],i,j;
      for(i=0;i<m;i++) {
        b[i] = 0;  /* set values of array b to 0 */
        c[i] = a[i];
      }
    I'd use memcpy and memset.

    And, since I'm stalling stalling stalling, at the end of this loop
    Code:
        for(i=0;i<m;i++) 
        {
            a[b[i]]=c[i]; 
        }
    i equals m hence
    b[i] is out-of-bound addressing. In other words, wrong.



    (ps. if you fix these problems the algorithm is correct. it sorts the data. congratulations!)
    Last edited by b49P23TIvg; January 7th, 2014 at 01:51 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo