I have been writing this program for about two weeks, just to see if i still know the C language
what i do is have the user enter how many numbers they want generated (numpicks) then what the biggest number they want generated is (numMax)
using a for loop i feed the numbers into an array and then check to see if there are any duplicates...
The issue occurs somewhere when i'm trying to generate the numbers and assign them into an array so i'll post two code bits
that's the whole program the bit dealing specifically with the array is here
when it prints out the numbers they're gibberish. it's print out the right amount of numbers, but not within the user defined scope!
help?
what i do is have the user enter how many numbers they want generated (numpicks) then what the biggest number they want generated is (numMax)
using a for loop i feed the numbers into an array and then check to see if there are any duplicates...
The issue occurs somewhere when i'm trying to generate the numbers and assign them into an array so i'll post two code bits
#include <stdio.h> #include <stdlib.h> #include <time.h> #include <stdbool.h> int NumMax;//global variable to store maximum number generated int NumPicks;//global variable to store number of numbers to generate bool isDuplicate; //check to see if number is duplicate int i, f; //counters for Loops int valid_picks(); //function to validate picks int is_Duplicate();//funstion to make sure all numbers are unique int main() { srand(time(NULL)); valid_picks(); is_Duplicate(); } int is_Duplicate() { int Numbers[NumPicks]; int i,f; f =0; i=0; for (f=0; f<NumPicks; f++); { isDuplicate = 0; // Assume that there are no duplicates Numbers[f] = rand() % NumMax; // Pick a number between 0 and NumMax do { for (i=0; i<f; i++); { //test all previous numbers if (f =! 0 ); //doesn't run on first loop because there is nothing to test against { if (Numbers[i] == Numbers[f]) { // There is a duplicate! isDuplicate = 1; // Set the flag to 1 } // Go back to the i for, check the next previous number } } } while (isDuplicate ==1); } printf("\n You're picks are as follows: \n"); for (f = 0;f < NumPicks; f++) { //prints out the results printf("%d \n", Numbers[f]); } printf("Good Luck! \n"); } int valid_picks () { int valid = 0; /*to validate chosen numbers */ while( valid == 0 ) { printf("Please enter the max number between 2 and 100: "); scanf("%d", &NumMax); if( NumMax < 2 ) { /*Number is Too Low */ printf("Number is below 2. Please re-enter\n"); valid = 0; } else if( NumMax > 100 ) { /*Number is Too High */ printf("Number is above 100. Please re-enter\n"); valid = 0; } else /*Number is acceptable */ valid = 1; /*rester */ } valid = 0; while( valid == 0 ) { printf("Please enter the number of picks between 1 and 10: "); scanf("%d", &NumPicks); if( NumPicks < 1 ) { /*Number is too low */ printf("Number is below 1. Please re-enter\n"); valid = 0; } else if( NumPicks > 10 ) { /*Number is too high */ printf("Number is above 6. Please re-enter\n"); valid = 0; } else /*Number is acceptable */ valid = 1; } }
that's the whole program the bit dealing specifically with the array is here
int is_Duplicate() { int Numbers[NumPicks]; int i,f; f =0; i=0; for (f=0; f<NumPicks; f++); { isDuplicate = 0; // Assume that there are no duplicates Numbers[f] = rand() % NumMax; // Pick a number between 0 and NumMax do { for (i=0; i<f; i++); { //test all previous numbers if (f =! 0 ); //doesn't run on first loop because there is nothing to test against { if (Numbers[i] == Numbers[f]) { // There is a duplicate! isDuplicate = 1; // Set the flag to 1 } // Go back to the i for, check the next previous number } } } while (isDuplicate ==1); } printf("\n You're picks are as follows: \n"); for (f = 0;f < NumPicks; f++) { //prints out the results printf("%d \n", Numbers[f]); } printf("Good Luck! \n"); }
when it prints out the numbers they're gibberish. it's print out the right amount of numbers, but not within the user defined scope!
help?