C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 4th, 2003, 11:18 AM
Fullest_Empty Fullest_Empty is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 1 Fullest_Empty User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to Fullest_Empty
Exclamation You click here now!

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.
oO;;

Reply With Quote
  #2  
Old January 4th, 2003, 04:18 PM
nate nate is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2003
Posts: 3 nate User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to nate
try using the rand() function to generate your decimal values and then using another rand() function to generate the Integer portion of the value. (All you have to do to keep it as a decimal is divide the result by 100 and store it into your "float" variable). Then all you need to do is add the decimal portion to the integer portion and you have a fully randomly generated decimal number.


If you want TRULY random numbers then you have to use srand(seed)...where seed is some integer value that you have to supply which will generate a different random number.

Play around with these two functions.

Reply With Quote
  #3  
Old January 4th, 2003, 05:13 PM
vpopper's Avatar
vpopper vpopper is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Location: Southern California
Posts: 73 vpopper User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 56 m 9 sec
Reputation Power: 13
How about this:

Code:

#include <stdio.h> 
#include <stdlib.h> 
#include <time.h> 
 
int myrand(int k) 
{ 
  double x = RAND_MAX + 1.0; 
  int y = 1 + rand()*(k/x); 
  return(y); 
} 
 
int main(int argc, char** argv) 
{ 
  int a; 
 
  srand( (unsigned)time(NULL) ); 
  a = myrand(100) + myrand(100); 
  printf("a = %d\n", a); 
 
  return EXIT_SUCCESS; 
} 

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > You click here now!

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap