1) Create an algorithm for a program that simulates a police radar gun. The algo
ID: 3699224 • Letter: 1
Question
1) Create an algorithm for a program that simulates a police radar gun. The algorithm should read an automobile speed (in km/h) and print the message “Speeding” if the speed exceeds 60kmh. The algorithm should then also calculate the appropriate fine. If the speed is 5 km/hr or less over this limit, only issue a warning. $80 for more than 5 and no more than 10 kmh above the limit, $150 for more than 10 but no more than 20kmh above the limit, $500 for more than 20kmh above the limit. You should give some thought as to the type of data you are working with before beginning to designed your solution.
2) Implement the program from question 1 into a C program. When developing your program pay particular attention to the use of constant values in your program. Identify what values for this problem should be constants and define them using the const keyword. Ideally you should find that you have very few actual numbers (called “literal values”) in your program apart from when defining constants. (The obvious exception to this is the use of zero to initialise variables etc.)
Run the program with your test data and fill in the remainder of the test table.
Having real difficulty, send help
Explanation / Answer
1) algorithm:
Step 1: Start
Step 2: Declare variables speed as normal integer variable
Step 3: Decalre FINE_FOR_65,FINE_FOR_70 and FINE_FOR_80 as const variables of type int and assign fine as 80, 150 and 500 dollars respectively
Step 4: Read value of speed in kmph.
Step 5: if speed <=60
output Good speed
5.1 : else
5.1.1 if( speed >60 && speed < 65 ) // if speed is between 60 and 65kmph we give warning only
output Waring your are exeeding speed limit
5.1.2 else if (speed >= 65 && speed< 70 ) // if speed is between 65 and less than 70 kmph fine is 80 dollars
output you have to pay fine $: FINE_FOR_65
5.1.3 else if (speed >= 70 && speed < 80) // if speed is between 70 and less than 80 kmph fine is 150 dollars
output you have to pay fine $: FINE_FOR_70
5.1.4 else // if speed is more than 80 kmph fine is 500 dollars
output you have to pay fine $:,FINE_FOR_80
Step 6: Stop
2. // c program to simulate police radar gun
#include <stdio.h>
int main()
{
int speed=0;// intializing the variable speed to 0
const int FINE_FOR_65 = 80; //assigning $80 to constant varaible FINE_FOR_65
const int FINE_FOR_70 = 150; //assigning $150 constant varaible FINE_FOR_70
const int FINE_FOR_80 = 500; //assigning $ 500 to constant vairable FINE_FOR_80
printf("enter the speed in kmph ");
scanf("%d",&speed);
if (speed <=60 )
printf("Good speed");
else
{
if( speed >60 && speed < 65 ) // if speed is between 60 and 65kmph we give warning only
printf("Waring your are exeeding speed limit");
else if (speed >= 65 && speed< 70 ) // if speed is between 65 and less than 70 kmph fine is 80 dollars
printf(" you have to pay fine $: %d", FINE_FOR_65);
else if (speed >= 70 && speed < 80)
printf(" you have to pay fine $: %d", FINE_FOR_70);
else
printf(" you have to pay fine $: %d",FINE_FOR_80 );
}
return 0;
}
Sample Out puts:
enter the speed in kmph 59
Good speed
enter the speed in kmph 61
Waring your are exeeding speed limit
enter the speed in kmph 65
you have to pay fine $: 80
enter the speed in kmph 70
you have to pay fine $: 150
enter the speed in kmph 80
you have to pay fine $: 500
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.