473,508 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sum function of a for loop for a dice generator

2 New Member
I have written the code below that allows the user to input any sided dice and any number of rolls. The program will print out the output in sequence. I need to sum these outputs into one number, but I am having trouble writing a sum function for this. Any ideas?
Expand|Select|Wrap|Line Numbers
  1. //This Program Creates the Sum of a Number of Dice Rolls of the Same Size
  2.  
  3. #include <stdio.h> 
  4. #include <math.h>
  5. #include <time.h> 
  6. int rolldie(); 
  7.  
  8. int main(){ 
  9. int x, n, a; //'x' n=number of rolls, a=number sided dice
  10. printf ("Please Enter the Number Sided Dice Followed by the number of Rolls... %d"); 
  11. scanf ("%d%d", &a, &n);    
  12. srand(time(0));
  13. for(x=0;x<n;x++) 
  14. printf("%d ",rolldie(a,n)); 
  15. return 0; 
  16. }
  17. int rolldie(int c)
  18. {
  19. return 1 + rand() %c;
  20. }
  21.  
The output looks something like this currently:

Please Enter the Number Sided Dice Followed by the number of Rolls... 12 10
4 12 2 10 12 5 5 12 12 5 >Exit code: 0
Oct 15 '14 #1
2 1300
weaknessforcats
9,208 Recognized Expert Moderator Expert
Define a total variable and set it to zero.
Inside your loop, change the code so that you call the rolldie function and capture the return value in a variable.
Then add the variable to the total.
Then you can display the variable.
Now at the end of the program, the total will have the sum of all the calls to rolldie.
Oct 15 '14 #2
donbock
2,426 Recognized Expert Top Contributor
Line 10: printf format string contains "%d", but there is no corresponding subsequent argument.

Line 6: prototype for rolldie should specify the arguments.

Lines 14 and 17. You call rolldie with two arguments, but it is only defined to take one. A proper function prototype would have flagged this error.
Oct 15 '14 #3

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

Similar topics

1
1895
by: TheDustbustr | last post by:
<code> from __future__ import generators from time import time threads= def sleep(n): print "In Sleep" t=time() while (t+n>time()): yield None
9
2669
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as...
4
2306
by: Wai Yip Tung | last post by:
I'm attempting to turn some process than uses callback to return result into a more user friendly generator. I'm hitting some road block so any pointer would be appriciated. Let say there is an...
10
1953
by: Simon Wittber | last post by:
I use a coroutine/generator framework for simulating concurrent processes. To do this, I write all my functions using the general form: while True: do stuff yield None To make these...
25
3666
by: skull | last post by:
Hi everybody, it is my first post in this newsgroup. I am a newbie for python though I have several years development experience in c++. recently, I was stumped when I tried to del item of a list...
5
2236
by: Jerzy Karczmarczuk | last post by:
I thought that the following sequence gl=0 def gen(x): global gl gl=x yield x s=gen(1)
7
3586
by: Kirk McDonald | last post by:
Let's say I have a function that takes a callback function as a parameter, and uses it to describe an iteration: def func(callback): for i in : callback(i) For the sake of argument, assume...
7
1675
by: George Young | last post by:
I am puzzled that creating large dicts with an explicit iterable of key,value pairs seems to be slow. I thought to save time by doing: palettes = dict((w,set(w)) for w in words) instead of: ...
1
1839
by: cokofreedom | last post by:
if __name__ == '__main__': print "Globals (For Loop):" try: for i in globals(): print "\t%s" % i except RuntimeError: print "Only some globals() printed\n" else: print "All globals()...
13
2241
by: Andrew | last post by:
Hi I was wondering if there is anyway with XML RPC to send a string of text from the server to the client with out calling return thus breaking my loop for example def somefunc(): for...
0
7231
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,...
1
7063
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...
0
7504
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...
1
5059
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...
0
4720
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...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1568
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 ...
1
773
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.