Chapter 11: Case Studies1 Chapter 11 Case Studies.

Post on 02-Jan-2016

241 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

Transcript

Chapter 2 Chapter 11: Case Studies 1

Chapter 11

Case Studies

Chapter 11 Finding Maximum in List 2

Finding Maximum in List

Init maxWhile not end of list

Read valueIf (value>max)

max valueReport max

Need to know range of values

Need to assign sentinel, or use end-of-file (ctrl-d in UNIX) character.

Chapter 11 Finding Maximum in List 3

Finding Maximum in List

Assume data are positive integers.Set sentinel to -999 (or any non-positive

integer).max = –1;printf("Enter value (–999 to end): ");scanf("%d", &value);while (value != –999) { if (value > max) max = value; printf("Enter value (–999 to end): "); scanf("%d", &value);}printf("Maximum is %d\n", max);

Write a do-while loop.

Chapter 11 Finding Maximum in List 4

Finding Maximum in List

For the moment, to simplify matter, we assume first data is n, the number of elements in the list.

max = –1;printf("Enter the size of the list: ");scanf("%d", &n);for (i = 0; i < n; i++) { printf("Enter value: "); scanf("%d", &value); if (value > max) max = value;}printf("Maximum is %d\n", max);

Chapter 11 Finding Maximum in List 5

Finding Maximum in List

We may initialise first data value to max.

printf("Enter the size of the list: ");scanf("%d", &n);printf("Enter value: ");scanf("%d", &value);max = value;for (i = 1; i < n; i++) { printf("Enter value: "); scanf("%d", &value); if (value > max) max = value;}printf("Maximum is %d\n", max);

Chapter 11 Problem 6

Problem

Find the maximum and second maximum of a list of integers.

Do not assume the range of values for list elements.

Assume first data is list size n, where n >= 2.

Chapter 11 Existential & Universal Test 7

Existential & Universal Test

Sometimes, we need to test some property in a list. Two common tests are:

Existentiality: is there an element with a certain property?

Universality: do all elements have a certain property?

Chapter 11 Existential & Universal Test 8

Existential & Universal Test

Test for existentiality versus test for universality.

Is there a black egg among all eggs? This employs existentiality.

Are all eggs white? This employs universality.

Their codes are different.

Chapter 11 Existential & Universal Test 9

Existential & Universal Test

Is there a black egg among all eggs?Data representation: 'b' is black, 'w' is white.

black_exists = 0;for (i = 0; i < n; i++) { scanf("%c", &egg); if (egg == 'b') { black_exists = 1; break; }}if (black_exists) printf("Black egg exists.\n");else printf("Black egg does not exist.\n");

Chapter 11 Existential & Universal Test 10

Existential & Universal Test

What is wrong with this code?

black_exists = 0;for (i = 0; i < n; i++) { scanf("%c", &egg); if (egg == 'b') black_exists = 1;}if (black_exists) printf("Black egg exists.\n");else printf("Black egg does not exist.\n");

Chapter 11 Existential & Universal Test 11

Existential & Universal Test

What is wrong with this code?

black_exists = 0;for (i = 0; i < n; i++) { scanf("%c", &egg); if (egg == 'b') black_exists = 1; else black_exists = 0;}if (black_exists) printf("Black egg exists.\n");else printf("Black egg does not exist.\n");

Chapter 11 Existential & Universal Test 12

Existential & Universal Test

Are all eggs white?

all_white = 1;for (i = 0; i < n; i++) { scanf("%c", &egg); if (egg == 'b') { all_white = 0; break; }}if (all_white) printf("All eggs are white.\n");else printf("Not all eggs are white.\n");

Chapter 11 Existential & Universal Test 13

Existential & Universal Test

What is wrong with this code?

all_white = 0;for (i = 0; i < n; i++) { scanf("%c", &egg); if (egg == 'w') { all_white = 1; break; }}if (all_white) printf("All eggs are white.\n");else printf("Not all eggs are white.\n");

