Know C programming? Could use a little help

General chitchat, advertisements for other services, and other non-Cantr-related topics

Moderators: Public Relations Department, Players Department

User avatar
SekoETC
Posts: 15525
Joined: Wed May 05, 2004 11:07 am
Location: Finland
Contact:

Know C programming? Could use a little help

Postby SekoETC » 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;
}
Not-so-sad panda

Return to “Non-Cantr-Related Discussion”

Who is online

Users browsing this forum: No registered users and 0 guests