CSC 180 Notes for Tutorial #4 Monday 7 October Wednesday 9 October ============ Notes for your tutorial ========================================= SOME SMALL PROGRAMMING EXAMPLES ================================ - Some suggested problems to solve: I o Write a program that asks the user to enter a postitive integer and then prints the sum of the digits in the number. The user interface might looks somthing like: This program sums the digits in an integer. Enter a positive integer: 1729 The sum of digits is 19 <(Try writing the exit condition as n >= 0 first, and then figure our why it needs to be n >0 ) > A sample solution follows. II o Note that our solution to I does not explicitly check whether or not the user entered a positive integer. What happens if the user enters an negative or 0 input? III o Modify the program for I so that it repeatedly processes positive integers until the user enters a number <= 0. IV Rewrite the program for I so that instead of adding the digits in the number, it generate the number that has the same digits, but in reverse order, as illustrated by this sample run: This program reverses the digits in an integer. Enter a positive integer: 1729 The reversed number is 9271 V A times-table is often used to teach people simple multiplication results. For example, the 4 by 4 times table is: 1x1=1 2x1=2 3x1=3 4x1=4 1x2=2 2x2=4 3x2=6 4x2=8 1x3=6 2x3=6 3x3=9 4x3=12 Write a program that generates the n by n times table for an input value of n. Assume that n has only one digit, and format the output so that each example lines up. VI The times-table has redundant information. e.g. 2x1=2 and 1x2=2. How would you modify the program from V so that only new times are printed? VII Write a program to display the following diagram on the screen. The number of rows in the figure should be a #define constant which has the value 8 for this sample run: * ** *** **** ***** ****** ******* ******** Exercise for them: Modify the program above so that it generates a different triangle. In this triangle, each line contains two more points than the previous line does, and the point of the triangle faces upwards, as follows: * *** ***** ******* If you have time, talk to them about how the original program will have to change. Here are sample solutions to the problems: =========================================== Problem I main() { int n, dsum; printf("This program sums the digits in an integer.\n"); printf("Enter a positive integer: "); scanf("%d", &n); dsum = 0; while (n > 0) { dsum = dsum + ( n%10 ); n = n / 10; } printf("The sum of the digits is %d\n", dsum); } Problem IV main() { int n, reversedn; printf("This program reverses the digits in an integer.\n"); printf("Enter a positive integer: "); scanf("%d", &n); reversedn = 0; while (n > 0) { reversedn = reversedn * 10 + ( n%10 ); n = n / 10; } printf("The reversed number is %d\n", reversedn); } Problem V main() { int i, j, n; printf("This program prints a times-table.\n"); printf("Enter a positive integer: "); scanf("%d", &n); printf("Here is the %dx%d times-table.\n", n, n ); for ( i = 1; i <=n ; i++ ){ for ( j = 1; j <=n ; j++ ){ printf("%dx%d=%-4d", i, j, i*j ); /* note format to line up. */ } printf("\n"); /* so next line starts on a new line */ } } Program VII main() { #define n 8 int i, j; for (i = 1; i <= n; i++ ){ for (j = 1; j <= i; j++ ){ printf("*"); } printf("\n"); } }