help with Arrays

help with Arrays

Post by shdwcat on Sun Dec 09, 2012 5:57 pm
([msg=71618]see help with Arrays[/msg])

We are learning arrays in class and the teacher says it makes programming easier, I just dont see it or I might be missing something perhaps.

The teacher asked us to make a program asking the following:
Your program this week is:


1. Design a program that will keep track of a users golf score and let him/her know how far over or under par they are.

2. The program should accept the following input from the user:
a. Golfers Name
b. Amount of strokes the Golfer just had on the current hole, for 18 holes.

3. The program should prompt the golfer for the number of strokes on the just finished hole( EX. How many strokes did you have on hole 4?)

4. The program should keep track of the golfer’s total strokes and compare it to the current par. The program should let the golfer know how he/she is doing after data is entered for each hole (You are currently 3 over par).

Par refers to the number of strokes that it should take you to get the golf ball in the hole. If a hole is listed as par 3 and it takes you 4 strokes to get the ball in the hole, then you are one over par. If you get the ball in the hole in 2 strokes, then you are one under par. If the total par for 18 holes is 54 and you had a total of 60 strokes to complete the course, then you are 6 over par.


5. The following holes are par 3:
a. 1, 4 ,5, 7, 17

6. The following holes are par 4:
a. 2, 3, 6, 11, 12, 15

7. The following holes are par 5:
a. 8, 9, 10, 13, 14, 16

8. The following hole is par 8:
a. 18

9. When the golfer finishes all 18 holes the scorecard should be output to the screen. The scorecard should show the hole number, par for the hole, and the golfers strokes for the hole. It should also show the total par, and total strokes.

10. A message should be displayed based on the golfers score compared to par. Different messages should be displayed based on over par, under par, or exactly par.
I have so far:
Code: Select all
#include <iostream>

using namespace std;

int main()
{
    string firstname, lastname;
    int hol1, hol2, hol3, hol4, hol5, hol6, hol7, hol8, hol9, hol10;
    int par;
   
    int holes[18] = {3, 4, 4, 3, 4, 4, 3, 5, 5, 5, 4, 4, 5, 5, 4, 5, 3, 8};
   
   
   
    cout << "Enter golfers name: ";
    cin >> firstname >> lastname;
   
    cout <<"Your name is " << firstname << " " << lastname << endl;
   
                 //hole1
    cout << "Enter your strokes for hole 1: ";
    cin >> hol1;
   
    par = hol1 - holes[0];
   
    if (hol1 < holes[0])
    {
        cout << "you are " << par <<  " under par, Great Job!\n";
        }
        else
        cout << "you are " << par << " over par.\n";
       
                // Hole 2
    cout << "Enter your strokes for hole 2: ";
    cin >> hol2;
   
    par = hol2 - holes[1];
   
    if (hol2 < holes[1])
    {
        cout << "you are " << par <<  " under par, Great Job!\n";
        }
        else
        cout << "you are " << par << " over par.\n";
       
                //hole3
        cout << "Enter your strokes for hole 3: ";
    cin >> hol3;
   
    par = hol3 - holes[2];
   
    if (hol3 < holes[2])
    {
        cout << "you are " << par <<  " under par, Great Job!\n";
        }
        else
        cout << "you are " << par << " over par.\n";
       
                //hole4
    cout << "Enter your strokes for hole 4: ";
    cin >> hol4;
   
    par = hol4 - holes[3];
   
    if (hol4 < holes[3])
    {
        cout << "you are " << par <<  " under par, Great Job!\n";
        }
        else
        cout << "you are " << par << " over par.\n";
       
                //hole5
    cout << "Enter your strokes for hole 5: ";
    cin >> hol5;
   
    par = hol5 - holes[4];
   
    if (hol5 < holes[4])
    {
        cout << "you are " << par <<  " under par, Great Job!\n";
        }
        else
        cout << "you are " << par << " over par.\n";
       
                //hole6
    cout << "Enter your strokes for hole 6: ";
    cin >> hol6;
   
    par = hol6 - holes[5];
   
    if (hol6 < holes[5])
    {
        cout << "you are " << par <<  " under par, Great Job!\n";
        }
        else
        cout << "you are " << par << " over par.\n";
   
    system ("pause");
    return 0;
}


