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

Leslie leslie.brothers at verizon.net
Thu Apr 12 03:06:41 UTC 2012


On Sun, 2012-04-08 at 22:37 -0700, Carla Schroder wrote:

====HOMEWORK====

> Modify staticdemo with different values for all the variables and see what 
> happens, and explain how it works.
As you said, the static variable always retains its value.

> The for loop in my example is supposed to stop when a is no 
> longer less than 10-- but staticvariable stops at 11. Why?
There are 10 iterations (0-9 inclusive), and we start at 2, so that
takes us to 11.

> 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



More information about the Courses mailing list