I am having trouble with my code and I don't know where to go from

Golf ACSL Help

Posted 14 December 2009 - 04:26 PM

PROBLEM: In the sport of golf, scoring is based upon what a normal score should be for a particular hole on a course. That normal score called “par” is based upon the length of that hole measured from the tee, the start point, to the green, the end point. For the vast majority of golf courses and for this problem par values will be either 3, 4 or 5. Golf scores can be reported in three ways. The first way is to report cumulative score. That is, the player scored a 68 after playing the standard 18 holes. The second way is to report the cumulative score in relation to par. If par for the course ( the sum of the par values of all the holes) is 72, then the score reported as 68 would be 4 under par. A score of 75 would be 3 over par. A score of 72 is reported as par. The third way is by holes won. A player wins a hole when his score is lower than his opponent’s score.

INPUT: There will be 9 input lines. Each line will contain 3 positive integers. The first integer will give the par value of each of the nine holes. The second integer will give the score for player A on the same nine holes. The third integer will give the score for player B on the those nine holes.

OUTPUT: There will be five outputs. Print the following:

1. The cumulative score for each player with the better score (the smaller number) first.
2. The score in relation to par for the better score.
3. The score in relation to par for the other player.
4. The number of holes won by the player with the better score.
5. The sum of the scores on a the hole that is the highest for all the holes played.
______________________________________________________________
INPUT:
1. 3,2,3
2. 4,5,5
3. 5,6,6
4. 4,3,4
5. 4,3,4
6. 4,4,5
7. 5,5,6
8. 3,3,3
9. 4,4,5

Re: Golf ACSL Help

Posted 14 December 2009 - 09:46 PM

A, B, C, D p, q, r, s what are these ?

Steve McConnell
Code Complete 2nd edition
Microsoft Press

Creating Short Names That Are Readable
The desire to use short variable names is in some ways a remnant of an earlier age of computing. Older languages like assembler, generic Basic, and Fortran limited variable names to 2–8 characters and forced programmers to create short names. Early computing was more closely linked to mathematics and its use of terms like i, j, and k as the variables in summations and other equations. In modern languages like C++, Java, and Visual Basic, you can create names of virtually any length; you have almost no reason to shorten meaningful names.

If circumstances do require you to create short names, note that some methods of shortening names are better than others. You can create good short variable names by eliminating needless words, using short synonyms, and using any of several abbreviation strategies. It's a good idea to be familiar with multiple techniques for abbreviating because no single technique works well in all cases.

General Abbreviation Guidelines
Here are several guidelines for creating abbreviations. Some of them contradict others, so don't try to use them all at the same time.

- Use standard abbreviations (the ones in common use, which are listed in a dictionary).

- Truncate consistently after the first, second, or third (whichever is appropriate) letter of each word.

- Keep the first and last letters of each word.

- Use every significant word in the name, up to a maximum of three words.

- Remove useless suffixes—ing, ed, and so on.

- Keep the most noticeable sound in each syllable.

Be sure not to change the meaning of the variable.

Iterate through these techniques until you abbreviate each variable name to between 8 to 20 characters or the number of characters to which your language limits variable names.

Phonetic Abbreviations
Some people advocate creating abbreviations based on the sound of the words rather than their spelling. Thus skating becomes sk8ing, highlight becomes hilite, before becomes b4, execute becomes xqt, and so on. This seems too much like asking people to figure out personalized license plates to me, and I don't recommend it. As an exercise, figure out what these names mean:

ILV2SK8
XMEQWK
S2DTM8O
NXTC
TRMN8R

Comments on Abbreviations
You can fall into several traps when creating abbreviations. Here are some rules for avoiding pitfalls:

Don't abbreviate by removing one character from a word Typing one character is little extra work, and the one-character savings hardly justifies the loss in readability. It's like the calendars that have "Jun" and "Jul." You have to be in a big hurry to spell June as "Jun." With most one-letter deletions, it's hard to remember whether you removed the character. Either remove more than one character or spell out the word.

Abbreviate consistently Always use the same abbreviation. For example, use Num everywhere or No everywhere, but don't use both. Similarly, don't abbreviate a word in some names and not in others. For instance, don't use the full word Number in some places and the abbreviation Num in others.

