# Help with a programming challenge (input, functions, output)

Page 1 of 1

## 4 Replies - 865 Views - Last Post: 10 February 2013 - 03:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=311660&amp;s=b2783e1c4350f337d146488f8b73e71b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 xrlk

Reputation: 0
• Posts: 3
• Joined: 08-February 13

# Help with a programming challenge (input, functions, output)

Posted 08 February 2013 - 11:59 PM

This is practice work for a coding contest at the school, the code is pretty bad; I'm sure there are better methods to approaching the problem, and I'm sure I mucked something (or a lot of things) up along the way. I don't think I could explain it too well, but I have provided the question booklet.

And this is the output I get based on the code I provided (which I wrote):

```#include <iostream>

int speedCalc( int, int );
// a mess, the purpose of the table was to hold the threshhold information for determing fines
int fineTable[3][3]={1,20,100,21,30,270,31,31,500};

int main()
{
int inputLimit, inputPlaneSpeed;
std::cout <<"Enter the limit: ";
std::cin >> inputLimit;
std::cout <<"Enter the recorded speed of the car: ";
std::cin >> inputPlaneSpeed;
// Might have to clear input stream? idk

speedCalc( inputLimit, inputPlaneSpeed );

return 0;
}

int speedCalc( int speedLimit, int planeSpeed )
{
if ( planeSpeed < speedLimit )
{
std::cout <<"Congratulations, you are within the speed limit!";
std::cin.get();
}
else if ( planeSpeed > speedLimit )
{
if ((planeSpeed >= speedLimit+fineTable[1][1]) and (planeSpeed<= speedLimit+fineTable[1][2]))
{
std::cout <<"You are speeding and your fine is " <<fineTable[1][3];
}
}
}

```

The images don't appear to work on the original thread post, hopefully this does.

Is This A Good Question/Topic? 0

## Replies To: Help with a programming challenge (input, functions, output)

### #2 sepp2k

• D.I.C Lover

Reputation: 2277
• Posts: 3,507
• Joined: 21-June 11

## Re: Help with a programming challenge (input, functions, output)

Posted 09 February 2013 - 04:46 AM

Your code only produces output if you're below the speed limit or between 30 and 270 above the speed limit. In your sample input, you're neither, so you don't get any output.

### #3 xrlk

Reputation: 0
• Posts: 3
• Joined: 08-February 13

## Re: Help with a programming challenge (input, functions, output)

Posted 09 February 2013 - 11:19 AM

This is my revised code, as far as I have tried it works well. Since this is a coding comp. I am preparing for, hopefully something can point me in the direction of improving the code or certain conventions I am not following.

```#include <iostream>

int speedCalc( int, int );
// My opinion, better to have a table that is easy to adjust
int fineTable[3]={100,270,500};

int main()
{
int inputLimit, inputPlaneSpeed;
std::cout <<"Enter the limit: ";
std::cin >> inputLimit;
std::cout <<"Enter the recorded speed of the car: ";
std::cin >> inputPlaneSpeed;

speedCalc( inputLimit, inputPlaneSpeed );

return 0;
}

int speedCalc( int speedLimit, int planeSpeed )
{
if ( planeSpeed <= speedLimit )
{
std::cout <<"Congratulations, you are within the speed limit!";
}

else if ( planeSpeed > speedLimit )
{
if ( ( planeSpeed >= speedLimit+1) and ( planeSpeed<= speedLimit+20 ) )
{
std::cout <<"You are speeding and your fine is " <<fineTable[1];
}
else if ( ( planeSpeed >= speedLimit+21 ) and ( planeSpeed<= speedLimit+30 ) )
{
std::cout <<"You are speeding and your fine is " <<fineTable[2];
}
else if ( planeSpeed >= speedLimit+31 )
{
std::cout <<"You are speeding and your fine is " <<fineTable[3];
}
}
}

```

### #4 xrlk

Reputation: 0
• Posts: 3
• Joined: 08-February 13

## Re: Help with a programming challenge (input, functions, output)

Posted 09 February 2013 - 01:45 PM

This is my final draft of the code, if anybody has suggestions please feel free to share them, I need improvement.
```    #include <iostream>
#include <cmath>

int speedCalc(int, int );
unsigned int fineTable[3]={100,270,500};

int main()
{
int inputLimit, inputPlaneSpeed;
std::cout <<"Enter the limit: ";
std::cin >> inputLimit;
std::cout <<"Enter the recorded speed of the car: ";
std::cin >> inputPlaneSpeed;

speedCalc( inputLimit, inputPlaneSpeed );

return 0;
}

int speedCalc(int speedLimit, int planeSpeed )
{
if ( abs(planeSpeed) <= speedLimit )
{
std::cout <<"Congratulations, you are within the speed limit!";
}

else if ( abs(planeSpeed) > speedLimit )
{
if ( ( abs(planeSpeed) >= speedLimit+1) and ( planeSpeed<= speedLimit+20 ) )
{
std::cout <<"You are speeding and your fine is " <<fineTable[0];
}
else if ( ( abs(planeSpeed) >= speedLimit+21 ) and ( planeSpeed<= speedLimit+30 ) )
{
std::cout <<"You are speeding and your fine is " <<fineTable[1];
}
else if ( abs(planeSpeed) >= speedLimit+31 )
{
std::cout <<"You are speeding and your fine is " <<fineTable[2];
}
}
}

```

### #5 Skydiver

• Code herder

Reputation: 4379
• Posts: 14,145
• Joined: 05-May 12

## Re: Help with a programming challenge (input, functions, output)

Posted 10 February 2013 - 03:39 PM

What's the point of using a fine table, but not also use a limits table? You might as well just use magic numbers for the fines as well.

Here's my suggested solution:
```struct SpeedFine
{
int lowerLimit;
int fine;
};

SpeedFine g_speedFines[] =
{
{ 31, 500 },
{ 21, 270 },
{ 1, 100 },
{ 0, 0 }
};

int LookupFine(int speedOverLimit)
{
SpeedFine * pspeedfine = g_speedFines;

while(pspeedfine->lowerLimit > speedOverLimit &&
pspeedfine->lowerlimit != 0)
pspeedfine++;

return pspeedfine->fine;
}

int ComputeSpeedOverLimit(int carSpeed, int planeSpeed)
{
return carSpeed > planeSpeed ? carSpeed - planeSpeed : 0;
}

int main()
{
:
int speedOverLimit = ComputeSpeedOverLimit(carSpeed, planeSpeed);
int fine = LookupFine(speedOverLimit);
if (fine <= 0)
cout << "Slow poke. << endl;
else
cout << "Your fine is \$" << fine << ". Call your lawyer." << endl;
:
}

```