mercredi 20 janvier 2016

Unable to shuffle cards randomly

I am doing a project that shuffle 10 cards (12) time randomly but it didn't work with my code.

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

void show(int[],int);

void shuffle(int[],int,int);

int main (void)
{
    int karten[]={1,2,3,4,5,6,7,8,9,10};
    int n = sizeof(karten)/sizeof(int);
    //printf("%d",n);
    int s=12;

    srand(time(NULL));
    printf("Karten vor dem Mischen: \n");
    show(karten,n);
    shuffle(karten,n,s);
    printf("Karten nach dem Mischen:\n");
    show(karten,n);
    return 0;
}
void show(int karten[],int n)
{
    for(int i=0;i<n;i++)
    {
        printf("%d,",karten[i]);
    }
    printf("\n");
}
void shuffle(int karten[],int n,int s)
{
    int i=0;
    int d=0;
    int v[]={(int)malloc(sizeof(karten))};
    for(int z=0;z<=s;z++)
    {
        i=rand()%10;
        d=rand()%10;
        //printf("%d-->%d\n",i,d);
        v[i]=karten[i];
        v[d]=karten[d];
        karten[d]=v[i];
        karten[i]=v[d];

    }
    printf("Es wurden %d Vertauschungen gemacht\n",s);
    free(v);
}

The error is

(((process returned 255))) 

and the program crashed.




Aucun commentaire:

Enregistrer un commentaire