PDA

View Full Version : Can someone help with my C program please




Jennygrrl
Feb 28, 2007, 02:22 AM
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

#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
Feb 28, 2007, 02:42 AM
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

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




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
Feb 28, 2007, 02:45 AM
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
Feb 28, 2007, 02:54 AM
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
Feb 28, 2007, 03:10 AM
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 :(

macman2790
Feb 28, 2007, 03:17 AM
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 :(

i'll take a look tomorrow, i kinda need some sleep lol.

iMeowbot
Feb 28, 2007, 04:05 AM
Okay, it's cheesy diagram time! Here we have three balls of different sizes, and a handy dandy sorting ramp.
69245
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
Feb 28, 2007, 04:11 AM
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
Feb 28, 2007, 04:13 AM
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:-


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
Feb 28, 2007, 04:31 AM
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:-


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
Feb 28, 2007, 05:55 AM
A couple more things, you're printf's should end with \n and you need a printf for the $500 fine.

janey
Feb 28, 2007, 06:26 AM
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...

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
Feb 28, 2007, 06:51 AM
there's also a boundary condition that's being ignored...


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:



if (carspeed <= SPEED_LIMIT) {

}
else if (carspeed <= SPEED_LIMIT2) {
}
else if (carspeed <= SPEED_LIMIT3) {
}
else {
fine = 500;}

iMeowbot
Feb 28, 2007, 06:56 AM
A short and sweet way:

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
Feb 28, 2007, 07:49 AM
How about this to be a little bit different:-


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



b e n

janey
Feb 28, 2007, 07:27 PM
While the above code is correct, there are several tests which are unnecessary, you can simplify further to:
It not being 4am right now i just realized that :D

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

Jennygrrl
Feb 28, 2007, 08:34 PM
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%:

#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
Mar 1, 2007, 08:30 AM
It not being 4am right now i just realized that :D

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

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
Mar 1, 2007, 10:02 AM
Gotta love the ternary operator...


#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

grapes911
Mar 1, 2007, 10:25 AM
Gotta love the ternary operator...

I'm usually a huge fan. But that code is barely readable.