Chapter 11 Existential & Universal Test 14

Existential & Universal Test

What is wrong with this code?

all_white = 0;for (i = 0; i < n; i++) { scanf("%c", &egg); if (egg == 'w') all_white = 1; else all_white = 0;}if (all_white) printf("All eggs are white.\n");else printf("Not all eggs are white.\n");

Chapter 11 Problem 15

Problem

Given a list of integers, determine if the list is in strictly increasing order. For example, (-3, 0, 6, 17) is in strictly increasing order, but (-6, 2, 2, 5) and (5, 7, 8, 6) are not.

Do not assume the range of values for list elements.

Assume first data is list size n, where n >= 0. (An empty list and a list of one element are trivially in strictly increasing order.)

Chapter 11 Quadratic Equations 16

Quadratic Equations

A quadratic equation in x is of this form:

ax2 + bx + c = 0

Assume that a, b, and c are integers, and a is not equal to zero.

Examples:2x2 - 3x -7 = 0; -3x2 + 8x -3 = 0; x2 + 4 = 0But 4x + 5 = 0; x3 + 3x2 + 2 = 0 are not.

Chapter 11 Quadratic Equations 17

Quadratic Equations

Write a program to request for the values a, b, c of a quadratic equation and display the equation. See three sample runs below.

$ quadraticEnter a, b, c: 3, -7, 2Quadratic equation is 3x^2 – 7x + 2 = 0$ quadraticEnter a, b, c: 0, 2, 4This is not a quadratic equation.$ quadraticEnter a, b, c: -1, 0, -3Quadratic equation is -x^2 – 3 = 0

Chapter 11 Quadratic Equations 18

Quadratic Equations

Add a loop in your program so that you can enter many equations in a single run, as shown below.

$ quadraticEnter a, b, c: 3, -7, 2Quadratic equation is 3x^2 – 7x + 2 = 0 Do you want to continue (y/n)? yEnter a, b, c: 0, 2, 4This is not a quadratic equation. Do you want to continue (y/n)? yEnter a, b, c: -1, 0, -3Quadratic equation is -x^2 – 3 = 0 Do you want to continue (y/n)? nBye!

Chapter 11 Quadratic Equations 19

Quadratic Equations

Recall that the roots are:

[ -b ± (b2 - 4ac) ] / 2a

The discriminant is (b2 - 4ac). If this is positive, then there are two real roots

zero, then there is only one real root

negative, then there are imaginary roots

Chapter 11 Quadratic Equations 20

Quadratic Equations

Solve the quadratic equations in your program, if they have real roots.

$ quadraticEnter a, b, c: 3, -7, 2Quadratic equation is 3x^2 – 7x + 2 = 0Real roots are: 2.00 and 0.33 Do you want to continue (y/n)? yEnter a, b, c: 0, 2, 4This is not a quadratic equation. Do you want to continue (y/n)? yEnter a, b, c: -1, 0, -3Quadratic equation is -x^2 – 3 = 0There are imaginary roots. Do you want to continue (y/n)? nBye!

Chapter 11 Factorisation 21

Factorisation

Write a program to enter a list of positive integers, using zero as a sentinel to end the input.

For each positive integer entered, find out its factors, and display the factors as shown in the example, in increasing order of factors. (Factor 1 is listed only once)

Chapter 11 Factorisation 22

Factorisation

Sample run. Enter a positive integer (0 to end): 11 = 1

Enter a positive integer (0 to end): 51 = 1 * 5

Enter a positive integer (0 to end): 81 = 1 * 2 * 2 * 2

Enter a positive integer (0 to end): 901 = 1 * 2 * 3 * 3 * 5

Enter a positive integer (0 to end): 911 = 1 * 7 * 13

Enter a positive integer (0 to end): 0Bye!

Chapter 11 Homework 23

Homework

Try exercises in preceding slides.

top related