May 25, 200521 yr ok heres my program. 2 random numers are generated. the first one a day, and the second is a month. limits are set for the second (1-12, duh, 12months in a year) and the first number is given a limit depenting on the 2nd (eg jan has 31, 28 days for feb and so on). now heres the problem. i (and many others, well, i only agree because of others) considder it "bad programming" (which in turn, makes you a "bad person") to just have a print statement for every condition. heres the code: #include #include #include int main(int argc, char *argv[]) { int m, n; srand(time(NULL)); m = rand() % 12 + 1; if(m == 1) { n = rand() % 31 + 1; printf("The date is January %d\n", n); } else if(m == 2) { n = rand() % 28 + 1; printf("The date is February %d\n", n); } else if(m == 3) { n = rand() % 31 + 1; printf("The date is March %d\n", n); } else if(m == 4) { n = rand() % 30 + 1; printf("The date is April %d\n", n); } else if(m == 5) { n = rand() % 31 + 1; printf("The date is May %d\n", n); } else if(m == 6) { n = rand() % 30 + 1; printf("The date is June %d\n", n); } else if(m == 7) { n = rand() % 31 + 1; printf("The date is July %d\n", n); } else if(m == 8) { n = rand() % 31 + 1; printf("The date is August %d\n", n); } else if(m == 9) { n = rand() % 30 + 1; printf("The date is September %d\n", n); } else if(m == 10) { n = rand() % 31 + 1; printf("The date is October %d\n", n); } else if(m == 11) { n = rand() % 30 + 1; printf("The date is November %d\n", n); } else if(m == 12) { n = rand() % 31 + 1; printf("The date is December %d\n", n); } printf("The month number is %d, the day is %d\n", m, n); system("PAUSE"); return 0; } is there a way to make it better. to make it do, when it gets to the if statement jsut look up the value in some way? and also does it really need 12 if statements? edit: i refuse to disable smilies for this post. so yes, august is the 'cool' month.
May 25, 200521 yr Sure. Store the number of days in a month, and the name of the month in an array of objects, and pick a random item in the array. Then you can use the days value in the array to generate a random day, too. (much cleaner). However, my C knowledge is quite lacking, so I have no idea how you'd do it in C. I could do it in JavaScript though (which might be a helpful reference): var months = [ {name: "January", days: 31}, {name: "February", days: 28}, {name: "March", days: 31}, {name: "April", days: 30}, {name: "May", days: 31}, {name: "June", days: 30}, {name: "July", days: 31}, {name: "August", days: 31}, {name: "September", days: 30}, {name: "October", days: 31}, {name: "November", days: 30}, {name: "December", days: 31}]; var randomMonth = Math.floor(Math.random() * 12); var randomDay = Math.floor(Math.random() * months[randomMonth].days) // Alert (pop up a dialog box) for lack of a print statement in JS alert("The date is " + months[randomMonth].name + " " + String(randomDay) + ".") /* Note that javascript doesn't have the oh-so-handy printf statement, so we have to concatenate manually, see above. * It could be done neater using a lambda replace, but if you just started learning C then that'd probably be way over your head for now. */ Again, note my C knowledge with regards to arrays and objects is lacking, but your teacher should probably be able to help you here.
May 25, 200521 yr Author cool, thx alot. this concludes my homework for tonight, jk its cisco time now.
May 25, 200521 yr Author hmm, it doesnt seem to work. i keep getting an undeclared error with the vars function.
Create an account or sign in to comment