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

Leslie leslie.brothers at verizon.net
Sat Apr 14 21:40:31 UTC 2012


Akkana and Simon,
	Thank you!  I have learned many things from this homework.  For one
thing, if I had taken Akkana's advice back on Feb. 5 to heart -- namely,
use -Wall when compiling -- I would have detected some of the obvious
things like typos.  So, lesson learned.
	Simon, I really appreciate the helpful details and comments.  At the
end you wrote:

"Ok, now I am hungry but there is no cake left... :o("

and so I fixed that too! (See below.)
	I am also looking forward to learning how to do error detection for
scanf and fopen (as mentioned previously by Jacinta).
	Doing the revisions was a good exercise and I am happy to learn about
the boolean datatype too.

-Leslie

*****************************

//TakeCakeRev3.c

#include <stdio.h>
#include <stdbool.h>

//function prototypes
int Dan_takes(int, bool);
int Mike_takes(int, bool);
int Marcie_takes(int, int, bool);
int Fred_takes(int, bool);
int AnnandFran_take(int,int, bool);

int main (void)
{
    int piecesstart = 0;
    int round = 1;
    int piecesleft = 0;
    bool bLastRound = false;

    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 agrees to save the last piece for Simon.\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 > 1)
    {
	int a, b, c, d, e;
        a = Dan_takes (piecesleft, bLastRound);
        piecesleft -= a;
        b = Mike_takes (piecesleft, bLastRound);
        piecesleft -= b;
        c = Marcie_takes (piecesleft, round, bLastRound);
        piecesleft -= c;
        d = Fred_takes (piecesleft, bLastRound);
        piecesleft -= d;
        e = AnnandFran_take (piecesleft, round, bLastRound);
        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 and we're saving it for
Simon.\n\n");
            bLastRound = true;
            Dan_takes (piecesleft, bLastRound);
            Mike_takes (piecesleft, bLastRound);
            Marcie_takes (piecesleft, round, bLastRound);
            Fred_takes (piecesleft, bLastRound);
            AnnandFran_take (piecesleft, round, bLastRound);
            printf ("Simon's total pieces:\t\t1\n\n");
        }
        else
        {
            printf ( "There are %d pieces left.\n\n",piecesleft);
        }
        round ++;    
   }
   return 0;
}

int Dan_takes (int c, bool p)
{
   static int total = 0;
   int a = 0;
   if (c > 6)
   {
      a = 2;
   }
   else if (c < 2)
   {
      a = 0;
   }
   else
   {
      a = 1;
   }
   total += a;
   if (p)
   {
       printf ("Dan's total pieces:\t\t%d\n", total);
   }
   return a;
}

int Mike_takes (int c, bool p)
{
   static int total = 0;
   int a = 0;
   if (c > 6)
   {
      a = 2;
   }
   else if (c < 2)
   {
      a = 0;
   }
   else
   {
      a = 1;
   }
   total += a;
   if (p)
   {
       printf ("Mike's total pieces:\t\t%d\n", total);
   }
   return a;
}

int Marcie_takes (int c, int d, bool p)
{
   static int total = 0;
   int a = 0;
   if ( d == 1)
   {
       if (c > 1)
       {
          a = 1;
       }
       else 
       {
          a = 0;
       }
   }
   else
   {
      a = 0;
   }
   total += a;
   if (p)
   {
       printf ("Marcie's total pieces:\t\t%d\n", total);
   }
   return a;
}

int Fred_takes (int c, bool p)
{
   static int total = 0;
   int a = 0;
   if (c > 1)
   {
      a = 1;
   }
   else 
   {
      a = 0;
   }
   total += a;
   if (p)
   {
       printf ("Fred's total pieces:\t\t%d\n", total);
   }
   return a;
}

int  AnnandFran_take (int c, int r, bool p)
{
   static int total = 0;
   int a = 0;
   if (c > 1)
   {
       if (r > 1)
       {
          a = 1;
       }
       else
       {
          a = 0;
       }
   }
   else
   {
       a =0;
   }
   total += a;
   if (p)
   {
       printf ("Ann and Fran's total pieces:\t%d\n", total);
   }
   return a;
}





More information about the Courses mailing list