[Courses] C Programming For Absolute Beginners, Lesson 6: Moar Functions, Local, Static, Global Variables

Carla Schroder carla at bratgrrl.com
Thu Apr 12 04:42:39 UTC 2012


LOL! This is awesomely clever .

I got some compiler warnings; I'm traveling so I'll give it a closer look when 
I can. Perchance someone can figure out what is causing these warnings:

$ gcc -Wall -o ~/bin/take-cake take-cake.c
take-cake.c: In function ‘main’:
take-cake.c:37:9: warning: implicit declaration of function ‘AnnandFran_take’ 
[-Wimplicit-function-declaration]
take-cake.c:63:17: warning: unused variable ‘e’ [-Wunused-variable]
take-cake.c:62:17: warning: unused variable ‘d’ [-Wunused-variable]
take-cake.c:61:17: warning: unused variable ‘c’ [-Wunused-variable]
take-cake.c:60:17: warning: unused variable ‘b’ [-Wunused-variable]
take-cake.c:59:17: warning: unused variable ‘a’ [-Wunused-variable]
take-cake.c:71:1: warning: control reaches end of non-void function [-Wreturn-
type]

But it runs:

$ take-cake
  The party is over and your last 6 guests are leaving.
 There is leftover cake and you would like them to take it with them,
 so you will pass it around -- as many times as it takes --
     and ask them each to take some.
  Dan and Mike have roommates, so as long as there are more than 6 pieces
     on any given round, they will each take 2; 
     otherwise, they will take 1.
  Marcie will take 1 piece but she has a ride to catch, so she will
     only stay for the first round.
  Fred will take 1 piece on any round.
  Fran and Ann are dieting, so they will pass on the first round,
      but give in and split a piece between them on each subsequent round.
  Everyone is too polite to take the last piece, except for Fran and Ann,
      who will take it and split it.
   Let's see how many rounds it takes to unload all the cake.

Enter the number of pieces of cake you are starting with.
56

Round no. 1:
Dan took 2, Mike took 2, Marcie took 1, Fred took 1, and Fran and Ann took 0.
There are 50 pieces left.


Round no. 2:
Dan took 2, Mike took 2, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 44 pieces left.


Round no. 3:
Dan took 2, Mike took 2, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 38 pieces left.


Round no. 4:
Dan took 2, Mike took 2, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 32 pieces left.


Round no. 5:
Dan took 2, Mike took 2, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 26 pieces left.


Round no. 6:
Dan took 2, Mike took 2, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 20 pieces left.


Round no. 7:
Dan took 2, Mike took 2, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 14 pieces left.


Round no. 8:
Dan took 2, Mike took 2, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 8 pieces left.


Round no. 9:
Dan took 2, Mike took 1, Marcie was gone, Fred took 1, and Fran and Ann took 
1.
There are 3 pieces left.


Round no. 10:
Dan took 1, Mike took 1, Marcie was gone, Fred took 0, and Fran and Ann took 
1.
There are no pieces left.  Done!

Dan's total pieces:             19
Mike's total pieces:            18
Marcie's total pieces:          1
Fred's total pieces:            9
Ann and Fran's total pieces:    9




