CSE 130 Spring 2010 Lab 4 PLEASE DO NOT SUBMIT LAB EXERCISES. They are for your practice only. TA will help you with the following exercise. ---------------------------------------------------------------------- 1. Using bash a. After you sign in, type a command `bash' (without quotes). With this backspace key will work. All other UNIX commands will work as before. A list of UNIX command is on the course webpage. b. When you are done, type exit to get out of bash. Do not do that right now. Type logout to disconnect from sparky. 2. Nested while loops c. Cut and paste this program in your pico window. Save this as lab4.c in your directory. Compile and execute. The program just echoes values of m and n that you typed. // lab4.c Feb 24, 2010. // A program to study nested while loops. // Example for class CSE130 #include int main (void) { int m, n printf("Type in m, n on the same line. Let m > n.\n"); scanf ("%d%d", &m, &n); printf("\nm = %d, n = %d\n\n", m, n); // Insert code in the space below for nested loops. // Do this step by step. Do the inner loop first. return 0; } d. We are going to add code to the above program, just after the second printf statement to print a rectangle of m rows (lines) with n stars (*) on each line. TA will explain loop structure on white-board. e. For this purpose we will write two nested while loops with i and j as two loop counters. Outer loop runs from i = 1 to m printing m rows of stars. Inner while loop prints n stars on each line. It runs from j = 1 to n. Declare and initialize i, j properly. Also print \n at correct places. f. To do this step by step, first write the inner while loop that prints a line (or just one row of stars). Use n here. Cut and paste this code to your PICO window, after the second printf statement. g. Fill in the condition of while loop, and printf statement. After the loop, print \n. j = 1; while ( ) { printf ( ); j = j+1; } h. Save compile and execute. It will ask for both m and n. Type m = 4 and n = 3. The program should print one line of 3 stars. i. Now add the outer while loop. What is the condition of outer loop? Do not forget to initialize i = 1; or something like that. Also you need to increment i (i++;) in the body of the outer loop. j. Save your program, compile and execute. If m = 4, and n = 3 then rectangle would be like: *** *** *** *** 3. Modification-I: Printing a right angle triangle: k. For this we print 1 star on the first line, 2 stars on the second and so on. On the last line print m stars. We need only m. Pro- gram will ask for m and n both. That is ok. Type in both. Just DO NOT USE n. l. The outer loop runs from i = 1 to m. Keep that as it is. The inner loop prints m stars for the mth iteration of the outer loop. Be careful with modifying the inner loop. We use j for the inner loop. But the loop runs from j = 1 to j = m and not n. Change the condition of inner while-loop. m. For m = 4, triangle would look like: * ** *** **** n. Do not use for or do-while loops. You MUST use while loops. 4. An if-else statement (if you have time) o. TA will tell you how to write an `if' statement for checking if m is indeed greater than n. Print proper message such as m is greater than n or m is not greater than n. // An if-else statement if ( ) printf ("m is greater than n. \n"); else printf ("m is not greater than n. \n"); p. Insert an if-else statement after nested while loops. q. Save, Compile and execute. Try different values of m. Here you need to type in n also. See if your if statement works correctly. r. Try m = 4, n = 3. Also try m = 5, n = 8. Remember every time you execute start with ./a.out