# Boat loader my practice program

• 05-29-2010
jimtuv
Ferry Boat Balancing my practice program
This is a quick practice program I did this morning.

You are loading a ferry and need to keep it in balance. This program determines if the number of cars are even or odd and balances the boat for you.

My question is: How is the style? Is it logical in it's structure? Is there any advice you have to improve the program?

Here is the code.

Code:

```/* Ferry boat loading program  *  * File:  main.c  * Author: James Tuvell  *  * Created on May 29, 2010, 9:24 AM  * This program is practice.  *  * In it the user will input a number up to 9  * and the program will determine whether it is  * even or odd number and print out a boat loading  * layout that keeps the boat balanced  */ #include <stdio.h> #include <stdlib.h> /*  *Define Constants  */ #define MinCars '0' #define MaxCars '9' #define EVEN 0 #define ODD 1 #define CURSOR ">>" #define ASCII 48 // Define the boat #define BowTip      "      ^" #define Bow1        "    / \\" #define Bow2        "    /  \\" #define Bow3        "  /    \\" #define Bow_WCar    "  /  x  \\" #define Bow_WOCar    "  /      \\" #define Bow4        " /        \\" #define Middle_First " |  ***  |" #define Middle_Wcar  " | x | | x |" #define Middle_WOCar " |  | |  |" #define Middle_Last  " |  ***  |" #define Aft1        " \\        /" #define Aft2        "  \\_______/" #define Ramp          "    |  |  " void Print_Instructions (void); int EvenOdd ( int number ); int UserInput (void); void ClearBuff (void); void PrintBoat (int numb, int odd); void PrintBow ( int cars , int odd); void PrintMid ( int cars); void PrintAft (void); void PrintLine (void); /*------------------------------------------------------------  * main  * Parameters: NONE  * Gets      : UserInput  * Prints    : Boat balancing layout  * Returns  : Nothing  *------------------------------------------------------------*/ int main(void) {     int rvalue;     int numb;     Print_Instructions ();     numb = UserInput();     rvalue = EvenOdd (numb);     PrintBoat (numb,rvalue);     return 0; } /*------------------------------------------------------------  * Print Instructions  * Parameters: NONE  * Prints    : The Instructions  * Returns  : Nothing  *------------------------------------------------------------*/ void Print_Instructions (void){     PrintLine();     printf ("This is the automatic boat loader");     PrintLine();     printf ("Just input the number of cars to load");     PrintLine();     printf ("and I will print out a loading chart");     PrintLine();     printf ("for you to use to keep the boat in balance");     PrintLine(); } /*------------------------------------------------------------  * EvenOdd  * Parameters:  * number = then number to check wether even or odd  * Prints    : Nothing  * Returns  : EVEN or ODD ( 0 or 1)  *------------------------------------------------------------*/ int EvenOdd ( int number ){         int even;         even = number % 2;             if ( even == EVEN ) return EVEN;         return ODD; } /*------------------------------------------------------------  * UserInput  * Parameters: NONE  * Prints    : Nothing  * Returns  : A number between 0 and 9  *------------------------------------------------------------*/ int UserInput (void){         int input;     input = MinCars;         printf ("Enter the number of cars to load (0-9)\n");     do{         printf ( CURSOR );         input = getchar();         ClearBuff ();          }     while ( input < MinCars || input > MaxCars);         return input-ASCII; } /*------------------------------------------------------------  * ClearBuff  * clears the stdin of extra stuff  * Parameters: NONE  * Prints    : Nothing  * Returns  : Nothing  *------------------------------------------------------------*/ void ClearBuff (void){         while (getchar() != '\n');     } /*------------------------------------------------------------  * Print Boat  * Parameters:  * numb = number of cars to load  * odd = is the number of cars odd  * Prints    : The boat using calls to Printbow,PrintMid and PrintAft  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBoat (int numb, int odd){     PrintBow (numb , odd);     if ( odd == ODD ) PrintMid (numb-1);     else PrintMid (numb);     PrintAft (); } /*------------------------------------------------------------  * PrintBow  * Parameters:  * cars = number of cars to load  * odd = is that value odd  * Prints    : The bow of the boat checking on if a car should go upfront  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBow ( int cars, int odd){     int load;         if (odd == ODD){         load = cars -1;         printf (BowTip);         PrintLine();         printf (Bow1);         PrintLine();         printf (Bow2);         PrintLine();         printf (Bow3);         PrintLine();         printf (Bow_WCar);         PrintLine();         printf (Bow4);         PrintLine();     }     else{         printf (BowTip);         PrintLine();         printf (Bow1);         PrintLine();         printf (Bow2);         PrintLine();         printf (Bow3);         PrintLine();         printf (Bow_WOCar);         PrintLine();         printf (Bow4);         PrintLine();          } } /*------------------------------------------------------------  * PrintMid  * Parameters:  * cars = the even number of cars to load (2,4,6,8)  * Prints    : The middle of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintMid ( int cars){         printf (Middle_First);     PrintLine();     if ( cars < 6 ) printf (Middle_WOCar);     else printf (Middle_Wcar);     PrintLine();     if ( cars > 0) printf (Middle_Wcar);     else printf (Middle_WOCar);     PrintLine();     if ( cars > 2 && cars != 6) printf (Middle_Wcar);     else printf (Middle_WOCar);     PrintLine();     if ( cars == 6 || cars == 8) printf (Middle_Wcar);     else printf (Middle_WOCar);     PrintLine();     printf (Middle_Last);     PrintLine();      } /*------------------------------------------------------------  * PrintLine  * Parameters: NONE  * Prints    : A line feed to make things more readable  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintLine (void){     printf("\n"); } /*------------------------------------------------------------  * PrintAft  * Parameters: NONE  * Prints    : The aft of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintAft (void){     printf (Aft1);     PrintLine();     printf (Aft2);     PrintLine();     printf (Ramp);     PrintLine(); }```
• 05-29-2010
Salem
Oh man, I need new glasses - I thought you said "boot loader" :eek:
• 05-29-2010
jimtuv
lol I changed the title to avoid that
• 05-29-2010
claudiu
I have to say this is one of the most original practice programs I have seen in a while. I haven't actually checked any of the logic, but aesthetically the code looks pretty good and readable.
• 05-29-2010
msh
Style is good. Only nit for me to pick is that you (sometimes) put a space between the function name and parenthesis. This is usually reserved for loops.

