473,767 Members | 2,226 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

64 New Member
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 2503
RedSon
5,000 Recognized Expert Expert
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 New Member
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 New Member
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
8288
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 B
18
2645
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: //============================== #include <stdlib.h> //allow rand() function int i; i = rand (); // i = any number of the set or if (0<=i<(4294967295/4))
23
2297
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 as fast as one block can fall, and the next to be determined. Generating different numbers in one spur can work, but people can play Tetris for hours (or even days), and so you can't predict how long. You could constantly be making more with the...
14
9904
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 to 1 . I am not sure how to do this. Any idea? Thanks.
4
10589
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 guessed right, it asks the user is he/she wants to play again. If the answer is yes, it generates a random number and asks the user to guess the number again. The user can exit if he enters 0. I have created the following code so far but it does not work....
5
5284
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 program should start by asking the user how many multiplication problems they would like to try, and read this value from the keyboard. Then, the program should present to the user that many multiplication problems and allow the user to enter an...
20
7855
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 numbers, multiply them, and ask you for the result. It also needs to have four responses for both right and wrong answers and should print them randomly as well. The program should use at least 2 functions. Any help would be greatly appreciated. ...
9
6608
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() {
0
9571
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10009
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.