310 likes | 543 Views
Case Study. Instructor: Chieh-Chih (Bob) Wang Department of Computer Science & Information Engineering National Taiwan University. Print a table that shows the value of π approximated by one term of this series, by two terms, by three terms, etc.
E N D
Case Study Instructor: Chieh-Chih (Bob) Wang Department of Computer Science & Information Engineering National Taiwan University
Introduction to Computer Programming -- Fall 2008 • Print a table that shows the value of π approximated by one term of this series, by two terms, by three terms, etc. • How many terms of this series do you have to use before you first get 3.14? 3.141? 3.1415? 3.14159?
Introduction to Computer Programming -- Fall 2008 Pythagorean Triples • A right triangle can have sides that are all integers. The set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. • Find all Pythagorean triples for side1, side2, and the hypotenuse all no larger than 500. Use a triple-nested for loop that simply tries all possibilities.
Introduction to Computer Programming -- Fall 2008 Round a number to a specific decimal place • Function floor may be used to round a number to a specific decimal place. • The statement y = floor( x * 10 + .5 ) / 10; rounds x to the tenths position (the first position to the right of the decimal point). • The statement y = floor( x * 100 + .5 ) / 100; rounds x to the hundredths position (i.e., the second position to the right of the decimal point). • Write a program that defines four functions to round a number x in various ways • a) roundToInteger( number ) • b) roundToTenths( number ) • c) roundToHundreths( number ) • d) roundToThousandths( number )
Introduction to Computer Programming -- Fall 2008 Write statements that assign random integers to the variable n in the following ranges:
Introduction to Computer Programming -- Fall 2008 Write a function that returns the smallest of three floating point numbers.
Introduction to Computer Programming -- Fall 2008 Perfect Number • An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. • For example, 6 is a perfect number because 6 = 1 + 2 + 3. • Write a function perfect that determines if parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. • Challenge the power of your computer by testing numbers much larger than 1000.
Introduction to Computer Programming -- Fall 2008 Prime • An integer is said to be prime if it is divisible only by 1 and itself. For example, 2, 3, 5 and 7 are prime, but 4, 6, 8 and 9 are not. • How?
Introduction to Computer Programming -- Fall 2008 Greatest Common Divisor • The greatest common divisor (GCD) of two integers is the largest integer that evenly divides each of the two numbers
Introduction to Computer Programming -- Fall 2008 Recursive version
Introduction to Computer Programming -- Fall 2008 Arrays • Mean, median, mode
Introduction to Computer Programming -- Fall 2008 • Write a program that simulates the rolling of two dice. The program should use rand to roll the first die, and should use rand again to roll the second die. The sum of the two values should then be calculated.
Introduction to Computer Programming -- Fall 2008 Characters and Strings • Write a program that inputs a line of text with function gets into char array s[ 100 ]. Output the line in uppercase letters and in lowercase letters.
Introduction to Computer Programming -- Fall 2008 • Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. atoi
Introduction to Computer Programming -- Fall 2008 • Write a program that inputs a line of text and a search string from the keyboard. • Using function strstr, locate the first occurrence of the search string in the line of text, and assign the location to variable searchPtr of type char *.
Introduction to Computer Programming -- Fall 2008 Use the string comparison functions and the techniques for sorting arrays to write a program that alphabetizes a list of strings.
Introduction to Computer Programming -- Fall 2008 Write a program that reads a series of strings and prints only those strings that end with the letters “ed.”
Introduction to Computer Programming -- Fall 2008 • Write a program that merges two ordered lists of integers into a single ordered list of integers. • Function merge should receive pointers to the first node of each of the lists to be merged and should return a pointer to the first node of the merged list.
Introduction to Computer Programming -- Fall 2008 • Write a program that inserts 25 random integers from 0 to 100 in order in a linked list. The program should calculate the sum of the elements and the floating-point average of the elements.
Introduction to Computer Programming -- Fall 2008 Final Exam Reminder • Array, String • Pointer, Data Structure • File Processing • Sorting • Recursion
Introduction to Computer Programming -- Fall 2008 Questions?