CEG220: Introduction to C Programming for Engineers – I

Section 2

 

Homework for Week 3

 

1)      pp. 152 – 153, #1-16

 

2)      Boolean Math: Evaluate the following expressions

1.      int a = 5, int b = 6, int c = 10

1.      (a == b) == c

2.      ((a > b) && (a < b))

3.      ((a == b) || (a == c)) && (!((a == b) && (a == c)))

2.      double a = 5, double b = 10, double c = 2

1.      (a != (b / c))

2.      ((c == a) == (b == a))

3.      ((c != a) != (c == a))

3.      What’s the LOGICAL OPPOSITE of the < and > operators?

4.      What’s the LOGICAL OPPOSITE of the <= and >= operators?

 

3)      Flow Control

1.      given, double grade, which ranges from 0 to 100, print out the grade letter that the student receives; if an invalid grade is specified, print an error message.

2.      Your classmate Seamus thought that he’d solve problem 2.1 by using a switch statement (see below) for each letter grade. What’s wrong with his logic? What would you do to correct his logic?

 

switch(grade) {

            case 100: printf(“You got an A+!\n”); break;

            case 90: printf(“You got an A!\n”); break;

case 80: printf(“You got a B!\n”); break;

            case 70: printf(“You got a C!\n”); break;

            case 60: printf(“You got a D!\n”); break;

            case 50: printf(“You have failed the course!\n”); break;

            default: printf(“Perhaps you should drop the course?\n”); break;

}

 

3.      When do you want to use a switch statement instead of an if statement?

4.      Is the following code valid? If so, what will the program print out? If not, why not (ie. how can you fix this code to make it compile if there is a compile error, or make it do what is intended if there is an error of intention)?

 

unsigned int data = 15;

unsigned int ui = 10, uj = 20, uk = 30, ul = 40, um = 50;

switch(data) {

case ui: printf(“Insufficient Funds!\n”); break;

case uj: printf(“You are out of money!\n”); break;

case uk: printf(“You have about 10 dollars left over.”); break;

case ul: printf(“You have about 20 dollars left over.”); break;

case um: printf(“You have about 30 dollars left over.”); break;

}

1.      Why are if statements important?