473,378 Members | 1,439 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Generate a random number then use that to divide the user input number

64
Like the titles says I'm suppose to generate a random number then divide that by a number inputed by a user. The random number can range from 2-8. I tried to do the code, but I get some weird result here is the result

Expand|Select|Wrap|Line Numbers
  1. Please enter a number:  18
  2. a. number                         18.00
  3. b. the random number              6
  4.    00-1.#J << suppose to be 3 , but I get this number. (Trying to get 18 divided by 6.
  5.  
This is the code I have so far.

Expand|Select|Wrap|Line Numbers
  1. int main (void)
  2.  
  3. {
  4. //    Local Declarations 
  5.     double number;
  6.     float result;
  7.     float random = 2 + rand()%8;
  8.  
  9. //    Statements 
  10.     printf("Please enter a number:");
  11.     scanf("%lf", &number);
  12.     srand(time(NULL));
  13.  
  14.  
  15.  
  16.     printf("a. number\t\t\t %5.2lf\n", number);
  17.     printf("b. the random number\t\t  %d\n", rand() % (int)random + 2);
  18.     result = number / random;
  19.     printf(" the result is %5.2lf", result);
  20.  
  21.  
Thanks in advance!
Jan 31 '07 #1
3 2460
RedSon
5,000 Expert 4TB
I think it is a problem with your printf statement. I'm not sure what though since you have edited your output to be different then what I expect the code to output. Also, why did you decide to make some of your variables double precision and others float?
Jan 31 '07 #2
td0g03
64
I think it is a problem with your printf statement. I'm not sure what though since you have edited your output to be different then what I expect the code to output. Also, why did you decide to make some of your variables double precision and others float?

Well, to tell you the truth I wasn't sure why it was working so I just played around with the lf, d, c, and even some other things. Here is the exact question the book asks me to do and maybe you can give me a small code so I can get idea how it works.

1. prompt the user to enter a number (type double).
2. read number (using %lf).
3. Generate a random number and scale it to range 2...8.
4. Set result to number divided by the random number.

Ex.One output could be
number: 127.50 <<-- user input
the random number 2
result 63.75 <<-- 127.50 / 2 = 63.75
Jan 31 '07 #3
td0g03
64
Thanks again guys! But I was able to figure out what I did wrong.
Feb 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Laphan | last post by:
Hi All This is a strange request, but I just cannot fathom how to do it. In theory the requirement is very basic, but in practise its a noodle!! I have 10 team names like so: Team A Team...
18
by: Toby Newman | last post by:
I need to randomly choose one of four paths in my program. Using the tools I know, the best way I can think to do it is by doing something like the following: //==============================...
23
by: Alvin | last post by:
Well, I'm developing a Tetris game in SDL, but when it comes to deciding the next block, I'm stuck. It's random, but when I try something like seeding the randomizer with the time, it won't update...
14
by: Anthony Liu | last post by:
I am at my wit's end. I want to generate a certain number of random numbers. This is easy, I can repeatedly do uniform(0, 1) for example. But, I want the random numbers just generated sum up...
4
by: fatimahtaher | last post by:
Hi, I am supposed to create a program that generates a random number and then asks the user to guess the number (1-100). The program tells the user if he guessed too high or too low. If he...
5
by: scan87 | last post by:
Can somebody please help me with the following problem. I need to submit the problem on Monday. A program is required which could be used to help a child practice their multiplication tables. The...
20
by: jjmillertime | last post by:
I'm new so i apologize if this is in the wrong spot. I'm also new to programming in C and i've been searching for quite a while on how to create a program using C that will generate two random...
9
by: Chelong | last post by:
Hi All I am using the srand function generate random numbers.Here is the problem. for example: #include<iostream> #include <time.h> int main() {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.