On Wednesday, April 11, 2012 08:06:41 PM Leslie wrote:
> On Sun, 2012-04-08 at 22:37 -0700, Carla Schroder wrote:
> 
> ====HOMEWORK====
> 
>
> 
> > Write your own program. Show your steps and then your program.
> 
> I set myself this problem:
> My party is over and my last few guests are leaving. There is leftover
> cake and I want them to take it home with them, so I will pass it around
> and ask them each to take some pieces, until it's gone.
>  Each guest has his/her own 'cake-taking' behavior, which I will capture
> in functions.
> 
> My steps:
> 1. Asked for user input of the number of cake slices to start with.
> 2. Wrote a function for each person's cake-taking behavior, with
> different arguments as needed.
> 3. When those were set, designed a loop to pass through the functions
> until the pieces of cake were gone.
> 4.  Tweaked the printouts so I could see what each person did in each
> round -- a good way to check.
> 5.  Read the lesson more attentively (it always helps to read the
> lesson!) and discovered that static variables would be a great way to
> capture cumulative counts for each person.
> 6.  Added the argument 'lastround' to everyone's function so I could set
> that as a flag and grab the static total counts at the end.
> 7.  Ran it with various numbers to make sure it behaved.
> 
> So here's the program and I hope this behemoth makes it through on
> e-mail:
> 
> //TakeCake.c
> 
> #include <stdio.h>
> 
> //function prototypes
> int Dan_takes(int, int);
> int Mike_takes(int, int);
> int Marcie_takes(int, int, int);
> int Fred_takes(int, int);
> int AnnandFran_takes(int, int);
> 
> int main (void)
> {
>     int piecesstart = 0;
>     int round = 1;
>     int piecesleft = 0;
>     // the following variable is either 0(for all rounds but the last)
> or 1 (for the last).
>     int lastround = 0;
> 
>     puts ("  The party is over and your last 6 guests are leaving.\n
> There is leftover cake and you would like them to take it with them,\n
> so you will pass it around -- as many times as it takes --\n     and ask
> them each to take some.\n  Dan and Mike have roommates, so as long as
> there are more than 6 pieces\n     on any given round, they will each
> take 2; \n     otherwise, they will take 1.\n  Marcie will take 1 piece
> but she has a ride to catch, so she will\n     only stay for the first
> round.\n  Fred will take 1 piece on any round.\n  Fran and Ann are
> dieting, so they will pass on the first round,\n      but give in and
> split a piece between them on each subsequent round.\n  Everyone is too
> polite to take the last piece, except for Fran and Ann,\n      who will
> take it and split it.\n   Let's see how many rounds it takes to unload
> all the cake.\n");
> 
>     printf("Enter the number of pieces of cake you are starting
> with.\n");
>     scanf ( "%d", &piecesstart);
>     piecesleft = piecesstart;
> 
>     while (piecesleft > 0)
>     {
>         int a = Dan_takes (piecesleft, lastround);
>         piecesleft = (piecesleft - a);
>         int b = Mike_takes (piecesleft, lastround);
>         piecesleft = (piecesleft - b);
>         int c = Marcie_takes (piecesleft, round, lastround);
>         piecesleft = (piecesleft -c);
>         int d = Fred_takes (piecesleft, lastround);
>         piecesleft = (piecesleft - d);
>         int e = AnnandFran_take (round, lastround);
>         piecesleft = (piecesleft - e);
> 
>         printf ( "\nRound no. %d:\n",round);
>         printf ( "Dan took %d, Mike took %d, ", a,b);
>         if (round == 1)
>         {
>             printf ("Marcie took %d, ", c);
>         }
>         else
>         {
>              printf ("Marcie was gone, ");
>         }
>         printf ("Fred took %d, and Fran and Ann took %d.\n",d, e);
>         if (piecesleft == 1)
>         {
>             printf ( "There is one piece left.\n\n");
>         }
>         else if (piecesleft == 0)
>         {
>             printf ( "There are no pieces left.  Done!\n\n");
>             lastround = 1;
>             int a = Dan_takes (piecesleft, lastround);
>             int b = Mike_takes (piecesleft, lastround);
>             int c = Marcie_takes (piecesleft, round, lastround);
>             int d = Fred_takes (piecesleft, lastround);
>             int e = AnnandFran_take (round, lastround);
>         }
>         else
>         {
>             printf ( "There are %d pieces left.\n\n",piecesleft);
>         }
>         round ++;
>    }
> }
> 
> int Dan_takes (int c, int l)
> {
>    static int total = 0;
>    int a = 0;
>    if (c > 6)
>    {
>       a = 2;
>    }
>    else if (c < 2)
>    {
>       a = 0;
>    }
>    else
>    {
>       a = 1;
>    }
>    total = total + a;
>    if (l == 1)
>    {
>        printf ("Dan's total pieces:\t\t%d\n", total);
>    }
>    return a;
> }
> 
> int Mike_takes (int c, int l)
> {
>    static int total = 0;
>    int a = 0;
>    if (c > 6)
>    {
>       a = 2;
>    }
>    else if (c < 2)
>    {
>       a = 0;
>    }
>    else
>    {
>       a = 1;
>    }
>    total = total + a;
>    if (l == 1)
>    {
>        printf ("Mike's total pieces:\t\t%d\n", total);
>    }
>    return a;
> }
> 
> int Marcie_takes (int c, int d, int l)
> {
>    static int total = 0;
>    int a = 0;
>    if ( d == 1)
>    {
>        if (c > 1)
>        {
>           a = 1;
>        }
>        else
>        {
>           a = 0;
>        }
>    }
>    else
>    {
>       a = 0;
>    }
>    total = total + a;
>    if (l == 1)
>    {
>        printf ("Marcie's total pieces:\t\t%d\n", total);
>    }
>    return a;
> }
> 
> int Fred_takes (int c, int l)
> {
>    static int total = 0;
>    int a = 0;
>    if (c > 1)
>    {
>       a = 1;
>    }
>    else
>    {
>       a = 0;
>    }
>    total = total + a;
>    if (l == 1)
>    {
>        printf ("Fred's total pieces:\t\t%d\n", total);
>    }
>    return a;
> }
> 
> int  AnnandFran_take (int r, int l)
> {
>    static int total = 0;
>    int a = 0;
>    if (r > 1)
>    {
>       a = 1;
>    }
>    total = total + a;
>    if (l == 1)
>    {
>        total = total -1;
>        printf ("Ann and Fran's total pieces:\t%d\n\n", total);
>    }
>    return a;
> }
> 
> That's it!
> 
> 
> -Leslie
> 
> _______________________________________________
> Courses mailing list
> Courses at linuxchix.org
> http://mailman.linuxchix.org/mailman/listinfo/courses
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Carla Schroder
ace Linux nerd
author of Linux Cookbook,
Linux Networking Cookbook,
Book of Audacity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


More information about the Courses mailing list