CSE 130 Fall 2009 Lab 3 NOTE: It is very important for this course that you have an active sparky account for completing assignments. If you don't have one, visit the SINC site in the main library building and speak to a representative to resolve this as soon as possible. TA will help you with the following exercise. ---------------------------------------------------------------------- 1. lab2.c Revisited a. During our second exercise, we studied lab2.c program that prints the numbers from 1 to 10 (on one line). The while (test- condition) {.. body ..} statement causes the {.. body ..} to be repeated as long as the (condition) is true. The <= is how one types "less than or equals". b. The int i=1; statement has been changed to two statements as explained later. // This is lab3.c // A different version of previous program. // Sample program to print: 1 2 3 4 5 6 7 8 9 10. // if user types a starting value = 1. // Example for class CSE130. // We print i using printf function. // Use of %d means i will be printed as a decimal integer. // There is a space before % character. #include int main (void) { int i; // Indentation is important. printf("Type in starting value for i: \n"); scanf("%d", &i); while (i <= 100) { printf(" %d", i); i=i+1; } printf(".\n"); return 0; } c. After sign-on, change your working directory to cse130. The com- mand is: cd cse130 d. Open the editor window for pico by typing pico. Cut and paste lab3.c to your pico window. Save as lab3.c (ctrl-o) and exit (ctrl-x). e. Note that here we just have int i; as a declaration for i. We read starting value for i from the user. f. TA will discuss indentation and explain why is that important. All program must be properly indented. Some points are reserved for that. g. Compile your program and execute. (gcc lab3.c followed by ./a.out) Try i = 1 h. It should output 1 2 3 4 5 6 7 8 9 10 ...... up to 100. 2. Modification - I i. For this program the increment in i is always 1. This is because we have i = i+1; in our while loop. j. Replace that statement with i = i+j; Make sure that j is declared as in integer. Either insert int j, or have int i, j; k. Also do not forget to initialize j to whatever increment that you want. Let us give j a value of 4. This must be done before while loop. A good place would be after the scan statement that reads i. l. Save your program, compile and execute. Try i = 7. See what it prints. 3. Modification - II m. Instead of using a fixed value for j, we can ask user for a value for j as well. This is our increment in i. n. Delete j = 4; Insert a print statement that should say Type in a value for increment j. Do not forget to add \n. o. Follow this up with a scanf statement scanf("%d", &j); p. Save your program. Compile and execute. Try different values of i and j for every execution. Try i = 8, j = 9. 4. Modification - III q. What if we want to add values of i that are getting printed by our while loop? r. So we declare a variable called total. What should be its type? Initialize it to 0. s. Inside the loop, just after we print i, accumulate total by doing total = total + i; t. Print total outside the loop, but after the dot (`.') has been printed. Use a printf statement. Follow the format Total = ____ u. Save your program, compile and execute. Try i = 1, j = 10. Total should be 460 5. Modification - IV v. If you run out of time try this later. w. Now after you print the period ("."), calculate square of i and cub of i. You need to declare these two variables. x. Before return 0; statement insert two statements. i_sq = i * i; and i_cube = i_sq * i; y. Print i_sq and i_cube using a printf statement. z. Format is: i_sq = , i_cube = Do not forget the \n at the end of printf statement.