E.g.
Code:

```while (1); functionCall(int p1, int p2);```
I also put a space between opening parenthesis and the first argument/condition to function call/loop if this argument/condition is another, immediate function call. E.g.:
Code:

```while ( getchar() != '\n'); f( f());```
• 05-29-2010
Just a few suggestions:

1) printAft, printMid, but printbow, instead of "printBow"? Be consistent, change printbow to printBow in your comments.
Quote:

The boat using calls to Printbow,PrintMid and PrintAft
2) A function just to print '\n' ? Just one char? Just include the newline char you want, at the start of the next print statement (or at the end of the last one).

3) #define EVEN and ODD is unneeded. Just a local oddeven variable, would be fine. and it's one line of code. Great that you're using functions, but one line functions are taking it a bit far. Yes, that's my opinion, but who's opinion did you expect to read, here? :D

4) "Aft" is a direction on a boat, not a place. There is no "boat's aft". The term is "stern". It's like printing "car's South".

Also, you don't "balance" a boat. You "trim" it**. Landlubbers! < sigh > :p :p

**4 a (1) : to cause (as a ship) to assume a desirable position in the water by arrangement of ballast, cargo, or passengers (2) : to adjust (as an airplane or submarine) for horizontal movement or for motion upward or downward b : to adjust (as cargo or a sail) to a desired position
• 05-29-2010
jimtuv
@claudiu Thank you I do try to make my practice programs fun

@msh I didn't even notice I was doing that. I corrected the code to leave out those spaces. Thanks for pointing that out.

@Adak I really appriciate your opinions and expect to here how you would do it.

