CSE 130 Fall 2009 Lab 2 NOTE: It is very important for this course that you obtain a sparky account (ic.sunysb.edu) for completing assignments. It is same as your NetID. If it is not working, visit the SINC site in the main library building and speak to a representative to resolve the issue. TA will help you with the following skills: ---------------------------------------------------------------------- 1. Review of lab1.c program (Same as Lab1 document) a. If you created the directory cse130, then change your working directory to cse130 (command cd). b. The program is given here, but use your name, and your id number. You may use PICO to type the following program in. But it will save you a lot of time if you just cut and paste this from the CSE 130 webpage to your PICO window. // lab1.c // CSE 130 Fall 2009. Lab 1, Sept 9, 2009. // Shaunak Pawagi, Id-No: xxx-yy-zzzz // Sample program to print two lines. #include int main (void) { printf ("These lines are printed by\n"); printf ("the lab1.c program.\n"); return 0; } c. Use `ls' to see that it is there. If not, follow instructions from Lab1 document to create this file. d. Compile the C program by typing the Unix command gcc lab1.c (Typically the compiler produces some error messages here, indi- cating that some statements are not correct; if so go back to step f and fix the the errors.) e. Use `ls' to see that the gcc command created a new file named a.out f. Run your program by typing the command ./a.out g. You should see it output the two lines of text. 2. Handing in e-copy of your C program. i. Your first C program is in file lab1.c. Please make sure that every program you submit, has your name, ID and program number right at the beginning as shown in the example (as comments). j. Use command `more lab1.c' (without quotes) to display it on your screen. Check that it has your name, ID etc. at the beginning. k. Execute ~cse130/submit command to hand an e-copy of this file. l. Respond to questions, and when asked for file-name, type ./lab1.c m. If your submission is successful, you will get an acknowledgement right away. n. Use the option to check on your submission. 3. Another program lab2.c o. Open a pico window. Cut and paste lab2.c program from the web- page. Save it as lab2.c. p. Compile your program using gcc and execute using ./a.out command. q. It should output 1 2 3 4 5 6 7 8 9 10. // lab2.c // Sample program to print: 1 2 3 4 5 6 7 8 9 10. // Example for class CSE130. // We print i using printf function. // Use of %d will be explained later. // There is a space before % character. #include main ( ) { int i = 1; while (i <= 10) { printf(" %d", i); i=i+1; } printf(".\n"); } r. Program lab2.c has a while loop. When executed, this loop 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 combination <= is how one types "less than or equals". s. The int i=1; collapses two statements: int i; means that i is declared to be an integer variable, and i=1; is an assignment statement setting an initial value of one to i. t. Note that main has not been declared as an integer function. Also return 0; statement is missing. Some C compiler assume that main () is an integer function, and there is no need for explicit declaration. But if you declared main to be an integer function, then return 0; statement is required at the end. Also there is no need to have (void) declaration for main; it is optional. 4. Modifications to the above program u. Modify the program so it outputs the positive numbers counting by fives up to 100, namely: 5, 10, 15, 20, ..., 100. Initialize i to 5. Change the condition to (i <= 100). Increment i by 5 inside the loop. You DO NOT have to modify the printf statement. Compile your program and execute it (type ./a.out). v. Another Modification: Change the program so it counts down from 20 to 0, printing only the even numbers: 20 18 16 14 12 10 8 6 4 2 0. Set i = 20; Let condition be (i >= 0), and have i = i-2; in the body of the loop. Here also you do not need to modify the printf statement. Again, compile your program and execute it. 5. Infinite loop: w. Now we will study an infinite or unterminated loop. x. Note that i has been set to 20. Keep the condition (i >= 0) as is. Replace i = i-2; by i = i+2; y. Since i is 20 to start with, program prints 20, 22, 24, 26, ...... but does not end, since i will remain above 0 all the time. z. To stop execution type ^c (control-c). This will terminate the program. 6. Reading input from keyboard: - Fix the program by changing i = i+2; back to i = i-2; - In the above program we count down from 20. But the value 20 is assigned to variable i in the program. But we can get it from user as well, by using a scanf statement. - Remove i=20; statement and insert these two in its place. printf("Type in an integer value for i: "); scanf("%d", &i); - Compile your program using gcc and execute. During execution, your program will print the message Type in an integer value for i: and will wait for your response. - Type 10 and see what happens. Execute again. Type 45, and see what it prints.