-
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!!!
-
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
-
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!!!!!!!! ;)
-
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