as for the PrintLine function. I know it's taking things a bit too far. I included it just to clean up the #defines a bit.

I renamed the functions and code to change from Aft to Stern and replaced the balance reference to trim.

I realized I had unnecessary duplication of code in the PrintBow function so I cleaned that up as well.

Here is the revised code:

Code:

```/* Ferry boat loading program  *  * File:  main.c  * Author: James Tuvell  *  * Created on May 29, 2010, 9:24 AM  * This program is practice.  *  * In it the user will input a number up to 9  * and the program will determine whether it is  * even or odd number and print out a boat loading  * layout that keeps the boat trimmed  */ #include <stdio.h> #include <stdlib.h> /*  *Define Constants  */ #define MinCars '0' #define MaxCars '9' #define EVEN 0 #define ODD 1 #define CURSOR ">>" #define ASCII 48 // Define the boat #define BowTip      "      ^      \n" #define Bow1        "    / \\    \n" #define Bow2        "    /  \\    \n" #define Bow3        "  /    \\  \n" #define Bow_WCar    "  /  x  \\  \n" #define Bow_WOCar    "  /      \\  \n" #define Bow4        " /        \\ \n" #define Middle_First " |  ***  |  \n" #define Middle_Wcar  " | x | | x |  \n" #define Middle_WOCar " |  | |  |  \n" #define Middle_Last  " |  ***  |  \n" #define Stern1      " \\        /  \n" #define Stern2      "  \\_______/  \n" #define Ramp        "    |  |  " void Print_Instructions(void); int EvenOdd( int number ); int UserInput(void); void ClearBuff(void); void PrintBoat(int numb, int odd); void PrintBow( int cars , int odd); void PrintMid( int cars); void PrintStern(void); /*------------------------------------------------------------  * main  * Parameters: NONE  * Gets      : UserInput  * Prints    : Boat trimming layout  * Returns  : Nothing  *------------------------------------------------------------*/ int main(void) {     int rvalue;     int numb;     Print_Instructions();     numb = UserInput();     rvalue = EvenOdd(numb);     PrintBoat(numb,rvalue);     return 0; } /*------------------------------------------------------------  * Print Instructions  * Parameters: NONE  * Prints    : The Instructions  * Returns  : Nothing  *------------------------------------------------------------*/ void Print_Instructions(void){       printf ("\nThis is the automatic boat loader\n");     printf ("Just input the number of cars to load\n");     printf ("and I will print out a loading chart\n");     printf ("for you to use to keep the boat trimmed\n");     } /*------------------------------------------------------------  * EvenOdd  * Parameters:  * number = then number to check wether even or odd  * Prints    : Nothing  * Returns  : EVEN or ODD ( 0 or 1)  *------------------------------------------------------------*/ int EvenOdd( int number ){         int even;         even = number % 2;             if ( even == EVEN ) return EVEN;         return ODD; } /*------------------------------------------------------------  * UserInput  * Parameters: NONE  * Prints    : Nothing  * Returns  : A number between 0 and 9  *------------------------------------------------------------*/ int UserInput(void){         int input;     input = MinCars;         printf("Enter the number of cars to load (0-9)\n");     do{         printf( CURSOR );         input = getchar();         ClearBuff();          }     while ( input < MinCars || input > MaxCars);         return input-ASCII; } /*------------------------------------------------------------  * ClearBuff  * clears the stdin of extra stuff  * Parameters: NONE  * Prints    : Nothing  * Returns  : Nothing  *------------------------------------------------------------*/ void ClearBuff(void){         while(getchar() != '\n');     } /*------------------------------------------------------------  * Print Boat  * Parameters:  * numb = number of cars to load  * odd = is the number of cars odd  * Prints    : The boat using calls to PrintBow,PrintMid and PrintAft  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBoat(int numb, int odd){     PrintBow(numb , odd);     if ( odd == ODD ) PrintMid(numb-1);     else PrintMid(numb);     PrintStern(); } /*------------------------------------------------------------  * PrintBow  * Parameters:  * cars = number of cars to load  * odd = is that value odd  * Prints    : The bow of the boat checking on if a car should go upfront  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBow( int cars, int odd){       printf(BowTip);     printf(Bow1);     printf(Bow2);     printf(Bow3);     if (odd == ODD){              printf(Bow_WCar);          }     else{                printf(Bow_WOCar);              }     printf(Bow4); } /*------------------------------------------------------------  * PrintMid  * Parameters:  * cars = the even number of cars to load (2,4,6,8)  * Prints    : The middle of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintMid ( int cars){         printf(Middle_First);     if ( cars < 6 ) printf(Middle_WOCar);     else printf(Middle_Wcar);     if ( cars > 0) printf(Middle_Wcar);     else printf(Middle_WOCar);     if ( cars > 2 && cars != 6) printf(Middle_Wcar);     else printf(Middle_WOCar);     if ( cars == 6 || cars == 8) printf(Middle_Wcar);     else printf(Middle_WOCar);     printf(Middle_Last);           } /*------------------------------------------------------------  * PrintStern  * Parameters: NONE  * Prints    : The stern of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintStern(void){     printf(Stern1);     printf(Stern2);     printf(Ramp);   }```
• 05-29-2010
I prefer concise, almost minimalist, programming *IF* the clarity of the program, is not sacrificed.

