CSE 130 Fall 2009 Lab 5 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. Using for loops c Write a program that reads six integers from keyboard and prints their sum. You must use a for loop. d. Open a pico window. Part of lab5.c is given here. Cut and paste this program in your pico window. Save this as lab5.c in your directory. Compile and execute. The program just prints value of sum (which is 0). // lab5.c October 7, 2009. // A program to study for loops and break statement. // Example for class CSE130 #include int main (void) { int i, sum = 0; int n = 6; // Read six integers. int number; // Number to be read from keyboard. // Insert a for loop printf("Sum = %d, Count = %d\n", sum, i); return 0; } e. We are going to add code to the above program, just after declarations. Insert a for loop, that runs from i = 0 to i < n. Inside the loop prompt the user for the next number. Read it using scanf. Add it to sum. printf("Type in a number: "); scanf("%d", &number); sum = sum + number; f. Save, compile and execute. It will ask for six numbers because n is six. Type 1 2 3 4 5 6. Sum printed should be 21, and count should be 6. 3. Modification-I (Break statement) g. Now modify the program as follows. We want to get out of the loop, if user types in a negative number, even if the count has not reached 6. Remember, if user does not type in any negative number then loop must terminate after six iterations. h. Insert an if statement right after scanf. if (number < 0) break; The break statement will get you out of the loop. Since your print statement is after the loop, you will also get the count of numbers read. i. Save your program, compile and execute. Try 1, 2, 3, -4. Here sum should be 6 and count = 3. 4. Modification-II: Changing to a while loop j. For this modification, replace the for loop by a while loop. Use i as the loop counter for while. k. Initialize i = 0, just before the loop. Condition of while loop is (i < n). You need to increment i inside the loop. Do it after sum = sum+number. l. Save your program, compile and execute. Try 4, 5, -1. Here sum = 9 and count = 2. 5. Modification-III (Remove break statement) m. Now modify the program such that it does not use break statement. But if the number read is negative ( < 0), then you should not continue with the next iteration. n. For this, you will need one scanf statement before the loop. Do not forget to have the print statement that prompts user. o. Have one more condition for while loop. Include (number >= 0). Use && connective. p. Delete the break statement. The scanf statement should be at the end of your loop. As soon as you enter the loop, update sum fol- lowed by i++. q. Save, Compile and execute. Try 4, 5, 0, -2. Here sum should be 9 and count = 3.