All pastes #763066 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-11-06 03:11 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main (void)
{
FILE *fp;
int i = 0;
int seen[5];
int store[5];
int tot = 0;
int ran = 0;
int tot2 = 0;
int l = 0;

for (i = 0; i < 5; i++) {
                seen[i] = 0;
                store[i] = 0;
}

srand((unsigned) time(NULL));


while (tot < 5) {
                ran = rand() % 5 + 1; //Generate random number between 1 and 5
                if (seen[ran - 1] == 0) // Have we seen this number before?
                {
                                store[l] = ran; // If so store the value of it into array store
                                l++; // Increment l
                                seen[ran - 1] = 1; // Mark that we've seen this number
                                //printf("Stored %d in store[%d]\n", ran, l - 1);
                }


                for (i = 0, tot = 0; i < 5; i++)
                                tot = tot + seen[i]; // Add up all different numbers we've seen - If all have been seen; = 5
}


for (i = 0; i < 5; i++)  
if (store[i] == i % 5 + 1) // Go through and see if index matches stored number
                tot2++;


fp = fopen ("output.txt", "a");
if (fp == NULL)
                exit(EXIT_FAILURE);

fprintf(fp,"%d\n", tot2);
printf("%d\n", tot2);
fclose(fp);
}