Is there a way to simplify this instead of me doing the 18 holes how I have started?
shdwcat
New User
New User
 
Posts: 27
Joined: Sun Nov 11, 2012 6:48 pm
Location: FL
Blog: View Blog (0)


Re: help with Arrays

Post by fashizzlepop on Sun Dec 09, 2012 6:01 pm
([msg=71619]see Re: help with Arrays[/msg])

Yes, a for loop.
Code: Select all
for i = 0 to 17
  hole[i]... do stuff.
  ++i
endFor



Edit: to further elaborate, I would suggest having two arrays of ints. One that holds the par for each hole (i being the hole# -1) and one that holds the score of the golfer.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: help with Arrays

Post by shdwcat on Sun Dec 09, 2012 6:06 pm
([msg=71620]see Re: help with Arrays[/msg])

ok i will give that a try, i was thinking there had to be another way then the crap I was doing, thx
shdwcat
New User
New User
 
Posts: 27
Joined: Sun Nov 11, 2012 6:48 pm
Location: FL
Blog: View Blog (0)


Re: help with Arrays

Post by fashizzlepop on Sun Dec 09, 2012 6:08 pm
([msg=71621]see Re: help with Arrays[/msg])

A general rule of thumb is if you ever use code more than once, put it in a function. In this case,a loop works pretty good.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: help with Arrays

Post by shdwcat on Sun Dec 09, 2012 6:48 pm
([msg=71622]see Re: help with Arrays[/msg])

Does this look a little better?
Code: Select all
#include <iostream>

using namespace std;

int main()
{
    string first, last;
    int underpar, overpar;
    int par, hole, swings, totpar;
    int holes[18] = {3, 4, 4, 3, 4, 4, 3, 5, 5, 5, 4, 4, 5, 5, 4, 5, 3, 8}; // par for the 18 holes
    int sum = 0;
   
    cout << "Please enter Golfers name: ";
    cin >> first >> last;
   
   
    for (hole = 0; hole < 18; hole++)
    {
       
        cout << "enter number of swings for hole (1-18): ";
        cin >> holes[hole];
        sum = sum + holes[hole];
        }
       
       
        swings = sum;
   
       
        cout << "Golfer " << first << last << endl;
        cout << "Had " << swings << " swings and number of total swings for par is 78.\n";
       
        if (swings < 78)
        {
                   underpar = 78 - swings;
                   cout << "You are " << underpar << " under par\n";
                   }
        if (swings >= 78)
        {
                   overpar = swings - 78;
                   cout << "You are " << overpar << " over par, Great job!\n";
                   }
                   
       
   
   
   
    system("pause");
    return 0;
}


only thing is I believe he wanted it to say their par after every swing
EX. how many swings for this hole 3 ( and its a par 4) it should tell him he is 1 under par.

-- Sun Dec 09, 2012 6:52 pm --

shdwcat wrote:1. Design a program that will keep track of a users golf score and let him/her know how far over or under par they are.

2. The program should accept the following input from the user:
a. Golfers Name
b. Amount of strokes the Golfer just had on the current hole, for 18 holes.

3. The program should prompt the golfer for the number of strokes on the just finished hole( EX. How many strokes did you have on hole 4?)

4. The program should keep track of the golfer’s total strokes and compare it to the current par. The program should let the golfer know how he/she is doing after data is entered for each hole (You are currently 3 over par).



This is what the teacher wants, why I was confused about the arrays and how to ask for a number on each hole and output the par he is under or over par
shdwcat
New User
New User
 
Posts: 27
Joined: Sun Nov 11, 2012 6:48 pm
Location: FL
Blog: View Blog (0)


Re: help with Arrays

Post by fashizzlepop on Mon Dec 10, 2012 10:05 am
([msg=71628]see Re: help with Arrays[/msg])

It looks like you should be keeping a second array holding the golfer's score. That way you can always calculate how below or over par he is to that point. I'd suggest making a function that takes 2 arrays of ints and a number of holes. Then you can call the function with the two arrays and it calculates how far over or below he is up to that point.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2303
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests

cron