Thread: Program help

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    6

    Question Program help

    Hi. I am a beginner at C Programming. I'm really in the dark about a lot of stuff in this department, so I thought maybe I could get some help from on here.

    I have an assignment I have to do for class, and it's just mind-boggling to my little brain So, hopefully someone can give me some advice and assistance.

    Okay, here's what I have to do:

    Objective
    The objective of this problem is to develop a Fine Calculator program that will:
    Input a driver’s speed as measured by traffic patrol.
    Determine if a speeding ticket is to be issued and if so, determine the amount of the fine.
    Assume fine amount 0 when there is no speed violation.
    Output the fine.
    Background
    For this lab, make the following assumptions:
    The fine for speeding in a 45 MPH zone is $10 for every mile per hour over the speed limit for speeds from 46 to 55 MPH.
    It is $15 for every additional mile per hour between 56 and 65 MPH.
    It is $20 for every additional mile per hour over 65 MPH.
    For example, the fine for driving 57 MPH is $100 for the fast 10 MPH plus $30 for the 2 MPH in excess of 55 MPH, for a total of $130.


    Now, I know I need to first have a scan function that will scan for the user's input. And should have an "int" for the speed? I was wondering how I would do this for the if statement...since I need the if statement to say "if it's in between two numbers" would it be like:

    if (speed>45 && speed<55); ?

    and any ideas on what I could do to make it so it would add 10 dollars for each mile gone over the 45.


    Any help would be greatly appreciated.
    I just really want to learn how to do this.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by kazoo View Post
    Hi. I am a beginner at C Programming. I'm really in the dark about a lot of stuff in this department, so I thought maybe I could get some help from on here.

    I have an assignment I have to do for class, and it's just mind-boggling to my little brain So, hopefully someone can give me some advice and assistance.

    Okay, here's what I have to do:

    Objective
    The objective of this problem is to develop a Fine Calculator program that will:
    Input a driver’s speed as measured by traffic patrol.
    Determine if a speeding ticket is to be issued and if so, determine the amount of the fine.
    Assume fine amount 0 when there is no speed violation.
    Output the fine.
    Background
    For this lab, make the following assumptions:
    The fine for speeding in a 45 MPH zone is $10 for every mile per hour over the speed limit for speeds from 46 to 55 MPH.
    It is $15 for every additional mile per hour between 56 and 65 MPH.
    It is $20 for every additional mile per hour over 65 MPH.
    For example, the fine for driving 57 MPH is $100 for the fast 10 MPH plus $30 for the 2 MPH in excess of 55 MPH, for a total of $130.


    Now, I know I need to first have a scan function that will scan for the user's input. And should have an "int" for the speed? I was wondering how I would do this for the if statement...since I need the if statement to say "if it's in between two numbers" would it be like:

    if (speed>45 && speed<55); ?

    and any ideas on what I could do to make it so it would add 10 dollars for each mile gone over the 45.


    Any help would be greatly appreciated.
    I just really want to learn how to do this.
    You don't need to "write your own scan function", you can just use scanf.

    As for the conditions, yes the if you posted is correct. You will probably need several if-else statements to cover all the cases.

    Get started and post here along the way if you get confused. This is an easy assignment even for a beginner and with some help you should be able to get this done in an hour.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Work out the logic on paper. Make a shell of a program that lets you input some speed, and compare it to whatever the speed is. Get started, show us where you're stuck.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    6

    Post .

    This is what I've got so far...

    Code:
    */
    @author:
    @date/version:20100512.01
    @title:Fine Calculator
    */
    
    #include <stdio.h>
    
    int main (void){
    
    //declarations
    int speed;
    int fine;
    int46;
    int56;
    int65;
    //give the user information on what to do
    printf("Please input the speed of the vehicle:\n");
    //get input
    
    scanf("%d",&speed);
    if(speed<46 && speed>56){
    fine=(
    }
    return 0;
    }

    any suggestions on the math for the fine=(

    I think if I could figure that out, I could get the rest done

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Remove this:

    int46;
    int56;
    int65;

    It is nonsensical.

    Also notice that your if condition is wrong. A number (speed) cannot be less than 46 and greater than 56 at the same time. What you want is slightly different.

    Just continue developing your if else framework. It should look something like:
    Code:
    if(speed>=46 && speed<56){
      /* calculate fine for this speed */
    }else{
      if(speed >= 56 && speed < 65){
       /* calculate fine for this speed */
      }else{
         /* speed is greater than 65 */
          /* calculate fine for this speed */
       }
    }
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    For the math of the fine just compute the difference between the lower threshold of that category and the current speed and store the result in another variable. That will tell you HOW MANY MPH the driver was speeding, and then you can just multiply that number by the penalty/per mph given in the problem and that's your dynamic fine. Make sure you add that to your static fine for that interval.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    claudiu
    >else
    Then this will execute not only for speed greater/equal to 65, but also for one that is lower than 46. One more if needed I suppose.
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  8. #8
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by GL.Sam View Post
    claudiu
    >else
    Then this will execute not only for speed greater/equal to 65, but also for one that is lower than 46. One more if needed I suppose.
    Yes, indeed, good catch Sam.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  9. #9
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Also it could overall be rewritten in less obvious, but much more compact way:

    Code:
    if (speed >= 65)
      /* calculate fine for this speed */
    else if (speed >= 56)
      /* we already know that value is lesser */
    else if(speed >= 46)
      /* ditto */
    else
      /*speed is lesser than 46 */
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  10. #10
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    i did something like this but i think its bad programing maybe there is another way of doing it still didnt finish it
    Code:
     
    
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    
    main(){
           
           int speed;
           int add;
           int t;
           printf("\n enter the speed  ");
           scanf("\n%i",&speed);
           
           if(speed<=45)
           {
                     
           }
           else if (speed>=46&&speed<=55)
           {
            if(speed==46)
           {
            add=10;
            printf("$%i",add);
            }
           if(speed==47)        
           {
            t=20;
           printf("$%i",t);
           }
           if(speed==48)        
           {
            t=30;
           printf("$%i",t);
           }
           if(speed==49)       
           {
            t=40;
           printf("$%i",t);
           }
           
            if(speed==50)       
           {
            t=50;
           printf("$%i",t);
           }
            if(speed==51)       
           {
            t=60;
           printf("$%i",t);
           }
            if(speed==52)       
           {
            t=70;
           printf("$%i",t);
           }
            if(speed==53)       
           {
            t=80;
           printf("$%i",t);
           }
            if(speed==54)       
           {
            t=90;
           printf("$%i",t);
           }
            if(speed==55)       
           {
            t=100;
           printf("total speed$%i",t);
           }
           
           
           }
    else if (speed>=56&&speed<=65)
     {
         {
               if(speed==56)
               {
               add=115;
               printf("$%i",add);
               }
               if(speed==57)        
               {
               add=130;
               printf("$%i",add);
               }
               if(speed==58)        
               {
               add=145;
               printf("$%i",add);
               }
               if(speed==59)       
               {
               add=160;
               printf("$%i",add);
               }
               if(speed==60)       
               {
               add=175;
               printf("$%i",add);
               }
               if(speed==61)       
               {
               add=190;
               printf("$%i",add);
               }
               if(speed==62)       
               {
               add=205;
               printf("$%i",add);
               }
               if(speed==63)       
               {
               add=220;
               printf("$%i",add);
               }
               if(speed==64)       
               {
               add=235;
               printf("$%i",add);
               }
               if(speed==65)       
               {
                add=250;
               printf("total speed$%i",add);
               }
           
           
               }               
           
           }
           else if (speed>=66)
           {
                          
           
           }
           getch();
                        
    }

  11. #11
    Registered User GL.Sam's Avatar
    Join Date
    Aug 2009
    Posts
    88
    Oh. My. God.

    Will you ever actually think before posting?
    The only good is knowledge and the only evil is ignorance.
    ~Socrates

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( speed -= 45, ticket = x = 0; speed - x > 0; x++, ticket += x < 10 ? 10 : x < 20 ? 15 : 20 );
    That looks about right.


    Quzah.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    i was just trying quzzah can you explain what your code does
    Last edited by mouse666666; 05-12-2010 at 09:55 PM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's fine to try. There are many ways to do just about everything in C.


    Quzah.
    Hope is the first step on the road to disappointment.

  15. #15
    Registered User
    Join Date
    Feb 2010
    Posts
    96
    RE: quazah can you explain what your code does. i try to follow your code but iam not sure

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM