Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Jennygrrl

macrumors newbie
Original poster
Feb 28, 2007
8
0
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);
}
 

macman2790

macrumors 6502a
Sep 4, 2006
716
1
Texas
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);
}
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");
 

Jennygrrl

macrumors newbie
Original poster
Feb 28, 2007
8
0
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
 

macman2790

macrumors 6502a
Sep 4, 2006
716
1
Texas
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.
 

Jennygrrl

macrumors newbie
Original poster
Feb 28, 2007
8
0
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.

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 :(
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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.
 

bousozoku

Moderator emeritus
Jun 25, 2002
16,120
2,397
Lard
It looks to me as if you're saying:

If the car's speed is greater than or equal to 60

"speeding"

otherwise (the speed is less than 60)

if the car's speed is less than or equal to 80 (it will meet both if it's less than 60)

fine = 80

otherwise

if the car's speed is less than or equal to 150 (less than 60 again)

fine = 150

otherwise (it will have to be going less than 60 and more than 150 at the same time. is that possible?)

fine = 500

Mind you, it's late but that's how it looks to me.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

macman2790

macrumors 6502a
Sep 4, 2006
716
1
Texas
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
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.
 

Eraserhead

macrumors G4
Nov 3, 2005
10,434
12,250
UK
A couple more things, you're printf's should end with \n and you need a printf for the $500 fine.
 

janey

macrumors 603
Dec 20, 2002
5,316
0
sunny los angeles
Code:
include <stdio.h>...
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..
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
there's also a boundary condition that's being ignored...
Code:
	if	(carspeed <= SPEED_LIMIT)	{

	}
	else if ( (carspeed > SPEED_LIMIT) && (carspeed <= SPEED_LIMIT2) ) 	{
	}
	else if ( (carspeed > SPEED_LIMIT2) && (carspeed <= SPEED_LIMIT3) )	{
	}
	else if (carspeed > SPEED_LIMIT3)	{
}

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;}
 

iMeowbot

macrumors G3
Aug 30, 2003
8,634
0
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");
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
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
 

Jennygrrl

macrumors newbie
Original poster
Feb 28, 2007
8
0
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
 

whooleytoo

macrumors 604
Aug 2, 2002
6,607
716
Cork, Ireland.
It not being 4am right now i just realized that :D

bad coder when my brain is fried late in the night :eek:

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
 

toddburch

macrumors 6502a
Dec 4, 2006
748
0
Katy, Texas
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.