Fun Stuff > CLIKC

A programming thread!

<< < (33/37) > >>

Pilchard123:
TIL that if you import antigravity into a python program, xkcd #353 is opened in the default browser.

Schmee:
So I'm doing an assignment in C, and for one part of it I have to print a specific character depending on the value of a double, temp_spl.
Basically, if temp_spl is between 30 and 35 I print a 3, 35 and 40 I print a space, and so on up to 90.
The solution I have currently is here, but it seems really clumsy. Is there a better way?

--- Code: ---if (temp_spl>90.0){
printf("9");
} else if (temp_spl<30.0) {
printf(" ");
} else if (((int)floor(temp_spl))%10 >= 5) {
printf(" ");
} else {
printf("%d",(int)floor(temp_spl/10.0));
}

--- End code ---

ev4n:
If it works, that seems ok?

I can think of performance improvements, or ways that are more elegant, but I'm not sure why one would want to spend more time on improving a working solution without a good reason.

Sorry, maybe I'm being too cynical.

ev4n:
Wait, Ecplise thinks that to run any kind of debugger, the top level make associated with the current workspace needs to be run?

What kind of moronic idea is THAT!?!?!?!

osaka:

--- Quote from: Schmee001 on 18 Oct 2014, 22:13 ---So I'm doing an assignment in C, and for one part of it I have to print a specific character depending on the value of a double, temp_spl.
Basically, if temp_spl is between 30 and 35 I print a 3, 35 and 40 I print a space, and so on up to 90.
The solution I have currently is here, but it seems really clumsy. Is there a better way?

--- Code: ---if (temp_spl>90.0){
printf("9");
} else if (temp_spl<30.0) {
printf(" ");
} else if (((int)floor(temp_spl))%10 >= 5) {
printf(" ");
} else {
printf("%d",(int)floor(temp_spl/10.0));
}

--- End code ---

--- End quote ---

I'd go with something a bit more nested, but that's just me:


--- Code: ---char* result = "9";
if(temp_spl < 90.0 && temp_spl > 30.0){
  if(temp_spl % 10 < 5){
    result[0] = (int) floor (temp_spl/10) + 48;
  }else{
    result[0] = 32;
  }
}
printf("%s%n", result);

--- End code ---

Some hacking in here with ascii tables and how C handles pointers/arrays, but it should work. You can always define result as char[]

EDIT: I just noticed that that doesn't take into account the possibility of the value being less than 30. Also, I found a way to express it in 1 line with ternary operators, just for funziez.


--- Code: ---(temp_spl > 90.0) ? printf ("9") : ((temp_spl < 30.0) ? printf (" ") : ((temp_spl % 10 < 5) ? printf("%d", (int)floor(temp_spl/10.0)) : printf(" ")));

//"a ? b : c" stands for "if a, do b. Else, do c". a has to be a boolean or an expression that returns a boolean.+
//Example: Max(a, b) as ternary operation: a > b ? a : a < b ? b : a == b ? a : b;
--- End code ---

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version