A great sculptor once was asked how he knew when his project was done. His wise answer was that it was done, when he could not remove anything further from it.

In programming, you have a LOT of STUFF you MUST program - lots and lots of code, for any kind of a serious program.

So it's very important to use commonly recognized concise lines of code, to help this "project" get done, as the sculptor noted.

For the number being odd or even, Consider the simple:

Code:

```if(number%2)   return 1; //it's odd else   return 0;  //it's even which you can see, leads to the even simpler: return (number%2);```
And no, I wouldn't use a function just for that one line of code.

In the book, "The C Programming Language", the creators of C emphasize that C should be used as a concise programming language. If you want flowery fluff, consider OOP with C++. Just earlier this week, Sebastiani posted up a very nice C++ program of a page and a half's worth of code, to augment a ONE statement for loop, in plain C. (It's in the second page of the "Python-->C" thread, iirc).

Keep that in mind. The longer the code, the more variables, the more "Rube Goldberg" the logic, the more chances that you will have errors. That is what a programmer is always fighting against - errors. Easy to do in a small program like this, but very hard to do in a large program.
• 05-29-2010
jimtuv
@Adak I completely agree! Unfortunately my brain seems to want the most complex code possible. But that's what all the practice is for isn't it. To learn ones weakness and hammer that area until it becomes a strength.

I stripped it of all code I could find that would be extra (not messing with the comments);

I paired that EvenOdd function down to

Code:

`evenodd = numb % 2;`
Here is the revised code:

Code:

