Quiz 6: Iteration and Switch statements

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

A loop is a control structure that causes certain statements to execute over and over. True or false?

True

To read data from a file of an unspecified length, an EOF-controlled loop is a good choice. True or false?

True

Walk through the following code fragment and write the exact output printed to standard output. Assume variable value is defined as type int and initialized with value 17. switch(value%4) { case 0: printf("zero") case 1: printf("one") case 2: printf("two"); break; case 3: printf("three"); break; }

onetwo

Walk through the following code fragment and write the exact output printed to standard output. Assume variable val is defined as type int and is initialized with a value of -5. while (val<13) val++; printf("%d", val);

13

Walk through the following code fragment and write the exact output printed to standard output. Assume val is defined to be a variable of type int and is initialized with the value -5. while (val++<13); printf("%d", val);

14

Walk through the following code fragment and write the exact output printed to standard output. Assume variable count is defined as type int and initialized with value 1. do { printf("%d ", count*(count-2)); } while(count++<=5);

-1 0 2 8 15 24

Walk through the following code fragment and write the exact output printed to standard output. Assume variable val is of type int and is initialized with value -5. while (val>5) val+=2; printf("%d", val);

-5

Walk through the following code fragment and write the exact output printed to standard output. Assume variable value is of type int and is initialized to value 5. switch(value) { case 1: case 2: value+=2; break; case 4: value+=1; case 5: value*=2; case 6: value+=5; break; default: value-=1; } printf("%d", value);

15

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and count are defined as type int and are initialized with values 100 and 1, respectively. while (count<100) { i--; ++count; } printf("%d%d", i, count);

1,100

Walk through the following code fragment and write the exact output printed to standard output. Assume all variables are defined as type int. for (i=19683,x=19683,y=0; i>=1; i/3,++y); printf("%d", y);

10

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and count are defined as type int and both variables are initialized with the value 0. while (i++<10) ++count; printf("%d", count);

10

Walk through the following code fragment and write the exact output printed to standard output. Assume variables x, y, and z are defined as int type. In addition, variables x and y are initialized with values 4 and 5, respectively. z=y+6; do { printf("%d", z); x+=7; } while(((z-x)%4));

11 18 25

Walk through the following code fragment and write the exact output printed to standard output. Suppose that the input to the program is 5 3 8. Assume all variables in the code fragment are of type int. scanf("%d%d%d", &a, &b, &c); for (j=1; j<a; ++j) { d = b+c; b = c; c = d; printf("%d ", c); }

11 19 30 49

Walk through the following code fragment and write the exact output printed to standard output. Assume variables x, y, and z are defined as type int. Also assume that variables x and y are initialized with values 4 and 5, respectively. z=y+6; while (((z-x)%4)!=0) { printf("%d",z); z+=7; }

111825

Walk through the following code fragment and write the exact output printed to standard output. for (int count=12; count>=0; count-=2) { if (count%5==0) break; printf("%d", count); }

12

Walk through the following code fragment and write the exact output printed to standard output. for (int count=12; count>=0; count-=2) { if (count%5==0) { count++; continue; } printf("%d ", count); }

12 9 7 4 2

Walk through the following code fragment and write the exact output printed to standard output. Assume all variables are defined as type int. for (i=0, value=0; i<=20; ++i) { if (i%2==0&&i>10) value -=1; else if (i%2==0&&i<=10) value+=i; else value+=i*i; } printf("%d", value);

1280

Walk through the following code fragment and determine the value written to standard output. Now, write this value in the box below. // definition of function foo int foo(int b, int n) { int i, p; for (i=1,p=1; i<=n; ++i) p*=b; return p; } // calling foo() in main() printf("%d", food(6,4));

1296

Walk through the following code fragment and write the exact output printed to standard output. Assume all variables in the code fragment are of type int. Suppose that the input to the program is 38 35 72 24 -1. scanf(" %d %d", &sum, &num); for (j=1; j<=3; ++j) { sum += num; scanf(" %d", &num); } printf("%d", sum);

169

Walk through the following code fragment and write the exact output printed to standard output. Assume variable value is of type int and is initialized to value 2. switch(value) { case 3: value+=3; case 1: value+=1; break; case 5: value+=5; case 4: value+=4; } printf("%d", value);

2

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and value are defined as type int and both are initialized with the value 0. while (i<=20) { if (i%2==0&&i<=10) value+=i*i; else if (i%2==0&&i>10) value+=i; else value-=i; i++; } printf("%d", value);

200

Walk through the following code fragment and write the exact output printed to standard output. Suppose that the input to the program is 58 23 46 75 24 -1. Assume variables num and sum are defined as type int. scanf(" %d %d", &sum &num); for (; num != -1; sum+=num) scanf(" %d", &num); printf("%d", sum);

202

