60 likes | 510 Views
Do these do the same thing?. #include < stdio.h > int main(void) { int num[9] = {10,20,30,40,50,60,70,80,90} ; int calc=0, i ; for ( i =0;i<9;i++) { calc = calc+num [ i ]; } calc = calc/9; printf (“ calc = %d<br>”, calc ); }. #include < stdio.h >
E N D
Do these do the same thing? #include <stdio.h> int main(void) { intnum[9]= {10,20,30,40,50,60,70,80,90}; int calc=0, i; for (i=0;i<9;i++) { calc = calc+num[i]; } calc = calc/9; printf(“calc= %d\n”, calc); } #include <stdio.h> int main(void) { intnum1= 10; int num2 = 20; int num3 = 30; int num4 = 40; int num5 = 50; int num6 = 60; int num7 = 70; int num8 = 80; int num9 = 90; int calc; calc = num1+num2+num3+num4+num5; calc = calc + num6+num7+num8+num9; calc = calc/9; printf(“calc= %d\n”, calc); } YES!
#include <stdio.h> int main() { int c[10]={0,1,1}; printf("c=%d,%d,%d,%d\n", c[0],c[1], c[2], c[9]); } 2. c=0,0,0,0 1. c=0,1,1,0 4. Gives error 3. c=0,1,1,1
#include <stdio.h> int main() { int c[10]={0,1,1}; printf("c=%d,%d,%d,%d\n", c[0],c[1], c[2], c[10]); } 2. c=0,0,0,0 1. c=0,1,1,0 4. Be careful, you are outside the bounds of the array – could give runtime error 3. c=0,1,1,1 The array ’c’ has a dimension (size) of 10, but its index starts at 0 and goes to 9
#include <stdio.h> int main() { float num[5]; inti; for (i=0;i<5;i++) { num[i] = 0.; } num[0] = 2.; num[4] = 4.; for(i=1;i<4;i++) { num[i] = (num[i+1]+num[i-1])/2.; } for(i=0;i<5;i++) { printf("num=%.2f\n", num[i]); } } 1. num=2.00 num=2.00 num=2.50 num=2.25 num=4.00 2. num=2.00 num=1.00 num=0.50 num=2.25 num=4.00 3. num=2.00 num=1.00 num=1.50 num=2.25 num=0.00 4. num=0.00 num=1.00 num=0.50 num=2.25 num=4.00
1. num=6 num=7 num=7 num=2 #include <stdio.h> #define SIZE 8 int main(void) { int subscript[4] = {2,4,6,7}; int num[8] = {1,4,6,2,7,2,7,2}; inti; for (i=0;i<4;i=i+1) { printf("num=%d\n",num[subscript[i]]); } return 0; } 2. num=2 num=4 num=7 num=2 3. num=6 num=7 num=3 num=1 4. num=0 num=0 num=0 num=0
YOUR HOMEWORK SHOULD YOU CHOOSE TO ACCEPT IT IS TO: Do your in-class programming example on Fibonacci series: Remember: 1 1 2 3 5 8 13 21 … And to: 2. Have a NICE BREAK!