```/* Ferry boat loading program  *  * File:  main.c  * Author: James Tuvell  *  * Created on May 29, 2010, 9:24 AM  * This program is practice.  *  * In it the user will input a number up to 9  * and the program will determine whether it is  * even or odd number and print out a boat loading  * layout that keeps the boat trimmed  */ #include <stdio.h> #include <stdlib.h> /*  *Define Constants  */ #define MinCars '0' #define MaxCars '9' #define EVEN 0 #define ODD 1 #define CURSOR ">>" #define ASCII 48 // Define the boat #define BowTip      "      ^      \n" #define Bow1        "    / \\    \n" #define Bow2        "    /  \\    \n" #define Bow3        "  /    \\  \n" #define Bow_WCar    "  /  x  \\  \n" #define Bow_WOCar    "  /      \\  \n" #define Bow4        " /        \\ \n" #define Middle_First " |  ***  |  \n" #define Middle_Wcar  " | x | | x |  \n" #define Middle_WOCar " |  | |  |  \n" #define Middle_Last  " |  ***  |  \n" #define Stern1      " \\        /  \n" #define Stern2      "  \\_______/  \n" #define Ramp        "    |  |  " /*  * Function templates  */ void Print_Instructions(void); int UserInput(void); void PrintBoat(int numb, int odd); void PrintBow( int cars , int odd); void PrintMid( int cars); void PrintStern(void); /*------------------------------------------------------------  * main  * Parameters: NONE  * Gets      : UserInput  * Prints    : Boat trimming layout  * Returns  : Nothing  *------------------------------------------------------------*/ int main(void) {     int evenodd;     int numb;     Print_Instructions();     numb = UserInput();     evenodd = numb % 2;     PrintBoat(numb,evenodd);     return 0; } /*------------------------------------------------------------  * Print Instructions  * Parameters: NONE  * Prints    : The Instructions  * Returns  : Nothing  *------------------------------------------------------------*/ void Print_Instructions(void){       printf ("\nThis is the automatic boat loader\n");     printf ("Just input the number of cars to load\n");     printf ("and I will print out a loading chart\n");     printf ("for you to use to keep the boat trimmed\n");     } /*------------------------------------------------------------  * UserInput  * Parameters: NONE  * Prints    : Nothing  * Returns  : A number between 0 and 9  *------------------------------------------------------------*/ int UserInput(void){         int input;     input = MinCars;         printf("Enter the number of cars to load (0-9)\n");     do{         printf( CURSOR );         input = getchar();         while(getchar() != '\n');     }     while ( input < MinCars || input > MaxCars);         return input-ASCII; } /*------------------------------------------------------------  * Print Boat  * Parameters:  * numb = number of cars to load  * odd = is the number of cars odd  * Prints    : The boat using calls to PrintBow,PrintMid and PrintAft  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBoat(int numb, int odd){     PrintBow(numb , odd);     if ( odd == ODD ) PrintMid(numb-1);     else PrintMid(numb);     PrintStern(); } /*------------------------------------------------------------  * PrintBow  * Parameters:  * cars = number of cars to load  * odd = is that value odd  * Prints    : The bow of the boat checking on if a car should go upfront  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBow( int cars, int odd){        printf(BowTip);     printf(Bow1);     printf(Bow2);     printf(Bow3);     if (odd == ODD){                      printf(Bow_WCar);          }     else{                printf(Bow_WOCar);              }     printf(Bow4); } /*------------------------------------------------------------  * PrintMid  * Parameters:  * cars = the even number of cars to load (2,4,6,8)  * Prints    : The middle of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintMid ( int cars){         printf(Middle_First);     if ( cars < 6 ) printf(Middle_WOCar);     else printf(Middle_Wcar);     if ( cars > 0) printf(Middle_Wcar);     else printf(Middle_WOCar);     if ( cars > 2 && cars != 6) printf(Middle_Wcar);     else printf(Middle_WOCar);     if ( cars == 6 || cars == 8) printf(Middle_Wcar);     else printf(Middle_WOCar);     printf(Middle_Last);          } /*------------------------------------------------------------  * PrintStern  * Parameters: NONE  * Prints    : The stern of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintStern(void){     printf(Stern1);     printf(Stern2);     printf(Ramp);  }```
• 05-29-2010
quzah
You don't need 'ascii', you can just subtract 'MinCars'. You don't really need to pass 'odd' to your function, since you're checking it inside the function anyway:
Code:

```void printsomething( int num ) {     if( num % 2 )     {         ...do odd...     }     else     {         ...do even...     } }```
I mean, you're already checking the value of 'odd' inside every function anyway, so lose the argument, and just check 'num' itself.

Quzah.
• 05-29-2010
jimtuv
Ok got rid of the ASCII ,EVEN and ODD. I also renamed some of the variables to clarify what they were for. (numb >> cars) I love refactoring in NetBeans!! Makes renaming variables super simple.

This process reminds me very much of the creative writing process. Where you start with a draft and refine, get input and refine some more. It's a lot of fun! I am really starting to enjoy programing. The details are my down fall but I am sure with some time and lots of practice I will improve there as well.

Here is the revised code:

Code:

