• 0

[C] YOU CLICK HERE NOW!


Question

Would someone be so kind as to tell me the codes used in the addition of decimal numbers? Like, the variables and such.. o_O.. please? I've asked a few people this question before, and they give me this kind of coding:

float a;

float b;

a = 5.5

b = 6.3

printf("%f\n", (a + b));

But I need the program to randomly choose decimal numbers and add them together (so I can't define the decimals beforehand). The numbers have to be between 1 and 100. Any ideas? Thanks. :ninja:

oO;;

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

u would have to do something like this

int a, b;

a = srand (43) % 101;
b = srand (23) % 101;

printf("sum of values added is %d\n", (a+b));

hope it helps

Link to comment
Share on other sites

  • 0

mackol: Your use of srand() is very suspect... :)

The following should do what you want:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

...

double a, b;
srand( clock() );
a = ((double) rand() / RAND_MAX) * 99.0 + 1;
b = ((double) rand() / RAND_MAX) * 99.0 + 1;

printf("%f + %f = %f\n", a, b, a + b);

Link to comment
Share on other sites

  • 0

i could have done what u did my friend.. i just wanted to keep it simple.. thats all :)

i know i shouldnt have used srand..

next time i will avoid it :)

Link to comment
Share on other sites

  • 0
i know i shouldnt have used srand..

That's not the problem; check my code, it uses srand(). srand() should be used once, before using rand(), to seed the random number generator. Also, according to my C reference, srand() doesn't return a value, so the code "srand (43) % 101" won't work. :cool:

Link to comment
Share on other sites

  • 0

i gave it a different seed so if u think of the random number generator formula, it will produce a different random number

though i agree i should have used the time functions

and about the srand returning a number

u r right

my bad

i feel so embarressed

i thought i knew C pretty well and i faultered on a simple thing like this

:cry:

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.