CSE 130 Fall 2009 Home Work 1 Solutions 1. An algorithm should be (i) complete, (ii) precise, (iii) finite series of steps, and (iv) correct. 2. Assignment statement does all computational work in any programming language. 3. Comments are used to document the code without affecting the flow of the program. This makes understanding and maintaining the code much easier. There are two possible ways to make a comment: a. This type of comment begins with /* and ends with */. Any characters within are part of the comment, and therefore are ignored by the compiler. The comment can span multiple lines, however it cannot be nested. In other words, the first */ ends the comment regardless of how many /* there are. b. The other form of comment begins with // and any text after it until the end of the line, is part of the comment. Obviously, this type of comment cannot span multiple lines. 4. a.out 5. Syntax error is a problem with program "grammar". A compiler is not able to understand what exactly is intended by user. A logical error occurs when program terminates, but gives incorrect result. It is a problem with the flow/logic of the code. 6. if (labSection == 1) printf("Section 01\n"); else if (labSection == 2) printf("Section 02\n"); else printf("Invalid section\n"); 7. #include int main() { int m, n; int power, count; printf("Enter m and n (both greater than 0): "); scanf("%d %d", &m, &n); power = 1; for ( count = 1; count <= n; count++ ) power = power * m; printf("m raised to the power n = %d\n", power); return 0; } 8. 1 1 2 4 8 16 32 64 128 256 9. // Program to generate the first 15 Fibonacci numbers #include int main(void) { int prev1, prev2, next, i; prev1 = 0; prev2 = 1; printf("%i\n", prev1); printf("%i\n", prev2); for ( i = 2; i < 15; i++ ) { next = prev1 + prev2; /* calculate next Fib. number */ printf("%i\n", next); /* display the new number */ prev1 = prev2; /* save the previous and new numbers */ prev2 = next; } return 0; }