Thread: help!

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    17

    help!

    why wont this function work, it wont arrange numbers from high to low in the array?

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int sort();               /*taulukko järjestely funktio esittely*/
    
    int main()
    {
        int lkm=0;                         
        int i=1;
        int taulukko[127]={0};
        int summa=0;
        
        do
        {
            printf("Anna luku %d.", i);
            scanf("%d", &taulukko[i]);
            lkm++;
            i++;
        }while(taulukko[i-1]>0);
            
        lkm--;                             
        
        for(i=1; i<=lkm; i++)
        {
                 summa+=taulukko[i];
        }
    int sort();
    printf("\nlukujen keskiarvo on %d ja pienin syottama luku on %d", summa/lkm, taulukko[1]);
    
    getch();
    }
    int sort()                      /*taulukon järjestely funktio*/
    {
    int taulukko;     
    int g,r,c;
    for ( r=0; r <=8; r++)
       {
         for ( g=r+1;g<=9;g++)
            {
                if ( taulukko[r]  < taulukko[g] )
                   {
                       c=taulukko[r];                       // these 3 statements swap values
                       taulukko[r] = taulukko[g];          // in the 2 cells being compared  
                       taulukko[g] = c;
                    }
            }   
       }
    }
    Last edited by Fenix; 08-24-2009 at 01:02 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Firstly, you need to use descriptive topic names. "help!" is a bad topic name.

    Next: one big problem is that you are defining the sort function within the main function. Define it separately, and call it from the main function.

    You also should be declaring parameters with their types. Oh, and you should indent your code more consistently (though I see that you have tried to some extent, but that is not enough).
    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

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    Ok, ill promise to ask questions smart, if u please help me on this!

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    Well now that you've made that change, what problems are you having? Do you get error messages? Is the output faulty?

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    17
    it just dosent arrange the array. at the bottom of main function, where i print array "taulukko[1]", well, that should be the smallest given number since "0" is smallest and at [0] cause u have to press 0 to stop giving numbers. well it dosent print the smallest numberr, it just prints the first mnumber that u gave...

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    void sort(int *, int);               /*taulukko järjestely funktio esittely*/
    
    int main()
    {
        int lkm=0;                         
        int i=0;   //arrays start at zero in C
        int taulukko[127]={0};
        int summa=0;
        
        do
        {
            printf("Anna luku %d.", i);
            scanf("%d", &taulukko[i]);
            lkm++;
            i++;
        }while(taulukko[i-1]>0);
            
        lkm--;                             
            
        for(i=0; i<lkm; i++)
        {
                 summa+=taulukko[i];
        }
    
        sort(taulukko, lkm); 
        //you'll want to print or do other work with the sorted array, here
    
       printf("\nlukujen keskiarvo on %d ja pienin syottama luku on %d", summa/lkm, taulukko[1]);
    
       getch();
       return 0;  
    }
    void sort(int * taulukko, int lkm)                          /*taulukon järjestely funktio*/
    {
         int g,r,c;
        
        for ( r=0; r < lkm-1; r++)  //remember to minus 1, here
        {
            for ( g=r+1;g<lkm; g++)  
            {
                if ( taulukko[r]  < taulukko[g] )  // descending order
                {
                    c=taulukko[r];                       // these 3 statements swap values
                    taulukko[r] = taulukko[g];          // in the 2 cells being compared  
                    taulukko[g] = c;
                }
            }   
        }
    }
    That should give you a few suggestions for progress.

    Edit: I'm not sure this is working right since I can't understand your program's message.

    If you want the smallest number, just add:
    Code:
       printf("\nSmallest integer was: %d\n", taulukko[lkm]);  //lkm - 1 to skip the zero
    
       //to see all the sorted numbers
       for(i = 0; i < lkm; i++)
          printf("\n %d", taulukko[i]);  //<= lkm to see the zero
    Last edited by Adak; 08-24-2009 at 01:49 PM.

Popular pages Recent additions subscribe to a feed