# We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design.

## Presentation on theme: "We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design."— Presentation transcript:

Example: Calculate Function Values Problem: Suppose y = (x-1) 2 +10. What value will y be when x = 10? Your main code: x = 10; y = (x-1)*(x-1)+10; C code #include int main() { int x, y; x = 10; y = (x-1)*(x-1) + 10; printf("The result is: %i\n", y); return 0; }

scanf Get user input from the keyboard –Same format as printf, except put “&” in front of variable names scanf(“%i”, &count); –“&” means the "address of“ to store whatever the user enters into the memory address (location) of the variable Leaving out the & will cause your program to work incorrectly! And indeterminately!

Example #include int main() { int x, y; printf("What is the value for x? \n"); scanf("%i", &x); y = (x-1)*(x-1) + 10; printf("The result is: %i\n", y); return 0; }

Fibonacci number Sequence 1, 1, 2, 3, 5, 8, … Speed rabbits could breed in ideal circumstances –Start with a newly-born pair of rabbits –At the end of their second month a female can produce another pair of rabbits –Rabbits never die –Female always produces one new pair (one male, one female) every month from the second month Let F(n) mean "the number of pairs of rabbits at the start of the n-th month"

Fibonacci number

Calculate Fibonacci number int main() { int f0, f1, f2, f3, f4, f5; f0 = 0; f1 = 1; f2 = f0 + f1; f3 = f1 + f2; f4 = f2 + f3; f5 = f3 + f4; printf("%i %i %i %i %i\n", f1, f2, f3, f4, f5); return 0; }

Calculate Fibonacci number – cont. #include int main() { int f1, f2, f3, f4, f5, f6, f7, f8, f9, f10; f1 = 1; f2 = 1; f3 = f1 + f2; f4 = f2 + f3; f5 = f3 + f4; f6 = f4 + f5; f7 = f5 + f6; f8 = f6 + f7; f9 = f7 + f8; f10 = f8 + f9; printf("%i %i %i %i %i %i %i %i %i %i\n", f1, f2, f3, f4, f5, f6, f7, f8, f9, f10); return 0; }

Download ppt "We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design."

Similar presentations