Learning C Language 
Author Message
 Learning C Language

Hi I'm new here and I have a few questions about my program that I
writing in C language.  My assignment is:

You have found some{*filter*}roaches in your apartment.  Rather than
call the exterminator, you decide to perform an experiment.  You
count the number of roaches and then wait a week and count them
again to determine their breeding rate.  Print out the estimated
roach population from that point on, assuming that the breeding
rate remains a constant. Stop at the week that shows over a
million roaches.  You need not actually contnue the experiment ot
valiadate your computer results, call the exterminator!

Now, Incorporate simple error checking on input values, i.e., the
number of roaches that you enter cannot be less than or equal to
zero.

My problem is having a way to make sure a person, won't enter
in 1.5 roaches.. This is a simply program.. But this is my first
one.. I can use any help someone can provide me. I tried to put up
my program but the buffer wouldn't hold it...

Thanks for your help..
                -Gen James

--
Gen James



Sun, 26 Jul 1998 03:00:00 GMT  
 Learning C Language

: Hi I'm new here and I have a few questions about my program that I
: writing in C language.  My assignment is:
:
: You have found some{*filter*}roaches in your apartment.  Rather than
: call the exterminator, you decide to perform an experiment.  You
: count the number of roaches and then wait a week and count them
: again to determine their breeding rate.  Print out the estimated
: roach population from that point on, assuming that the breeding
: rate remains a constant. Stop at the week that shows over a
: million roaches.  You need not actually contnue the experiment ot
: valiadate your computer results, call the exterminator!
:
: Now, Incorporate simple error checking on input values, i.e., the
: number of roaches that you enter cannot be less than or equal to
: zero.
:
: My problem is having a way to make sure a person, won't enter
: in 1.5 roaches.. This is a simply program.. But this is my first
: one.. I can use any help someone can provide me. I tried to put up
: my program but the buffer wouldn't hold it...
:

You could always read the initial number of roaches as a real number and
then truncate it to an integer.  After all, a fractional roach must be
dead and can't breed. :)
----------------------------------------------------------------------------

| #include "std_disclaimers.h"                                             |
----------------------------------------------------------------------------



Tue, 28 Jul 1998 03:00:00 GMT  
 Learning C Language
: Hi I'm new here and I have a few questions about my program that I
: writing in C language.  My assignment is:

: You have found some{*filter*}roaches in your apartment.  Rather than
: call the exterminator, you decide to perform an experiment.  You
: count the number of roaches and then wait a week and count them
: again to determine their breeding rate.  Print out the estimated
: roach population from that point on, assuming that the breeding
: rate remains a constant. Stop at the week that shows over a
: million roaches.  You need not actually contnue the experiment ot
: valiadate your computer results, call the exterminator!

: Now, Incorporate simple error checking on input values, i.e., the
: number of roaches that you enter cannot be less than or equal to
: zero.

: My problem is having a way to make sure a person, won't enter
: in 1.5 roaches.. This is a simply program.. But this is my first
: one.. I can use any help someone can provide me. I tried to put up
: my program but the buffer wouldn't hold it...

: Thanks for your help..
:               -Gen James

Hmm, with a bad-style-homework-answer contest going on in full blast,
are you really sure you want an answer here?

Just being curious :)

[to the moderator: can we use this as problem #4?]

[Sure. -mod]

Aside, if you really want heavy duty input validation, you'll have to
input strings and do your input conversion yourself.  Look up the
Horner's Scheme, though.

/Alby



Tue, 28 Jul 1998 03:00:00 GMT  
 Learning C Language
/*/ Homework assignment on counting{*filter*}roaches.
/*/

# marks a comment under UNIX.  /* As does this. */

#   /* make sure we get a prototype for printf, because ANSI says
       we can't call it without one.

       Makes you think, doesn't it.  How did K&R compilers manage?
       We had *real* compilers in those days... */ include <stdio.h>

char chaR[]="a buffer";

#define mi11i0n 1000000 /* for clarity */

main() {
  static
  /* we make the roaches static to make them easier to catch. */
  l1,ll;
  /* first we need to count the roaches here this week. */
  printf("Type in all the roaches you can see.  There must be at least one,%s"
         "\0%s","\n(assuming you're a student).\n");
  do fgets(chaR,
  sizeof(chaR)/sizeof(char)
  ,stdin);

  /* this is the main input loop */
  while(strcmp("roach\n",
        chaR)==sizeof(double)
        -sizeof
        1.0&&++l1)
    ; /* I prefer ; to {} in empty loops for stylistic reasons. */
  /* if the user says there are 1.5{*filter*}roaches, he's lying.
  This test is very important.
  I can't decide which file the message should go to. */
  if(l1==1.5) fprintf(0, "you seem to have missed half a{*filter*}roach.\n");

  /* now count the{*filter*}roaches next week. */
  sleep(604800);
  do fgets(chaR,6,stdin);while(!strcmp("roach\n",chaR)&&++ll);
  /* now output the number of roaches we think there are each week,
  until there are a million. */
  ll-=l1;  /* this is the growth rate (assumed to be constant). */
  while((l1+=ll)<=1l*mi11i0n)
    sleep(604800),
    printf("there are now %d{*filter*}roaches.\n", l1);

  /* now there are at least a million roachs, we can call the exterminator.
  this isn't covered directly by the ANSI C standard, so it can only be
  done in an implementation-defined way. */
  system("echo help! | mail exterminator");

Quote:
}

--



Wed, 29 Jul 1998 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. C as 2nd CS language (was Re: Languages ...

2. SURVEY of CS depts. (languages)

3. C as First Language for CS

4. Programming Language for Undergrad Intro CS Course?

5. Newbie: separate big .cs file into small .cs files

6. I want to learn C language

7. learning c language

8. Need guidance in learning the C language

9. Other Languages to learn...Have any suggestions

10. Seeking help for learning C programming language

11. good way to learn c (as a first language)

12. Wan'a learn another programing language.

 

 
Powered by phpBB® Forum Software