Jump to content

Dice game error...

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
wiwbiz

wiwbiz

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Could someone point out the mistakes in this code ?
It's just for calculating the number of times different sums appear on throw of two dices.
#include<iostream>
using namespace std;
int main()
{
    int a[]={2,3,4,5,6,7,8,9,10,11,12};
    int b[11]={};
    for(int i=0;i<359;i++)
    {int c=(rand()%6+1)+(rand()%6+1);
    if(c=a[c-2])b[c-2]++;}
    for(int i=0;i<11;i++)
    cout<<b[i]<<endl;
    cin.get();
}


#2
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
1. You don't return anything from main

2. You never seed rand()

3. Learn to indent properly

4. a[] has no purpose

#include <stdio.h>
#include <time.h>

int main(void) {
    int i, b[11] = {}; 
    srand(time(NULL));

    for(i = 0; i < 359; i++) 
        b[((rand()%6+1) + (rand()%6+1))-2]++;
    
    for(i = 0; i < 11; i++) 
        printf("%d: %d\n", i+2, b[i]);
    
    return 0;
}


#3
wiwbiz

wiwbiz

    Learning Programmer

  • Members
  • PipPipPip
  • 67 posts
Thanks that was helpful.