how to eliminate multipule if's

Started by
4 comments, last by Galaxy_X 21 years, 4 months ago
Hello, I got a C program that have a int test, if test is on I have to call function writeSum if a sum is computed, else if test is off I do nothing. The problem is that writeSum is spread over 14 places and I have to use 14 if - else statements to see if test is on. Is there a more efficient way in C to deal with this?? [edited by - Galaxy_X on December 6, 2002 6:34:34 PM]
Advertisement
post your code here. it''s a matter of programming skill and totally depends on the code.

My compiler generates one error message: "does not compile."
pass the conditional variable to the function as well, and modify it to only take action if that is true. Might, or might not, work in your case. We Need Code!

This could be a good place for a macro.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
ok, here's part of the code, the entire thing is too big and I did use a simple example at first, because I thought it was a easy solution,but now for the real thing:


      int main(int argc, char *argv[]){   int dataArray[MAX_SIZE]; //array of data's      int startTime,endTime,tempData;     startTime = time(NULL);   if(argc > 1)   {       if(strcmp(argv[1],"write") == 0)       {          test = 1;       }   }       //here I get the mean     getData(dataArray); // fill up the array with data    if(test == 1)    {         for(int i = 0; i < MAXSIZE -1;i++)         {            if(dataArray[i] > MEAN)            {               writeData(dataArray[i]);            }                   }    }    do {        calibrate(dataArray);// make the data smooth        if(test == 1)        {           for(int i = 0; i < MAXSIZE - 1;i++)           {             if(dataArray[i] > MAX_THRESHOLD)             {               writeData(dataArray[i]);             }        }       }    //while lower than 50 sec do this    while( time(NULL) - startTime < 100);  }}      



I am doing this without the actual source, so there will be mistakes. But this is the general idea, thanks..and there's lots more checks like this.

[edited by - Galaxy_X on December 6, 2002 7:03:48 PM]

[edited by - Galaxy_X on December 6, 2002 7:05:24 PM]

[edited by - Galaxy_X on December 6, 2002 7:28:20 PM]
Make a function (or macro) for:
for(int i = 0; i < MAXSIZE - 1; i++)    if(dataArray > MAX_THRESHOLD)        writeData(dataArray);</pre>  </i>   <br><br><HR color=7F7FC7 size=1><A href="http://www.profundis.se/files/gd-count-entry.html"><IMG src="http://www.profundis.se/files/gd-count.php?" border=0></A>    

This topic is closed to new replies.

Advertisement