```/* Ferry boat loading program  *  * File:  main.c  * Author: James Tuvell  *  * Created on May 29, 2010, 9:24 AM  * This program is practice.  *  * In it the user will input a number up to 9  * and the program will determine whether it is  * even or odd number and print out a boat loading  * layout that keeps the boat trimmed  */ #include <stdio.h> #include <stdlib.h> /*  *Define Constants  */ #define MinCars '0' #define MaxCars '9' #define CURSOR ">>" // Define the boat #define BowTip      "      ^      \n" #define Bow1        "    / \\    \n" #define Bow2        "    /  \\    \n" #define Bow3        "  /    \\  \n" #define Bow_WCar    "  /  x  \\  \n" #define Bow_WOCar    "  /      \\  \n" #define Bow4        " /        \\ \n" #define Middle_First " |  ***  |  \n" #define Middle_Wcar  " | x | | x |  \n" #define Middle_WOCar " |  | |  |  \n" #define Middle_Last  " |  ***  |  \n" #define Stern1      " \\        /  \n" #define Stern2      "  \\_______/  \n" #define Ramp        "    |  |  " /*  * Function templates  */ void Print_Instructions(void); int UserInput(void); void PrintBoat(int numb); void PrintBow( int cars); void PrintMid( int cars); void PrintStern(void); /*------------------------------------------------------------  * main  * Parameters: NONE  * Gets      : UserInput  * Prints    : Boat trimming layout  * Returns  : Nothing  *------------------------------------------------------------*/ int main(void) {     int cars;     Print_Instructions();     cars = UserInput();         PrintBoat(cars);     return 0; } /*------------------------------------------------------------  * Print Instructions  * Parameters: NONE  * Prints    : The Instructions  * Returns  : Nothing  *------------------------------------------------------------*/ void Print_Instructions(void){       printf ("\nThis is the automatic boat loader\n");     printf ("Just input the number of cars to load\n");     printf ("and I will print out a loading chart\n");     printf ("for you to use to keep the boat trimmed\n");    } /*------------------------------------------------------------  * UserInput  * Parameters: NONE  * Prints    : Nothing  * Returns  : A number between 0 and 9  *------------------------------------------------------------*/ int UserInput(void){         int input;     input = MinCars;         printf("Enter the number of cars to load (0-9)\n");     do{         printf(CURSOR);         input = getchar();         while(getchar() != '\n');     }     while ( input < MinCars || input > MaxCars);         return input-MinCars; } /*------------------------------------------------------------  * Print Boat  * Parameters:  * cars = number of cars to load  * Prints    : The boat using calls to PrintBow,PrintMid and PrintAft  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBoat(int cars){     PrintBow(cars);     if ( cars % 2 ) PrintMid(cars-1);     else PrintMid(cars);     PrintStern(); } /*------------------------------------------------------------  * PrintBow  * Parameters:  * cars = number of cars to load  * Prints    : The bow of the boat checking on if a car should go upfront  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintBow(int cars){        printf(BowTip);     printf(Bow1);     printf(Bow2);     printf(Bow3);     if (cars % 2){         printf(Bow_WCar);          }     else{                printf(Bow_WOCar);              }     printf(Bow4); } /*------------------------------------------------------------  * PrintMid  * Parameters:  * cars = the even number of cars to load (2,4,6,8)  * Prints    : The middle of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintMid(int cars){         printf(Middle_First);     if ( cars < 6 ) printf(Middle_WOCar);     else printf(Middle_Wcar);     if ( cars > 0) printf(Middle_Wcar);     else printf(Middle_WOCar);     if ( cars > 2 && cars != 6) printf(Middle_Wcar);     else printf(Middle_WOCar);     if ( cars == 6 || cars == 8) printf(Middle_Wcar);     else printf(Middle_WOCar);     printf(Middle_Last);      } /*------------------------------------------------------------  * PrintStern  * Parameters: NONE  * Prints    : The stern of the boat  * Returns  : Nothing  *------------------------------------------------------------*/ void PrintStern(void){     printf(Stern1);     printf(Stern2);     printf(Ramp); }```