4 Replies - 723 Views - Last Post: 10 February 2013 - 03:39 PM Rate Topic: -----

#1 xrlk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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):
Posted Image


Posted Image

#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.
Posted Image
Posted Image

Is This A Good Question/Topic? 0
  • +

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

#2 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,315
  • 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.
Was This Post Helpful? 1
  • +
  • -

#3 xrlk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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];
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#4 xrlk  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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];
            }
        }
    }


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3664
  • View blog
  • Posts: 11,492
  • 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; 
    :
}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1