Create names that you can pronounce Use xPos rather than xPstn and needsComp rather than ndsCmptg. Apply the telephone test—if you can't read your code to someone over the phone, rename your variables to be more distinctive (Kernighan and Plauger 1978).

Avoid combinations that result in misreading or mispronunciation To refer to the end of B, favor ENDB over BEND. If you use a good separation technique, you won't need this guideline since B-END, BEnd, or b_end won't be mispronounced.

Use a thesaurus to resolve naming collisions One problem in creating short names is naming collisions—names that abbreviate to the same thing. For example, if you're limited to three characters and you need to use fired and full revenue disbursal in the same area of a program, you might inadvertently abbreviate both to frd.

One easy way to avoid naming collisions is to use a different word with the same meaning, so a thesaurus is handy. In this example, dismissed might be substituted for fired and complete revenue disbursal might be substituted for full revenue disbursal. The three-letter abbreviations become dsm and crd, eliminating the naming collision.

Document extremely short names with translation tables in the code In languages that allow only very short names, include a translation table to provide a reminder of the mnemonic content of the variables. Include the table as comments at the beginning of a block of code. Here's an example:

Fortran Example of a Good Translation Table
C *******************************************************************

C Translation Table

C

C Variable Meaning

C -------- -------

C XPOS x-Coordinate Position (in meters)

C YPOS Y-Coordinate Position (in meters)

C NDSCMP Needs Computing (=0 if no computation is needed;

C =1 if computation is needed)

C PTGTTL Point Grand Total

C PTVLMX Point Value Maximum

C PSCRMX Possible Score Maximum

C *****************************************************************

You might think that this technique is outdated, but as recently as mid-2003 I worked with a client that had hundreds of thousands of lines of code written in RPG that was subject to a 6-character–variable-name limitation. These issues still come up from time to time.

Document all abbreviations in a project-level "Standard Abbreviations" document Abbreviations in code create two general risks:

A reader of the code might not understand the abbreviation.

Other programmers might use multiple abbreviations to refer to the same word, which creates needless confusion.

To address both these potential problems, you can create a "Standard Abbreviations" document that captures all the coding abbreviations used on your project. The document can be a word processor document or a spreadsheet. On a very large project, it could be a database. The document is checked into version control and checked out anytime anyone creates a new abbreviation in the code. Entries in the document should be sorted by the full word, not the abbreviation.

This might seem like a lot of overhead, but aside from a small amount of startup overhead, it really just sets up a mechanism that helps the project use abbreviations effectively. It addresses the first of the two general risks described above by documenting all abbreviations in use. The fact that a programmer can't create a new abbreviation without the overhead of checking the Standard Abbreviations document out of version control, entering the abbreviation, and checking it back in is a good thing. It means that an abbreviation won't be created unless it's so common that it's worth the hassle of documenting it.

This approach addresses the second risk by reducing the likelihood that a programmer will create a redundant abbreviation. A programmer who wants to abbreviate something will check out the abbreviations document and enter the new abbreviation. If there is already an abbreviation for the word the programmer wants to abbreviate, the programmer will notice that and will then use the existing abbreviation instead of creating a new one.

The general issue illustrated by this guideline is the difference between write-time convenience and read-time convenience. This approach clearly creates a write-time inconvenience, but programmers over the lifetime of a system spend far more time reading code than writing code. This approach increases read-time convenience. By the time all the dust settles on a project, it might well also have improved write-time convenience.

Remember that names matter more to the reader of the code than to the writer Read code of your own that you haven't seen for at least six months and notice where you have to work to understand what the names mean. Resolve to change the practices that cause such confusion.

Re: Golf ACSL Help

Posted 14 December 2009 - 09:55 PM

pbl, on 14 Dec, 2009 - 08:46 PM, said:

A, B, C, D p, q, r, s what are these ?

Steve McConnell
Code Complete 2nd edition
Microsoft Press

Creating Short Names That Are Readable
The desire to use short variable names is in some ways a remnant of an earlier age of computing. Older languages like assembler, generic Basic, and Fortran limited variable names to 2–8 characters and forced programmers to create short names. Early computing was more closely linked to mathematics and its use of terms like i, j, and k as the variables in summations and other equations. In modern languages like C++, Java, and Visual Basic, you can create names of virtually any length; you have almost no reason to shorten meaningful names.

