Speed vs License
Driving Age
Speed vs License
Variations on the program from Wednesday’s lecture: given the speed you were driving, and your license type, should you be arrested?
Note: you can see the full code below the images.
Code:
// Are you breaking the speed limit?
// Andrew Bennett (z3378327)
// 2017-08-02
// DRIVING SPEED RULES
// LICENSE MAX SPEED
// LEARNERS 80
// PROVISIONAL_1 90
// PROVISIONAL_2 100
// FULL ?????
#include <stdio.h>
#include <stdlib.h>
#define LEARNERS_LICENSE 0
#define P1_LICENSE 1
#define P2_LICENSE 2
#define FULL_LICENSE 3
int main () {
int license ;
int speed ;
int speed_limit = 130 ;
printf ( "What speed were you going? " );
scanf ( "%d" , & speed );
printf ( "What license type do you have? \n " );
printf ( "Choices: \n " );
printf ( "0: Learners \n " );
printf ( "1: P1 \n " );
printf ( "2: P2 \n " );
printf ( "3: Full \n " );
scanf ( "%d" , & license );
// option A
printf ( "A: " );
if ( speed > speed_limit ) {
printf ( "arrested \n " );
} else if ( license == P2_LICENSE && speed > 100 ) {
printf ( "arrested \n " );
} else if ( license == P1_LICENSE && speed > 90 ) {
printf ( "arrested \n " );
} else if ( license == LEARNERS_LICENSE && speed > 80 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
// option B
printf ( "B: " );
if ( speed > speed_limit ) {
printf ( "arrested \n " );
} else {
if ( license == P2_LICENSE && speed > 100 ) {
printf ( "arrested \n " );
} else if ( license == P1_LICENSE && speed > 90 ) {
printf ( "arrested \n " );
} else if ( license == LEARNERS_LICENSE && speed > 80 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
// option D
printf ( "D: " );
if ( speed > speed_limit ) {
printf ( "arrested \n " );
} else {
if ( license == LEARNERS_LICENSE ) {
if ( speed > 80 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
if ( license == P1_LICENSE ) {
if ( speed > 90 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
if ( license == P2_LICENSE ) {
if ( speed > 100 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
if ( license == FULL_LICENSE ) {
printf ( "not arrested \n " );
}
}
// option E
printf ( "E: " );
if ( speed > 80 ) {
if ( license == LEARNERS_LICENSE ) {
printf ( "arrested \n " );
} else {
if ( speed > 90 ) {
if ( license == P1_LICENSE ) {
printf ( "arrested \n " );
} else {
if ( speed > 100 ) {
if ( license == P2_LICENSE ) {
printf ( "arrested \n " );
} else if ( speed > 130 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
} else {
printf ( "not arrested \n " );
}
}
} else {
printf ( "not arrested \n " );
}
}
} else {
printf ( "not arrested \n " );
}
// option F
printf ( "F: " );
if ( license == LEARNERS_LICENSE ) {
if ( speed > 80 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
if ( license == P1_LICENSE ) {
if ( speed > 90 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
if ( license == P2_LICENSE ) {
if ( speed > 100 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
if ( license == FULL_LICENSE ) {
if ( speed > 130 ) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
}
// option G
printf ( "G: " );
if ( speed > speed_limit || ( license == P2_LICENSE && speed > 100 ) || ( license == P1_LICENSE && speed > 90 ) || ( license == LEARNERS_LICENSE && speed > 80 )) {
printf ( "arrested \n " );
} else {
printf ( "not arrested \n " );
}
// option H
printf ( "H: " );
if ( speed <= 80 && license == LEARNERS_LICENSE ) {
printf ( "not arrested \n " );
} else if ( speed <= 80 && ( license == P1_LICENSE || license == P2_LICENSE )) {
printf ( "not arrested \n " );
} else if ( speed <= 80 && ( license == FULL_LICENSE || license == LEARNERS_LICENSE )) {
printf ( "not arrested \n " );
} else if ( speed > 80 && license == LEARNERS_LICENSE && license != P1_LICENSE ) {
printf ( "arrested \n " );
} else if ( speed > 80 && license == P1_LICENSE && speed <= 90 ) {
printf ( "not arrested \n " );
} else if ( license == P2_LICENSE && speed > 80 && speed <= 100 ) {
printf ( "not arrested \n " );
} else if ( speed > 80 && license == FULL_LICENSE && speed <= speed_limit ) {
printf ( "not arrested \n " );
} else if ( speed > 90 && license == P1_LICENSE ) {
printf ( "arrested \n " );
} else if ( license == P2_LICENSE && speed > 100 ) {
printf ( "arrested \n " );
} else if ( speed > speed_limit ) {
printf ( "arrested \n " );
}
}
Driving Age
Variations on the program from Monday’s lecture: are you old enough to drive?
// Can a user drive?
// Andrew Bennett (z3378327)
// 2017-07-31
#include <stdio.h>
#include <stdlib.h>
#define MIN_DRIVING_AGE 16
#define MAX_DRIVING_AGE 120
int main ( int argc , char * argv []) {
int age ;
// Print "How old are you?"
printf ( "How old are you? \n " );
// Read in their age.
if ( scanf ( "%d" , & age ) == 1 ) {
} else {
printf ( "Invalid age \n " );
return EXIT_FAILURE ;
}
// Invalid age: not a number (eg "bananas").
if ( scanf ( "%d" , & age ) != 1 ) {
printf ( "Invalid age \n " );
return EXIT_FAILURE ;
}
// Print out their age, to visually check what the input was.
printf ( "Your age is: %d \n " , age );
// Invalid age: negative number.
if ( age < 0 ) {
printf ( "Invalid age \n " );
return EXIT_FAILURE ;
}
// If their age is >= 16 and < 120, print "You can drive".
// Otherwise: print "You cannot drive".
if ( age < MIN_DRIVING_AGE ) {
printf ( "You cannot drive \n " );
} else if ( age < MAX_DRIVING_AGE ) {
printf ( "You can drive \n " );
} else {
printf ( "You cannot drive \n " );
}
// Print "Have a nice day."
printf ( "Have a nice day :) \n " );
return EXIT_SUCCESS ;
}