# tutorial05_ans - CS1010E Programming Methodology Semester 1...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS1010E Programming Methodology Semester 1 2011/2012 Week of 26 – 30 September 2011 Tutorial 5 Suggested Answers Problem Solving with Control Flow Statements 1. Just like how printing number sequences is the best way to test your understanding of looping constructs, printing patterns is another good way to test your understanding of nested loop constructs. (a) Write a function printLeftTriangle() that takes as argument an integer n and prints a left-sided triangle. For example, if n is 5, the following is printed * ** *** **** ***** (b) Write a function printRightTriangle() that takes as argument an integer n and prints a right-sided triangle. For example, if n is 5, the following is printed * ** *** **** ***** (c) Write a function printMiddleTriangle() that takes as argument an integer n and prints a right-sided triangle. For example, if n is 5, the following is printed * *** ***** ******* ********* (d) Write a function printHollowTriangle() that takes as argument an integer n and prints a right-sided triangle. For example, if n is 5, the following is printed * * * * * * * ********* You are encouraged to devise your own patterns and post in the IVLE forum. 1 #include <stdio.h> void printLeftTriangle(int n); void printRightTriangle(int n); void printMiddleTriangle(int n); void printHollowTriangle(int n); int main(void) { int n; printf("Enter n of pattern: "); scanf("%d", &n); printLeftTriangle(n); printRightTriangle(n); printMiddleTriangle(n); printHollowTriangle(n); return 0; } void printLeftTriangle(int n) { int r, c; for (r = 1; r <= n; r++) { for (c = 1; c <= r; c++) printf("*"); printf("\n"); } return; } void printRightTriangle(int n) { int r, c; for (r = n; r >= 1; r--) { for (c = 1; c <= n; c++) if (c < r) printf(" "); else printf("*"); printf("\n"); 2 } return; } void printMiddleTriangle(int n)...
View Full Document

## This note was uploaded on 01/05/2012 for the course MATHEMATIC MA1505 taught by Professor Freudleong during the Spring '10 term at National University of Singapore.

### Page1 / 10

tutorial05_ans - CS1010E Programming Methodology Semester 1...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online