Walk through the following code fragment and write the exact output printed to standard output. Suppose that the input to the program is 58 23 46 75 24 -1. Assume num and sum are variables of type int. scanf(" %d %d", &sum &num); for (; num != -1; scanf(" %d", &num)) sum+=num; printf("%d", sum);

226

Walk through the following code fragment and write the exact output printed to standard output. Suppose that the input to the program is 58 23 46 75 98 150 12 176 145 -999. Assume variable num is of type int. for (scanf("%d", &num); num != -999; scanf("%d", &num), printf("%d ", num%25));

23 21 0 23 0 12 1 20 -24

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and j are defined as type int. for (i=0, j=0; i<5; ++i) { j = 2*j+1; } printf("%d", j);

26

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and j are defined as type int. for (i=0, j=2; i<=5; ++i, j=2*j+3); printf("%d", j);

317

Walk through the following code fragment and write the exact output printed to standard output. Suppose that the input to the program is 98 150 146 75 24 -1. Assume variables num and sum are of type int. scanf(" %d %d", &sum, &num); for (; num != -1; scanf(" %d", &num), sum+=num); printf("%d", sum);

342

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and count are defined as type int and are initialized with the values 5 and 0, respectively. while (--i>0) ++count; printf("%d", count);

4

Create a text file containing the following integral values: 36 -350 712 -249 -10. Walk through the following code fragment and write the exact output printed to standard output when the text file is redirected as input to the program. Assume variables num and sum are defined as type int. scanf("%d%d", &sum, &num); for (scanf("%d",&sum); EOF!=scanf("%d",&num); sum+=num); printf("%d", sum);

453

Walk through the following code fragment and write the exact output printed to standard output. Suppose that the input to the program is 98 150 146 75 24 -1. Assume variables num and sum are of type int. scanf(" %d %d", &sum, &num); for (; num != -1; sum+=num, scanf(" %d", &num)); printf("%d", sum);

493

Walk through the following code fragment and write the exact output printed to standard output. Assume i and j are defined as variables of type int. for (i=0, j=0; i<5; ++i); { j = 2*j+i; } printf("%d", j);

5

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and count are defined as type int and are initialized with values 5 and 0, respectively. while (i-->0) ++count; printf("%d", count);

5

Walk through the following code fragment and write the exact output printed to standard output. Assume variables i and count are defined as type int and initialized to values 1 and 0, respectively. while (i++<=5) ++count; printf("%d", count);

5

Walk through the following code fragment and write the exact output printed to standard output. Assume variables x and y are defined as type int and are initialized with values 5 and 50, respectively. do x += 10; while (x<y); printf("%d", x);

55

Walk through the following code fragment and write the exact output printed to standard output. Assume variable value is of type int and is initialized to value 3. switch(value) { case 3: value+=3; case 1: value+=1; break; case 5: value+=5; case 4: value+=4; } printf("%d", value);

7

Walk through the following code fragment and write the exact output printed to standard output. Assume x and y are defined as type int and initialized with values 5 and 20, respectively. do x += 2; while (x>=y); printf("%d", x);

7

Walk through the following code fragment and write the exact output printed to standard output. Suppose that the input to the program is 58 23 46 75 98 150 12 176 145 -999. Assume variable num is defined as type int. for (scanf("%d", &num); num != -999; printf("%d ", num%25), scanf("%d", &num));

8 23 21 0 23 0 12 1 20

Walk through the following code fragment and write the exact output printed to standard output. Assume variables x and y are defined as type int and are initialized with values 5 and 80, respectively. do x *= 2; while (x<y); printf("%d", x);

80

Walk through the following code fragment and write the exact output printed to standard output. Assume variable x is of type int and is initialized to value 6. if (x>0) switch(x) { case 1: x+=3; case 3: x+=1; break; case 6: x+=6; case 8: x*=8; break; default; x-=1; } else x+=2; printf("%d", x);

96

Consider the following code fragment where variable j is defined as int type and initialized with value 0. while (j<10) ++j; The while loop terminates when j > 10. True or false?

False

Walk through the following code fragment and write the exact output printed to standard output. Assume variable x is defined as type int and initialized with value 1. switch(x<=2) { case 0: printf("Draw"); break; case 1: printf("Win"); break; case 2: printf("Lose"); break; }

Win

Walk through the following code fragment and write the exact output printed to standard output. Assume variable ch is defined as type char and is initialized with character constant 'B'. while ('A'<=ch&&ch<'Z') ++ch; printf("%d", ch);

Z


Kaugnay na mga set ng pag-aaral

MSN 377: Ch 26 Workbook Questions

View Set

earth science - earthquakes and earths interior CH 8

View Set

Lecture 16: Nuclear Energy and Radioactive Waste.

View Set

Life Insurance Basics Chapter Quiz

View Set

Simple IRA's / SEP IRA's / Retirement Funding

View Set

TEST #2 : Strategic Management Ch. 8 & Ch. 9

View Set