Download presentation
Presentation is loading. Please wait.
Published byMax Wile Modified over 10 years ago
1
We Have Learned main() { … } Variable –Definition –Calculation –Display We can do some real programming! –Think about your solution design –Express design in Pseudocode (in between C and English) –Code your design into C
2
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; }
3
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!
4
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; }
5
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"
6
Fibonacci number
7
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; }
8
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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.