Chapter 11 Case Studies Chapter 11: Case Studies 1 Finding Maximum in List Init max While not end of list Read value If (value>max) max value Report max Chapter 11 Need to know range of values Need to assign sentinel, or use endof-file (ctrl-d in UNIX) character. Finding Maximum in List 2 Finding Maximum in List Assume data are positive integers. Set sentinel to -999 (or any non-positive integer). Write a do-while loop. Chapter 11 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); Finding Maximum in List 3 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 4 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 Finding Maximum in List 5 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 Problem 6 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 7 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 8 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 9 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 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; 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 11 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 12 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 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; else all_white = 0; } if (all_white) printf("All eggs are white.\n"); else printf("Not all eggs are white.\n"); Chapter 11 Existential & Universal Test 14 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 Problem 15 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 = 0 But 4x + 5 = 0; x3 + 3x2 + 2 = 0 are not. Chapter 11 Quadratic Equations 16 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. $ quadratic Enter a, b, c: 3, -7, 2 Quadratic equation is 3x^2 – 7x + 2 = 0 $ quadratic Enter a, b, c: 0, 2, 4 This is not a quadratic equation. $ quadratic Enter a, b, c: -1, 0, -3 Quadratic equation is -x^2 – 3 = 0 Chapter 11 Quadratic Equations 17 Quadratic Equations Add a loop in your program so that you can enter many equations in a single run, as shown below. $ quadratic Enter a, b, c: 3, -7, 2 Quadratic equation is 3x^2 – 7x + 2 = 0 Do you want to continue (y/n)? y Enter a, b, c: 0, 2, 4 This is not a quadratic equation. Do you want to continue (y/n)? y Enter a, b, c: -1, 0, -3 Quadratic equation is -x^2 – 3 = 0 Do you want to continue (y/n)? n Bye! Chapter 11 Quadratic Equations 18 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 19 Quadratic Equations Solve the quadratic equations in your program, if they have real roots. $ quadratic Enter a, b, c: 3, -7, 2 Quadratic equation is 3x^2 – 7x + 2 = 0 Real roots are: 2.00 and 0.33 Do you want to continue (y/n)? y Enter a, b, c: 0, 2, 4 This is not a quadratic equation. Do you want to continue (y/n)? y Enter a, b, c: -1, 0, -3 Quadratic equation is -x^2 – 3 = 0 There are imaginary roots. Do you want to continue (y/n)? n Bye! Chapter 11 Quadratic Equations 20 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 21 Factorisation Sample run. Enter a positive integer (0 to end): 1 1 = 1 Enter a positive integer (0 to end): 5 1 = 1 * 5 Enter a positive integer (0 to end): 8 1 = 1 * 2 * 2 * 2 Enter a positive integer (0 to end): 90 1 = 1 * 2 * 3 * 3 * 5 Enter a positive integer (0 to end): 91 1 = 1 * 7 * 13 Enter a positive integer (0 to end): 0 Bye! Chapter 11 Factorisation 22 Homework Try exercises in preceding slides. Chapter 11 Homework 23