Can someone help with my C program please

Discussion in 'Mac Programming' started by Jennygrrl, Feb 28, 2007.

  1. macrumors newbie

    Joined:
    Feb 28, 2007
    Messages:
    8
    #1
    Hi. I'm really new to programming and my teacher said we need to write a c program that finds a fine for someone speeding.
    I am using xcode instead of the borland compiler on windows, so the teacher said he won't help me unless i use windows, but i hate it yuck!
    It compiles ok in xcode, but doesnt really work properly
    All i get is a fine for $80 even when i know it should be more than that.
    My code is at the bottom.

    Please help me! Thank you so much in advance all you smart boys out there!!! I appreciate it! :p
    - Jen

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    const int SPEED_LIMIT  = 60;
    const int SPEED_LIMIT2 = 80;
    const int SPEED_LIMIT3 = 150;
    int carspeed;
    int fine;
    
    printf("Please input speed: ");
    scanf("%d%*c", &carspeed);
    
    if	(carspeed >= SPEED_LIMIT)
    {
    	printf("Speeding");
    }
    else
    	if (carspeed <= SPEED_LIMIT2)
    	{
    		fine = 80;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    	else
    		if (carspeed <= SPEED_LIMIT3)
    		{
    			fine = 150;
    			printf("Speeding, your fine is: %d%*c", fine);
    		}
    		else
    			fine = 500;
    
    
    return (0);	
    }
     
  2. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Messages:
    720
    Location:
    Texas
    #2
    do you know how to use logical operators. logical operators: && (logical and) || (logical or), and ! ( logical not)
    i dont know much c but here's what i think you should start off with on your if statements, try to format regular variables with the second word capitalized, like carSpeed also, it helps it look easier to understand.
    Code:
    
    
    
    if (carSpeed > speedLimit) && (carSpeed < speedLimit2)  
         printf("speeding");     
         fine = 80;
    else if (carSpeed < speedLimit3){
          printf("speeding");          
          fine = 150;
    }
    else if (carSpeed > speedLimit3){
          printf("speeding");
    }
    else 
    printf("not speeding");
    
     
  3. macrumors newbie

    Joined:
    Feb 28, 2007
    Messages:
    8
    #3
    Thank you for your reply :)
    Yes i do know about logical operators.
    We were'nt supposed to use them its harder the way ive written it, but the teacher said to figure out how to do it without using && or || so i guess
    i cant write it the way you said.
    Thank you for your tips about the variable names i'll fix them up :)

    -Jen
     
  4. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Messages:
    720
    Location:
    Texas
    #4
    did it happen to compile and work correctly? Your welcome. I know what you mean about borland, i have to use jbuilder for a class and hate it, but its much better than xcode is with java, other than that xcode is pretty good for c, c++, and objective c.
     
  5. macrumors newbie

    Joined:
    Feb 28, 2007
    Messages:
    8
    #5
    Yes it compiles and works without crashing, but its a logic problem i think but i just cant figure out what because it looks ok to me i think :(
     
  6. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Messages:
    720
    Location:
    Texas
    #6
    i'll take a look tomorrow, i kinda need some sleep lol.
     
  7. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    Messages:
    8,647
    #7
    Okay, it's cheesy diagram time! Here we have three balls of different sizes, and a handy dandy sorting ramp.
    cheesy-diagram.jpg
    On the first try, we have the biggest hole near the top of the ramp. What happens when we drop the different sized balls from the top? They all fall down the first hole into the first trendy designer trash bin because any of them will fit.

    On the second try, we flip the ramp around so that the smaller holes come first. Now, the balls will roll down until a hole that fits comes along, so that they each end up in different trendy designer trash bins according to size.

    Traffic violations follow exactly the same principle, except that there aren't any balls, ramps or bins.
     
  8. Moderator emeritus

    Joined:
    Jun 25, 2002
    Messages:
    13,951
    Location:
    Gone but not forgotten.
    #8
    It looks to me as if you're saying:

    Mind you, it's late but that's how it looks to me.
     
  9. macrumors 6502a

    Joined:
    Sep 3, 2005
    Messages:
    686
    Location:
    Cramlington, UK
    #9
    Hi, just had a quick look and I think your problem is in the 1st if statement. It always evaluates to true if you are speeding so you never get into the main bit of code to calculate the fine. That bit of code is only entered if you are not speeding hence you always get $80.

    Try change your code to something like this:-

    Code:
    include <stdio.h>
    
    int main()
    {
    
    const int SPEED_LIMIT  = 60;
    const int SPEED_LIMIT2 = 80;
    const int SPEED_LIMIT3 = 150;
    int carspeed;
    int fine;
    
    printf("Please input speed: ");
    scanf("%d%*c", &carspeed);
    
    if	(carspeed < SPEED_LIMIT)
    {
    	printf("Not speeding!") ;
    }
    else
    	if (carspeed <= SPEED_LIMIT2)
    	{
    		fine = 80;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    	else
    	if (carspeed <= SPEED_LIMIT3)
    	{
    		fine = 150;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    	else
    		fine = 500;
    
    
    return (0);
    }
    
    
    Hope that works!

    b e n
     
  10. macrumors 6502a

    macman2790

    Joined:
    Sep 4, 2006
    Messages:
    720
    Location:
    Texas
    #10
    good job ben, thats what i meant to put when i did it, except i missed the if (carspeed < SPEED_LIMIT)
    {
    printf("Not speeding!") ;
    that should work, but like i said, i dont know much c.
     
  11. macrumors G4

    Eraserhead

    Joined:
    Nov 3, 2005
    Messages:
    10,275
    Location:
    UK
    #11
    A couple more things, you're printf's should end with \n and you need a printf for the $500 fine.
     
  12. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Messages:
    5,322
    Location:
    sunny los angeles
    #12
    Maybe it's just the way it's written but I don't really like the way it looks, and there's also a boundary condition that's being ignored...
    Code:
    include <stdio.h>
    
    int main()	{
    	const int SPEED_LIMIT  = 60;
    	const int SPEED_LIMIT2 = 80;
    	const int SPEED_LIMIT3 = 150;
    	int carspeed;
    	int fine;
    
    	printf("Please input speed: ");
    	scanf("%d%*c", &carspeed);
    
    	if	(carspeed <= SPEED_LIMIT)	{
    		// because technically if youre going at speed limit you're not speeding
    		printf("Not speeding!") ;
    	}
    	else if ( (carspeed > SPEED_LIMIT) && (carspeed <= SPEED_LIMIT2) ) 	{
    		fine = 80;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    	else if ( (carspeed > SPEED_LIMIT2) && (carspeed <= SPEED_LIMIT3) )	{
    		fine = 150;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    	else if (carspeed > SPEED_LIMIT3)	{
    		fine = 500;
    	}
    	else if (carspeed >= 250)	{
    		printf("i'm jealous of your car! is that a bugatti veyron you're driving? :P\n");
    	}
    
    	return (0);
    }
    
    edit: okay i didnt see the no &&/|| requirement :\
    Braindead at 4:30am...but i dont get that requirement...that only creates stupid amounts of nested loops..
     
  13. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Messages:
    6,257
    Location:
    Cork, Ireland.
    #13
    While the above code is correct, there are several tests which are unnecessary, you can simplify further to:

    Code:
    
    	if	(carspeed <= SPEED_LIMIT)	{
    
    	}
    	else if (carspeed <= SPEED_LIMIT2)  	{
    	}
    	else if (carspeed <= SPEED_LIMIT3)	{
    	}
    	else 	{
    		fine = 500;}
    
     
  14. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    Messages:
    8,647
    #14
    A short and sweet way:
    Code:
        if (carspeed > SPEED_LIMIT) {
            if (carspeed > SPEED_LIMIT3)
                fine = 500;
            else if (carspeed > SPEED_LIMIT2)
                fine = 150;
            else
                fine = 80;
            printf("Speeding, your fine is: %d\n", fine);
        }
        else
            printf("Carry on!\n");
    
     
  15. macrumors 6502a

    Joined:
    Sep 3, 2005
    Messages:
    686
    Location:
    Cramlington, UK
    #15
    How about this to be a little bit different:-

    Code:
    if ( speed > SPEED_LIMIT )
         fine = speed <= SPEED_LIMIT2 ? 80 : speed <= SPEED_LIMIT3 ? 150 : 500 ;
    

    b e n
     
  16. macrumors 603

    janey

    Joined:
    Dec 20, 2002
    Messages:
    5,322
    Location:
    sunny los angeles
    #16
    It not being 4am right now i just realized that :D

    bad coder when my brain is fried late in the night :eek:
     
  17. macrumors newbie

    Joined:
    Feb 28, 2007
    Messages:
    8
    #17
    Thank you!

    Thank you everyone for your input and help. Sorry I took a while to get back to everyone. There are too many good points in here.
    Ben, thank you so much! My mind was cloudy and i couldn't figure out the problem with my logic, but I used your basic code as well as what other people said and i came up with this, which works 100%:

    Code:
    #include <stdio.h>
    
    int main()
    {
    
    const int SPEED_LIMIT  = 60;
    const int SPEED_LIMIT2 = 80;
    const int SPEED_LIMIT3 = 150;
    int carSpeed;
    int fine;
    
    printf("Please input speed: ");
    scanf("%d%*c", &carSpeed);
    
    if	(carSpeed < SPEED_LIMIT)
    {
    	printf("Not speeding!") ;
    }
    else
    	if (carSpeed <= SPEED_LIMIT2)
    	{
    		fine = 80;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    	else
    	if (carSpeed <= SPEED_LIMIT3)
    	{
    		fine = 150;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    	else
    	if (carSpeed > SPEED_LIMIT3)
    	{
    		fine = 500;
    		printf("Speeding, your fine is: %d%*c", fine);
    	}
    
    return (0);
    }
    
    For the person who said using all there If statements is inefficient and i should just use && or ||, I agree with you, but I guess it was a better way to write the code.

    Anyways it works now perfectly! It compiles perfectly in Xcode and now I can stick to using the Mac for programming. For my next programs I'm going to use the syntax and will refer to the logic of this program so I can write better things. Yay!

    THANK YOU ALL YOU SMART PEOPLES SOOOO MUCH!!!!!!!! :p
    - Jen
     
  18. macrumors 603

    whooleytoo

    Joined:
    Aug 2, 2002
    Messages:
    6,257
    Location:
    Cork, Ireland.
    #18
    Hey, that's nothing.

    I remember being given a (vaguely) similar question when I interviewed for Apple some years ago. I took my time, thought the problem through to ensure I had the most efficient algorithm possible with no redundant tests, wrote the solution on the whiteboard and turned around.

    Silence.

    "Notice anything wrong with what you've written?" I'd committed the typical C error: I used "=" in lieu of "==" in every single if statement.

    Ah, what the hell. I laughed it off and got the job. :D
     
  19. macrumors 6502a

    Joined:
    Dec 4, 2006
    Messages:
    748
    Location:
    Katy, Texas
    #19
    Gotta love the ternary operator...

    Code:
    #include <stdio.h>
    
    int main()	{
    	const int SPEED_LIMIT  = 60;
    	const int SPEED_LIMIT2 = 80;
    	const int SPEED_LIMIT3 = 150;
    	
    	const int FINE1 = 80  ; 
    	const int FINE2 = 150 ; 
    	const int FINE3 = 500 ; 
    	
    	int carSpeed, fine = 0 ;
    				
    	printf("Please input speed: ");
    	scanf("%d%*c", &carSpeed);
    	
    	fine = carSpeed <= SPEED_LIMIT ? 0 : carSpeed <= SPEED_LIMIT2 ? FINE1 : carSpeed <= SPEED_LIMIT3 ? FINE2 : FINE3 ;
    	
    	if (fine==0) printf("There is no speeding fine.") ; 
    	else         printf("Your fine is $%d", fine) ; 
    	exit(0) ; 
    	}
    
    Todd
     
  20. Demi-God (Moderator emeritus)

    grapes911

    Joined:
    Jul 28, 2003
    Messages:
    6,946
    Location:
    Citizens Bank Park
    #20
    I'm usually a huge fan. But that code is barely readable.
     

Share This Page