Thread: function creator

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    21

    function creator

    hi guys, i need some help from u...
    I need to write a function, that recieves char array and the function returns the minimmum character, then, after i ask again for an other result, it ignores the previous one.

    this is what i thought will be the best way to write it, but i dont know why, it always returns me the same value...

    ------------------------------------------------------
    struct pass {
    char car[tamcla];
    char ava[tamcla];
    };





    int valmin( pass clave, int recla){ //recla, is the previous value found. (m)
    int count, m;

    for (count=0; count<recla; count++){
    if ((clave.car[count]< clave.car[(count+1)]) && (clave.ava[count]!='n')){
    clave.ava[count]='n';
    m=count;
    }
    }
    return (m);
    }


    thanx for your help!!!

  2. #2
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    There are some unclear areas in your code that have me guessing as to what exactly you are looking for. For now, I will demonstrate how to send in a char array with a function call and return the minimum char value (minimum decimal equivalent). If this does not suit your needs, please clarify what needs to be done different.
    Code:
    #include <iostream.h>
    
    char valmin(char myArray[], int arySize);
    
    int main()
    {
        char myArray[20] = "Some text here";
        char minChar;
    
        minChar = valmin(myArray, 20);
        cout << minChar << endl;
    
        return 0;
    }
    
    char valmin(char myArray[], int arySize)
    {
        char tempChar = myArray[0];
        for (int i = 0; i < arySize; i++)
            tempChar = (myArray[i] < tempChar) ? myArray[i] : tempChar;
    
        return tempChar;
    }
    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    21
    yeah man, thanx a lot, thats kind of what i need.... but, what if i ask for the next minimum value??? i need to have like an order, from least to greatest... and the complicated thing is that those values found, can't be altered. and so, thats why i used a structure:
    I got the text at clave.car[counter] and i say if i already chose that position as a minimum or not at clave.avalible[counter]

    thanx for ur help.... if u can make that change, i would be very greatfull!

    THANX!!!!!!!!

  4. #4
    Registered User Strider's Avatar
    Join Date
    Aug 2001
    Posts
    149
    Ok, I see what you are saying. Here is some lengthy code that helps to break down the process using a type of bubble sort to walk through the array. You could probably clean it up but this will work the way it is.
    Code:
    #include <iostream.h>
    #include <string.h>
    
    #define SIZE 20
    struct ArrayGroup
    {
        char originalArray[SIZE];
        char orderedArray[SIZE];
    };
    
    void valmin(ArrayGroup &myGroup);
    
    int main()
    {
        ArrayGroup arrayGroup;
    	
        for (int i = 0; i < SIZE; i++)
        {
            arrayGroup.originalArray[i] = '\0';
            arrayGroup.orderedArray[i] = '\0';
        }
    
        cout << "Enter a string of up to " << SIZE << " characters: ";
        cin.getline(arrayGroup.originalArray, SIZE);
    
        valmin(arrayGroup);
        
        cout << "Original: " << arrayGroup.originalArray << endl
            << "Ordered: " << arrayGroup.orderedArray << endl;
    
        return 0;
    } // end main
    
    void valmin(ArrayGroup &myGroup)
    {
        char tempArray[SIZE] = {'\0'};
        char tempChar;
        int count = 0;
    
        for (int i = 0; i < SIZE; i++)
        {
            // ignore all spaces and null characters
            if ((myGroup.originalArray[i] != '\0') &&
                (myGroup.originalArray[i] != ' '))
                tempArray[count++] = myGroup.originalArray[i];
        } // end for loop
    
        for (i = 0; i < SIZE; i++)
        {
            for (int j = 0; j < SIZE; j++)
            {
                // make sure we stay in the array bounds
                if ((j + 1) < SIZE)
                {
                    // set the temporary to the next in the array
                    tempChar = tempArray[j + 1];
    
                    // compare only valid values
                    if ((tempArray[j] != '\0') &&
                        (tempArray[j + 1] != '\0') &&
                        (tempChar < tempArray[j]))
                    {
                        // swap characters
                        tempArray[j + 1] = tempArray[j];
                        tempArray[j] = tempChar;
                    } // end if
                } // end if
            } // end for loop
        } // end for loop
        strcpy(myGroup.orderedArray, tempArray);
    } // end valmin
    Like I said, a bit lengthy, but hopefully this will give you a better idea of the process involved.

    David
    One Ring to rule them all, One Ring to find them,
    One Ring to bring them all and in the darkness bind them
    In the Land of Mordor where the Shadows lie.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM