Hey guys.
I have this function:
PHP Code:
// NOTE: "P" IS A MACRO FOR printf()...(#define P printf)
void find_four(struct atom *atomo, int size)
{
int w=0;
int k=0;
int t=0;
int u=0;
int proceed = 1;
do{
P("Please Type The Element Number:"); fgets(choice,50,stdin);
clrscr();
c = atoi(choice);
clrscr();
for(int j = 0; j<sz; j+=1)
{
if( (atomo[j].numb == c) )
{
k=j; // Assign loop variable to the selected Atom
P(" Most Probable Mates For This Element Forming A Four-Atom Molecule:\n");
P("__________________________________________\n");
if(atomo[k].takes == 0)
{ clrscr();
P("This is a Noble Gas. \n\nThis means it does not bond with other \nElements except under very special circumstances.\n\nIt has no known mates.\n\n\n");
break;
}
for(w=0;w<sz;w+=1)
{
for(t=0;t<sz;t+=1)
{
for(u=0;u<sz;u+=1)
{
if(atomo[t].gives != 0 && atomo[u].gives != 0 && atomo[w].gives != 0) // Noble gasses filtered out...
{
if(atomo[t].gives + atomo[u].gives + atomo[w].gives == atomo[k].takes) /// if the valence electrons fill this attoms shell...
{
P("FOUND for: #%i %-12s %4s \n"
" #%i %-12s %4s \n"
"AND #%i %-12s %4s \n"
"AND #%i %-12s %4s \n\n"
"To Form A \"%s-%s-%s-%s\" Molecule...\n"
"__ __________________________________\n\n"
,atomo[k].numb,atomo[k].elem,atomo[k].symb
,atomo[t].numb,atomo[t].elem,atomo[t].symb
,atomo[u].numb,atomo[u].elem,atomo[u].symb
,atomo[w].numb,atomo[w].elem,atomo[w].symb
,atomo[k].symb,atomo[t].symb,atomo[u].symb,atomo[w].symb);
fgets(choice,50,stdin); //If tired of this element,press x...
if( strstr(choice,"x") || strstr(choice,"x") )
{find_four(atomo,sz);} //...and return to beginning
}
}
}
}
}
}
}
P("\nCONTINUE? ('X' Exits)\n\n\n");
fgets(choice,50,stdin);
clrscr();
if(strstr(choice,"x") || strstr(choice,"X") ){proceed = 0;}
else{proceed=1;}
}while(proceed != 0);
} // <---End of function
The basic problem here is that I am getting every possible combination for the four given elements that are likely to combine.
Example:
H-Na-K-P
Na-P-H-K
P-H-K-Na
H-P-Na-K
...etc,etc,etc!!!
...when only ONE of those is necessary.
I am currently tweeking the loops so that each one only runs through a certain part of the array of structs a certain number of times. But I don't want to lose information either. I just want a mechanism to control it so that only one combination gets printed.
I am not looking for code, here, just theory, but if you need to use code to explain something, that's fine, too.