446,418 Members | 1,084 Online
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,418 IT Pros & Developers. It's quick & easy.

# C problem, likely simple but I can't see it!

10 Replies

 P: n/a sam writes: hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program is as follows: /* * * * * * * * * * * * * * * * * * * * * * * Lottery Simulation By Sam Halston * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include #define NMAX 6 int a[NMAX]; int b[NMAX]; Try to use more descriptive names than a and b. One is a client and the other is the government, is it not? It would be easier to help if these had some meaning. void getIntArray(int x[]); void bubbleSort(int x[]); void printArrays(int x[], int y[]); void getrandomarray(int x[]); void winnings(); int wins = 0; int main(void) { // welcome printf("\t Welcome To The National Lottery!\n"); printf("\tPlease Enter Six Whole Numbers Between 1 And 49\n\n"); getIntArray(a); getrandomarray(b); bubbleSort(a); bubbleSort(b); printf("Your Numbers In Numerical Order Are: \n"); printArrays(a, b); Are you happy with the results of that print? Knowing the answer to that divides the problem into two major "hunks". I scanned around but didn't spot anything noteworthy. I suppose there may well be an answer by the time I post this. Jul 22 '05 #3

 P: n/a You definately should initiate these, like init a[NMAX] = null; or so. If you mean initialize, they are by default. What is init a[NMAX] = null; Suppose to mean? Do you mean: int a[NMAX] = {0}; ? Jul 22 '05 #5

 P: n/a "Xenos" skrev i meddelandet news:c7*********@cui1.lmms.lmco.com... You definately should initiate these, like init a[NMAX] = null; or so. If you mean initialize, they are by default. What is init a[NMAX] = null; Suppose to mean? Do you mean: int a[NMAX] = {0}; ? Yes, i'm sorry. It was a long time ago since i coded C. /Andreas Jul 22 '05 #6

 P: n/a On Fri, 7 May 2004 18:55:04 +0100, "sam" wrote: hi, i am new to C/C++ and have just finished a program based on the UKnational lottery, where you enter 6 numbers, six are generated by thecomputer and there are appropriate messages and a compare function to decideyour winnings. I am using Microsoft's C++ version 6 standard, the programis as follows: "Before": void winnings(){ for(int i=0; i<6; i++) { for(int j=0; j<6; j++) { if(a[i]==b[j]) { wins++; } } if(wins==0) { printf("You Haven't Matched Any Numbers, Sorry\n"); return; } if(wins==1) { printf("You Have Matched One Number, Sorry\n"); return; } } if(wins==2) { printf("You Have Matched Two Numbers, Sorry\n"); return; } ... "After": void winnings() { printf("a: "); for (int i = 0; i < 6; i++) printf("%d ", a[i]); printf("\nb: "); for (int i = 0; i < 6; i++) printf("%d ", b[i]); for(int i=0; i<6; i++) { for(int j=0; j<6; j++) { if(a[i]==b[j]) { wins++; } } if(wins==0) { printf("You Haven't Matched Any Numbers, Sorry\n"); return; } if(wins==1) { printf("You Have Matched One Number, Sorry\n"); return; } } if(wins==2) { printf("You Have Matched Two Numbers, Sorry\n"); return; } // .... Notice anything different? ;-) -leor -- Leor Zolman --- BD Software --- www.bdsoft.com On-Site Training in C/C++, Java, Perl and Unix C++ users: download BD Software's free STL Error Message Decryptor at: www.bdsoft.com/tools/stlfilt.html Jul 22 '05 #7

 P: n/a "sam" wrote in message news:q4********************@brightview.com... hi, i am new to C/C++ and have just finished a program based on the UK national lottery, where you enter 6 numbers, six are generated by the computer and there are appropriate messages and a compare function to decide your winnings. I am using Microsoft's C++ version 6 standard, the program is as follows: void winnings() { for(int i=0; i<6; i++) { for(int j=0; j<6; j++) { if(a[i]==b[j]) { wins++; A "break;" would work well here, as you are done processing once you find the match, since they are sorted. } } You need a } HERE, it was moved down below the following... if(wins==1) { printf("You Have Matched One Number, Sorry\n"); return; } } ....to HERE. So fix that. Also: if(wins==6) { printf("You Have Matched 3 Numbers. Congratulations, You Have Won £5,000,000!!\n"); return; } wins == 6, but the message says 3. any quick fixes or suggestions as to an alternative end will be greatly appreciated, but if i could stick to this structure it would be good as i am trying hard to understand even what i've written! did anyone find this or am i just doomed to not be a programmer!? Well.... the code, in general, sucks, but then we all start somewhere. ;-) -- Mabden Jul 22 '05 #8

 P: n/a thanks for the help, it's looking a lot tidier now! i still cant get the numbers to compare properly at the end though, only 0 and 1 numbers works, as if im still doing a true or false comparison, why is it so hard to compare numbers!? im trying to get at: if the first number from the user inputted array equals or is equivalent to any of the numbers from the random generator, add 1. if the second.. etc then we should have up to 5 (6) matches i thought of covering all possible matches one at a time, ie if a[0] = b[0], b[1] etc, but that would take ages and won't look so good! Jul 22 '05 #9

 P: n/a sam wrote: thanks for the help, it's looking a lot tidier now! i still cant get the numbers to compare properly at the end though, only 0 and 1 numbers works, as if im still doing a true or false comparison, why is it so hard to compare numbers!? It isn't. Let me show you something. The beauty of consistent code indentation and how it helps to identify some type of bugs. Lets look at your winnings function(), but this time reformatted such that the indentation is consistent, correct and stands out a little bit more by using 2 spaces instead of just 1 void winnings() { for(int i=0; i<6; i++) { for(int j=0; j<6; j++) { if(a[i]==b[j]) { wins++; } } if(wins==0) { printf("You Haven't Matched Any Numbers, Sorry\n"); return; } if(wins==1) { printf("You Have Matched One Number, Sorry\n"); return; } } if(wins==2) { printf("You Have Matched Two Numbers, Sorry\n"); return; } if(wins==3) { printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n"); return; } if(wins==4) { printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n"); return; } if(wins==5) { printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n"); return; } if(wins==6) { printf("You Have Matched 3 Numbers. Congratulations, You Have Won £5,000,000!!\n"); return; } } Do you notice something? The if-s handling the case for 0 and 1 are not at the same indentation level then the other cases! Also the if-s for cases 0 and 1 are at the complete wrong indentation level. You need to compare *all* numbers, not just a[0] with all b[j] to decide if there are 0 or 1 matches. -- Karl Heinz Buchegger kb******@gascad.at Jul 22 '05 #10

 P: n/a "Karl Heinz Buchegger" wrote in message news:40***************@gascad.at... sam wrote: thanks for the help, it's looking a lot tidier now! i still cant get the numbers to compare properly at the end though, only 0 and 1 numbers works, as if im still doing a true or false comparison, why is it so hard to compare numbers!? It isn't. Let me show you something. The beauty of consistent code indentation and how it helps to identify some type of bugs. Lets look at your winnings function(), but this time reformatted such that the indentation is consistent, correct and stands out a little bit more by using 2 spaces instead of just 1 void winnings() { for(int i=0; i<6; i++) { for(int j=0; j<6; j++) { if(a[i]==b[j]) { wins++; } } if(wins==0) { printf("You Haven't Matched Any Numbers, Sorry\n"); return; } if(wins==1) { printf("You Have Matched One Number, Sorry\n"); return; } } if(wins==2) { printf("You Have Matched Two Numbers, Sorry\n"); return; } if(wins==3) { printf("You Have Matched 3 Numbers. Well Done, You Have Won £10\n"); return; } if(wins==4) { printf("You Have Matched 4 Numbers. Well Done, You Have Won £500\n"); return; } if(wins==5) { printf("You Have Matched 5 Numbers. Well Done, You Have Won £300,000!\n"); return; } if(wins==6) { printf("You Have Matched 3 Numbers. Congratulations, You Have Won £5,000,000!!\n"); return; } } Do you notice something? The if-s handling the case for 0 and 1 are not at the same indentation level then the other cases! Also the if-s for cases 0 and 1 are at the complete wrong indentation level. You need to compare *all* numbers, not just a[0] with all b[j] to decide if there are 0 or 1 matches. "And one more thing..." Am I the only one who notices case 6: "You Have Matched 3 Numbers"? -- Mabden Jul 22 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.