Thread: please solve this problem

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    10

    Question please solve this problem

    Write a program which does the following:
    1. Roll a dice 100 times and store the results in an array.
    2. Roll a dice 100 times again and store the results in a different array.
    3. The sums of the faces in each time are stored in another array.
    4. Show the frequencies of the sum array on the screen.
    5. Use functions to implement the program.

  2. #2

  3. #3
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Code:
    #include<stdlib.h>
    #include<stdio.h>
    
    int main( void ) {
       for(;;) 
          printf( "Do your own homework.\n" );
       return 0;
    }
    Code:
    while(!asleep) {
       sheep++;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Abdullahmert!

    We will *help* you when you show your work, and tell us what you're stuck on, in your C program. We don't start programs for people, however - that part is up to you.

    You know your assignment, and that is not a C problem. You must handle that part of it.

    p.s. Dice is plural. Die is the singlular: roll a die - one. Roll the dice: two or more die.

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    This might help get you going in the right direction..

    Code:
    #include <stdlib.h>
    
    /*.....*/
    
    
    int *roll_dice() {
    	int *ret = malloc(100);
    	int count;
    	for (count = 0; count < 100; count ++) {
    		ret[count] = arc4random() % 7; 
    	}
    	return ret; 
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by memcpy View Post
    This might help get you going in the right direction..

    Code:
    #include <stdlib.h>
    
    /*.....*/
    
    
    int *roll_dice() {
    	int *ret = malloc(100);
    	int count;
    	for (count = 0; count < 100; count ++) {
    		ret[count] = arc4random() % 7; 
    	}
    	return ret; 
    }
    First... stop posting pre-coloured text into code tags. The forum will colour it for you and it will display line numbers when it does.

    Second... how do you figure doing his homework for him is helping him? Now he's gonna go scoop and poop on that code and use your pre-fab sample without learning the first thing about programming. Fully half of a programmer's skillset is analysing a problem in such a way he can produce code to solve it... You just took that away from him.

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Quote Originally Posted by CommonTater View Post
    First... stop posting pre-coloured text into code tags. The forum will colour it for you and it will display line numbers when it does.
    It was literally my first post here, and I wasn't aware that it kept colors from programs such as Xcode.

    Quote Originally Posted by CommonTater View Post
    Second... how do you figure doing his homework for him is helping him? Now he's gonna go scoop and poop on that code and use your pre-fab sample without learning the first thing about programming. Fully half of a programmer's skillset is analysing a problem in such a way he can produce code to solve it... You just took that away from him.
    I thought by only doing one of the five things he specified, it was enough to not be spoon-feeding and just to be a hint in the right direction.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by memcpy View Post


    It was literally my first post here, and I wasn't aware that it kept colors from programs such as Xcode.



    I thought by only doing one of the five things he specified, it was enough to not be spoon-feeding and just to be a hint in the right direction.
    I don't think it's xcode that's dumping html into the forum messages... click on "reply with quote" on one of your own messages, there's a lot of html tagging going on...

    As for my quibble about handing out too much code... I sometimes do post code snippets here (as I'm sure you've noticed), we all do from time to time... but not without first enjoining the question asker to figure it out on his own. Once it gets obvious he's not getting it a bit of code might help. But don't confuse showing off with helping others...

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Agreed. The original poster of this thread could not possibly have showed less effort.
    There's not only no indication of any effort, there's every indication of a distinct lack of even wanting to put in the slightest bit of effort.
    If ever a poster should be given the brush off with no help whatsover, this would be it.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Even worse - that was the first post from a newly joined member too. He's probably spammed that to dozens of forums in the hope of getting a reply rather than even bothering to try. What's laughable is that I was writing programs like that when I was 7. Makes you wonder what he'll do when he gets a real assignment that takes time to do (rather than a 5-minute exercise) and people are loath to do it for him.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ledow View Post
    Even worse - that was the first post from a newly joined member too. He's probably spammed that to dozens of forums in the hope of getting a reply rather than even bothering to try. What's laughable is that I was writing programs like that when I was 7. Makes you wonder what he'll do when he gets a real assignment that takes time to do (rather than a 5-minute exercise) and people are loath to do it for him.
    It's not at all uncommon.

    He looked at the assignment for 10 seconds, sat down at his computer for 20 seconds then decided he didn't know how to do it and started begging others to do it for him. Happens all the time.

    As I keep pointing out, fully half of programming is figuring out what needs to be done. The assignment spells that out pretty clearly... 3 arrays, 2 for random numbers, one with the parallel sums of the other two and a quicky display of how often stuff happened...

    Thing is, it's about problem solving and he's never going to learn if people keep handing him code. As you point out, someone like that is useless in a coding pool and will be come a burden on his co-workers.

  12. #12
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by CommonTater View Post
    I don't think it's xcode that's dumping html into the forum messages... click on "reply with quote" on one of your own messages, there's a lot of html tagging going on...
    I had the same problem with Eclipse... a few versions ago.
    The problem was that it fed 'rich' text to the Editor box here...and the script 'tagged' it.

  13. #13
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    I solved it. It was pretty easy.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yeah, I solved it too. Half-assed Python implementation for kicks
    Code:
    #!/usr/bin/env python
    
    import random
    
    def getList(die_faces, num_vals):
       return [random.randint(1, die_faces) for i in range(num_vals)]
    
    def sumLists(a, b):
       return [a[i] + b[i] for i in range(len(a))]
    
    def getFrequencies(die_faces, s):
       d = dict([(i, 0) for i in range(2, (die_faces * 2) + 1)])
       for v in s:
          d[v] += 1
       return d
    
    def printFrequencies(d):
       for i, c in d.iteritems():
          print '%d : %d' % (i, c)
    
    DIE_TYPE=20
    NUM_VALS=100
    random.seed()
    printFrequencies(getFrequencies(DIE_TYPE, sumLists(getList(DIE_TYPE, NUM_VALS), getList(DIE_TYPE, NUM_VALS))))

  15. #15
    Registered User
    Join Date
    Dec 2011
    Location
    Seattle area
    Posts
    4
    Quote Originally Posted by memcpy View Post
    This might help get you going in the right direction..

    Code:
    #include <stdlib.h>
    
    /*.....*/
    
    
    int *roll_dice() {
        int *ret = malloc(100);
        int count;
        for (count = 0; count < 100; count ++) {
            ret[count] = arc4random() % 7; 
        }
        return ret; 
    }
    No! I felt I had to sign in to respond to this, by the hacker code and common decency. Just because someone asks a question that doesn't deserve an answer and is against forum policy, doesn't mean you should give them code that will crash their computer, and malfunction even if it doesn't crash, and teach them bad practices too.

    These are some issues I've found in that code so far, which should be studied by anyone who thought it looked like a helpful example:

    1) The size of an int is never 1 byte, so malloc(100) for an array of 100 integers is an underallocation and will result in other memory being overwritten when the array is written. Overwrites can result in system crashes and other unpredictable behavior, more likely when the amount of the overwrite is larger, but it's a risk in any amount.

    2) Using modular division by 7 ("% 7" in the code given) will result in values from 0 to 6. Since 7-sided dice with faces numbered from 0 to 6 are very rare, this will probably produce a malfunction from the point of view of a programmer who expects dice are 6-sided and tries to use this function.

    3) Any part that would deallocate using free() is missing. Without that you've got a function that would take more memory each time called, and not give it back, in common jargon, "a memory leak." It wasn't even necessary, for the small amount of memory needed, to use malloc instead of declaring a static, constant size array.

    "roll_dice" indeed. It's like taking your life in your hands with a code example like this. If intentional, I salute you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++problem to solve
    By truetrini20 in forum C++ Programming
    Replies: 9
    Last Post: 06-29-2010, 03:19 AM
  2. Please help me to solve this IO problem.
    By nichya88 in forum C++ Programming
    Replies: 2
    Last Post: 01-19-2010, 12:15 PM
  3. Help me Solve this problem
    By letsgoredwings in forum C++ Programming
    Replies: 10
    Last Post: 06-12-2008, 12:38 PM
  4. how do i solve this problem?
    By manav in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 04-15-2008, 09:58 AM
  5. problem cant solve please help.
    By sarah in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:32 PM