Question

In: Computer Science

Determine the execution count of the following programs: (number of loops executed?) int i = 10;...

Determine the execution count of the following programs: (number of loops executed?)

int i = 10;

while(i > 0)

{

for(int p = 5; p < 14; p++)

b{

System.out.println(i + p)

p++;

}

i--;

}

Solutions

Expert Solution

  • Outer loop(while loop) executes 10 times(When i is 10,9,8,7.......3,2,1)
  • inner loop(for loop) executes 5 times for each i value
    • Here p is increment twice in each iteration
    • One is in the for loop itself and other is in the loop body
    • First p is 5 which is less than 14, loop body executes
    • Then p increment to 7 which is less than 14, loop body executes
    • Then p increment to 9 which is less than 14, loop body executes and so on
    • Execution of loop body continue as long as p<14
    • So loop executes when p is 5,7,9,11,13.Total 5 times.
  • So execution count of the program is 10x5=50

Screen shot of code and output for more clarity


Related Solutions

Show the output of the following code segment. int count=0;                         for (int i=2; i <=...
Show the output of the following code segment. int count=0;                         for (int i=2; i <= 4; i++ ) {                                     StdOut.println(i);                                     for (int j=1; j <3; j++) {                                                 count++;                                                 StdOut.println(i +" " + j +" "+ count);                                     }                         } count i j I print 0 2 1 3 1 1 1 2 3 2 2 3 3 3 3 4 3 Show the output of the function call statements shown.             double x =...
int count = 10; while (count >= 0) { cout << count << endl; count =...
int count = 10; while (count >= 0) { cout << count << endl; count = count + 3; } How many times will this loop be executed? A. Endless loop B. 3 times C. 0 times D. Once
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter)...
consider execution of the following switch statement: int Enter = 10; cin >> Enter; switch (Enter) { case 1: Enter = -4; case 2: Enter = -6; case 4: break; case 6: Enter = -8; break; default: Enter = -1; } What would the value of Enter be after execution of this code if the value read for Enter were 4? -4,-6,-8 or none
Without using a computer determine the output for each of the loops. 1.             int x...
Without using a computer determine the output for each of the loops. 1.             int x = -2;             while (x <= 2) {                            System.out.println(x * x);                            x = x + 1;             } 2.               int x = 1;               int y = x;             while (x <= 3) {                            System.out.println(x + y);                              y = x * 5;                              x = x + 2;               } 3.               int x = 0;...
Without using a computer determine the output for each of the loops. 1. int x =...
Without using a computer determine the output for each of the loops. 1. int x = -2; while (x <= 2) { System.out.println(x * x); x = x + 1; } 2. int x = 1; int y = x; while (x <= 3) { System.out.println(x + y); y = x * 5; x = x + 2; } 3. int x = 0; int y = -2; while (x <= 2) { y = x * x + 1;...
The following is an infinite loop: for (int i = 0; i < 10; i++); System.out.println(i...
The following is an infinite loop: for (int i = 0; i < 10; i++); System.out.println(i + 4); TRUE OR FALSE?
JAVA programing language: What is printed when the following code is executed? int columns; int rows;...
JAVA programing language: What is printed when the following code is executed? int columns; int rows; for(rows = 1; rows < 2; ++rows) { for(columns = 1; columns < 3; ++columns) { System.out.print("x"); } System.out.println(): } select one: A) xx B) xxx xxx C) x x D) xx xx xx
1. Implement a method called count with the following signature: public int count(); such that the...
1. Implement a method called count with the following signature: public int count(); such that the method returns the number of nodes currently contained in the list. The method must compute this number by traversing the entire list. 2. Implement a method called sndLast with the following signature: public E sndLast(); such that the method returns the data stored at the second-to-last node of the linked list. If the size of the list is less than 2, then the method...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n,...
Convert the following C function to the corresponding MIPS assembly procedure: int count(int a[], int n, int x) { int res = 0; int i = 0; int j = 0; int loc[]; for(i = 0; i != n; i++) if(a[i] == x) { res = res + 1; loc [j] = i; j = j+1} return res, loc; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT