Know C programming? Could use a little help
Posted: Thu Sep 27, 2007 2:15 pm
I'm supposed to make a float out of three integers, the first one being the integer part and the two others forming the decimal part, then display it rounded up to tenths and rounded down. It seemed like a very simple task but the problem is it's supposed to show only two decimals in the first case and only one in the two following ones - without formatting. Ie. using %.2f or %.1f is forbidden, it needs to be cropped in some other way. Any suggestions?
Code: Select all
#include <stdio>
#include <math>//floor, ceil
#include <stdlib>//rand, srand
#include <time>
int main()
{
int i, luku[3];
float tulos;
char anykey[80];
srand (time(NULL));
for (i = 0; i < 3; i++)
luku[i] = rand() % 10;//luku 0 - 9
tulos = luku[0] + (float) luku[1]/10 + (float) luku[2]/100;
printf("Random number is %f\n", tulos);
printf("Rounded up it's %f", (float) ceil(tulos*10) / 10);
printf(" and rounded down it's %f\n", (float) floor(tulos*10) / 10);
gets(anykey);
return 0;
}