Thread: loops and "complicate / flexible" conditions

  1. #1
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39

    loops and "complicate / flexible" conditions

    ok i've written the following function :
    Code:
    int small_array(int subset, int i, double d[i])
    {
    	int j,n, result;
    	double temp[subset];
    	result = 0;
    	for ( n = 0; n < subset; n++)
    	{
    		for ( j = 0; j < i; j++)
    		{
    			if (d[result] == 0 || d[result] == temp[0] || d[result] == temp[1]
    				|| d[result] == temp[2] || d[result] == temp[4] 
    				|| d[result] == temp[5] || d[result] == temp[6]
    				|| d[result] == temp[7] || d[result] == temp[8]){result = result+1;}
    			else if (d[result] < d[j]){result = result;}
    			else if (d[result] > d[j]){result = j;}
    			else if (d[result] == d[j]){result = result;}
    		}
    		temp[n] = d[result];
    		printf("%d\n", result);
    	}
    	printf("\n");
    	return result;
    }
    which it takes an array and prints (for the time) the positions in the array that contain the "x"(subset) smallest values (except 0) (the first tests look like it's working) i've pre-set the subset int to 9, but as it is obvious i want to work with different subset values..is there a way to do such a condition...i've tried to put a "for" for the first if...but then the problem is that because if is inside a different loop ..it sees "else" as an error and doesn't compile.

    I use a C99 compilere as i found out recently...If there is a way around I would like to know...thanx in advance

  2. #2
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    If you want to know why it is compiling as C99 it is because your array has a variable elements. You can get around this with malloc. Write out exactly on paper what you are trying to do and mabey it will be more clear to you for your algorith problem.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  3. #3
    Registered User white's Avatar
    Join Date
    Nov 2004
    Posts
    39
    i know why it is compiling and it is working correctly (as it seems)...my problem is this line

    Code:
    if (d[result] == 0 || d[result] == temp[0] || d[result] == temp[1]
                                    || d[result] == temp[2] || d[result] == temp[4] 
                                    || d[result] == temp[5] || d[result] == temp[6]
                                    || d[result] == temp[7] || d[result] == temp[8]){result = result+1;}
    it is only woring with the pre-set subset int (9). I am just asking if it is possible to replace this condition with a loop. like that (it is not code just the thought)
    Code:
    if (d[result] == 0 || d[result] == temp[n] (n = from 0 to whatever))

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Yes, you'll need to use something like the following pseudo-code.
    Code:
    	int found = 0;
    
    	for (k = 0 To Whatever)
    	{
    		if d[result] equals temp[k]
    		{
    			result++;
    			found = 1;
    			break;
    		}
    	}
    
    	if found equals 0 /* Only run this section if value not found in loop. */
    	{
    		     if (d[result] < d[j]){result = result;}
    		else if (d[result] > d[j]){result = j;}
    		else if (d[result] == d[j]){result = result;}
    	}

Popular pages Recent additions subscribe to a feed