If circumstances do require you to create short names, note that some methods of shortening names are better than others. You can create good short variable names by eliminating needless words, using short synonyms, and using any of several abbreviation strategies. It's a good idea to be familiar with multiple techniques for abbreviating because no single technique works well in all cases.

General Abbreviation Guidelines
Here are several guidelines for creating abbreviations. Some of them contradict others, so don't try to use them all at the same time.

- Use standard abbreviations (the ones in common use, which are listed in a dictionary).

- Truncate consistently after the first, second, or third (whichever is appropriate) letter of each word.

- Keep the first and last letters of each word.

- Use every significant word in the name, up to a maximum of three words.

- Remove useless suffixes—ing, ed, and so on.

- Keep the most noticeable sound in each syllable.

Be sure not to change the meaning of the variable.

Iterate through these techniques until you abbreviate each variable name to between 8 to 20 characters or the number of characters to which your language limits variable names.

Phonetic Abbreviations
Some people advocate creating abbreviations based on the sound of the words rather than their spelling. Thus skating becomes sk8ing, highlight becomes hilite, before becomes b4, execute becomes xqt, and so on. This seems too much like asking people to figure out personalized license plates to me, and I don't recommend it. As an exercise, figure out what these names mean:

ILV2SK8
XMEQWK
S2DTM8O
NXTC
TRMN8R

Comments on Abbreviations
You can fall into several traps when creating abbreviations. Here are some rules for avoiding pitfalls:

Don't abbreviate by removing one character from a word Typing one character is little extra work, and the one-character savings hardly justifies the loss in readability. It's like the calendars that have "Jun" and "Jul." You have to be in a big hurry to spell June as "Jun." With most one-letter deletions, it's hard to remember whether you removed the character. Either remove more than one character or spell out the word.

Abbreviate consistently Always use the same abbreviation. For example, use Num everywhere or No everywhere, but don't use both. Similarly, don't abbreviate a word in some names and not in others. For instance, don't use the full word Number in some places and the abbreviation Num in others.

Create names that you can pronounce Use xPos rather than xPstn and needsComp rather than ndsCmptg. Apply the telephone test—if you can't read your code to someone over the phone, rename your variables to be more distinctive (Kernighan and Plauger 1978).

Avoid combinations that result in misreading or mispronunciation To refer to the end of B, favor ENDB over BEND. If you use a good separation technique, you won't need this guideline since B-END, BEnd, or b_end won't be mispronounced.

Use a thesaurus to resolve naming collisions One problem in creating short names is naming collisions—names that abbreviate to the same thing. For example, if you're limited to three characters and you need to use fired and full revenue disbursal in the same area of a program, you might inadvertently abbreviate both to frd.

One easy way to avoid naming collisions is to use a different word with the same meaning, so a thesaurus is handy. In this example, dismissed might be substituted for fired and complete revenue disbursal might be substituted for full revenue disbursal. The three-letter abbreviations become dsm and crd, eliminating the naming collision.

Document extremely short names with translation tables in the code In languages that allow only very short names, include a translation table to provide a reminder of the mnemonic content of the variables. Include the table as comments at the beginning of a block of code. Here's an example:

Fortran Example of a Good Translation Table
C *******************************************************************

C Translation Table

C

C Variable Meaning

C -------- -------

C XPOS x-Coordinate Position (in meters)

C YPOS Y-Coordinate Position (in meters)

C NDSCMP Needs Computing (=0 if no computation is needed;

C =1 if computation is needed)

C PTGTTL Point Grand Total

C PTVLMX Point Value Maximum

C PSCRMX Possible Score Maximum

C *****************************************************************

You might think that this technique is outdated, but as recently as mid-2003 I worked with a client that had hundreds of thousands of lines of code written in RPG that was subject to a 6-character–variable-name limitation. These issues still come up from time to time.

Document all abbreviations in a project-level "Standard Abbreviations" document Abbreviations in code create two general risks:

A reader of the code might not understand the abbreviation.

Other programmers might use multiple abbreviations to refer to the same word, which creates needless confusion.

