mercredi 5 juillet 2017

finding repeated numbers in a randomly generated array. ( C )

I'm trying to make a very simple program where the user inputs how many choices there are, and the program makes an array of size (choices +1) so that there is at least 1 number repeated. I also print a sorted list from smallest to largest of the generated choices just for the sake of looking nice. Where I'm stuck is I want to have the program look through the sorted list and find the choice that is repeated the most and print that choice out. The idea behind this program was to make a fun little random generator help make a decision (like where to eat, go out, etc.) when the choices are tough. I'm still learning basic C so forgive me if the solution is basic. Any help is appreciated.

int main()
{
int i;
int j;
int howmany;
int swapped;
int temp;

printf("How many choices are there?\n");
scanf(" %d", &howmany);

int choices[howmany];
srand(time(NULL));

printf("\nRandomly Generated Choices\n");
for (i=0; i<howmany+1; i++){
    choices[i] = (rand()%howmany)+1;
    printf(" %d ", choices[i]);
}
printf("\n");
while(1){
    swapped =0;

    for( i=0; i<howmany; i++){
        if (choices[i]>choices[i+1]){
            temp = choices[i];
            choices[i] = choices[i+1];
            choices[i+1]= temp;
            swapped = 1;
        }
    }
    if (swapped==0){
        break;
    }
}
printf("\nRandomly Generated Choices - Sorted\n");
for (i=0; i<howmany+1; i++){
    printf(" %d ", choices[i]);
}

for (i=0; i<howmany; i++){
    if (choices[i]== choices[i+1]){

    }
}
printf("\n\nMost Frequent Choice\n");


return 0;
}




Aucun commentaire:

Enregistrer un commentaire