Skip to content
View in the app

A better way to browse. Learn more.

Tip.It Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

c programmers, please help

Featured Replies

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.

limpxbizkid.png

i+r+newblet.png

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.

  • Author

cool, thx alot. this concludes my homework for tonight, jk its cisco time now.

limpxbizkid.png

i+r+newblet.png

  • Author

hmm, it doesnt seem to work. i keep getting an undeclared error with the vars function.

limpxbizkid.png

i+r+newblet.png

That's because that is Javascript code, won't work under C.

  • Author

ok, whats the c equiv of vars? :?

limpxbizkid.png

i+r+newblet.png

whatever you need to declare them as in c.

 

 

 

int, string, etc

  • Author

ok, fixed. thx everyone

limpxbizkid.png

i+r+newblet.png

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.