To address both these potential problems, you can create a "Standard Abbreviations" document that captures all the coding abbreviations used on your project. The document can be a word processor document or a spreadsheet. On a very large project, it could be a database. The document is checked into version control and checked out anytime anyone creates a new abbreviation in the code. Entries in the document should be sorted by the full word, not the abbreviation.

This might seem like a lot of overhead, but aside from a small amount of startup overhead, it really just sets up a mechanism that helps the project use abbreviations effectively. It addresses the first of the two general risks described above by documenting all abbreviations in use. The fact that a programmer can't create a new abbreviation without the overhead of checking the Standard Abbreviations document out of version control, entering the abbreviation, and checking it back in is a good thing. It means that an abbreviation won't be created unless it's so common that it's worth the hassle of documenting it.

This approach addresses the second risk by reducing the likelihood that a programmer will create a redundant abbreviation. A programmer who wants to abbreviate something will check out the abbreviations document and enter the new abbreviation. If there is already an abbreviation for the word the programmer wants to abbreviate, the programmer will notice that and will then use the existing abbreviation instead of creating a new one.

The general issue illustrated by this guideline is the difference between write-time convenience and read-time convenience. This approach clearly creates a write-time inconvenience, but programmers over the lifetime of a system spend far more time reading code than writing code. This approach increases read-time convenience. By the time all the dust settles on a project, it might well also have improved write-time convenience.

Remember that names matter more to the reader of the code than to the writer Read code of your own that you haven't seen for at least six months and notice where you have to work to understand what the names mean. Resolve to change the practices that cause such confusion.

I appreciate everything that you have told me, but I don't know where this helps me. This is how my computer science teacher taught me how to code. I'm using Dr.Java and my code compiles but not everything works correctly. Can you tell me what is wrong with my code and why not all of my outputs are working correctly? Does my code make any kind of sense?

Re: Golf ACSL Help

Posted 14 December 2009 - 10:04 PM

10rbacon, on 14 Dec, 2009 - 08:55 PM, said:

I appreciate everything that you have told me, but I don't know where this helps me. This is how my computer science teacher taught me how to code. I'm using Dr.Java and my code compiles but not everything works correctly. Can you tell me what is wrong with my code and why not all of my outputs are working correctly? Does my code make any kind of sense?

Appreciate you appreciated took me a while to type it
but I won't just try to decipher your code using variable names like A, B, C, D, p, q, r, s
We scan and reply to 10 to 20 posts here everynight, the less you can do is to have meningfull variable names imagine everybody named their variables A, B, C, D ....

Re: Golf ACSL Help

Posted 14 December 2009 - 10:10 PM

pbl, on 14 Dec, 2009 - 09:04 PM, said:

10rbacon, on 14 Dec, 2009 - 08:55 PM, said:

I appreciate everything that you have told me, but I don't know where this helps me. This is how my computer science teacher taught me how to code. I'm using Dr.Java and my code compiles but not everything works correctly. Can you tell me what is wrong with my code and why not all of my outputs are working correctly? Does my code make any kind of sense?

Appreciate you appreciated took me a while to type it
but I won't just try to decipher your code using variable names like A, B, C, D, p, q, r, s
We scan and reply to 10 to 20 posts here everynight, the less you can do is to have meningfull variable names imagine everybody named their variables A, B, C, D ....

Dogstopper, on 14 Dec, 2009 - 08:56 PM, said:

YES! THE GURU DELIVERS AGAIN! Nice one pbl!

Thanks
I recommend this Book to everybody

Well I blame my teacher because this is how she has taught me and my class. I am only a teenager in high school and I have only been programming for less than a half of a year. Is there any way you can tell me what a better variable name would be for say A and one for p? Take pity on me please!

Re: Golf ACSL Help

Posted 14 December 2009 - 10:24 PM

10rbacon, on 14 Dec, 2009 - 09:10 PM, said:

Well I blame my teacher because this is how she has taught me and my class. I am only a teenager in high school and I have only been programming for less than a half of a year. Is there any way you can tell me what a better variable name would be for say A and one for p? Take pity on me please!

I guess the text I posted should provide you with nice hints
What are A, B, C, D ? Number of par for every hole ? Number of beers you are allowed to drink after that hole ?
What are p, q, r, s ? just iteratrors ? OK I won't be fancy but why using 4 of them ?