{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 3_Answers_6_4_nested_loops-1 - CIS 15AG Answers Chapter 6...

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

CIS 15AG Answers Chapter 6: Repetitions (nested loops) 25. Write a fragment of code that prints 3 rows of 5 asterisks each. ***** ***** ***** 26. Write a function that prints n rows of asterisks. The first row has 2 asterisks, the second 4, the third 6, and so on. Below is the pattern for n = 5 ** **** ****** ******** ********** 1 int i; for ( i = 1; i <= 5; i++ ) printf( "*" ); printf( "\n" ); for ( i = 1; i <= 5; i++ ) printf( "*" ); printf( "\n" ); for ( i = 1; i <= 5; i++ ) printf( "*" ); printf( "\n" ); int i; int row; for ( row = 1; row <= 3; row++ ) { for ( i = 1; i <= 5; i++ ) printf( "*" ); printf( "\n" ); } void printTriangle(int height) { int row; int col; int width; for ( row = 1; row <= height; row++ ) { width = 2 * row ; for ( col = 1; col <= width; col++ ) printf( "*" ); printf( "\n" ); } return; } void printTriangle(int height) { int row; int col; for ( row = 1; row <= height; row++ ) { for ( col = 1; col <= 2 * row ; col++ ) printf( "*" ); printf( "\n" ); } return; } // No width as a local variable, but more operations: 2 * row is being calculated for each asterisk (instead of for each row)

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

View Full Document
CIS 15AG Answers Chapter 6: Repetitions (nested loops) 27. Predict the output. int i; int j; for ( i = 1; i <= 3; i++ ) { for ( j = 1; j <= 3; j++ ) printf("%d %d\n", i, j); printf("\n"); } 28. How many asterisks and '\n's are displayed?
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern