Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Galaxy_X

how to eliminate multipule if's

This topic is 5800 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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."

Share this post


Link to post
Share on other sites
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!


Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Make a function (or macro) for:

for(int i = 0; i < MAXSIZE - 1; i++)
if(dataArray > MAX_THRESHOLD)
writeData(dataArray[i]);



Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!