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 =...
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;...
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
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {    ...
(C++)Change the following loop to a while loop: int i; for (i=0; i<10; i++) {     cout<<i<<endl; }
1. What will be the value of numbers[1] after the following code is executed? int[] numbers...
1. What will be the value of numbers[1] after the following code is executed? int[] numbers = {22, 33, 44}; for(int k = 0; k < 3; k++) { numbers[k] = numbers[k] + 5; } } 2. What will be the results of the following code? final int ARRAY_SIZE = 5; double[] x = new double[ARRAY_SIZE]; for(int i = 1; i <= ARRAY_SIZE; i++) { x[i] = 10.0; } 3.What is the value of numbers [3] after the following line...
1) What's the contents of array vals after the following code is executed? int[] vals =...
1) What's the contents of array vals after the following code is executed? int[] vals = {6, 1, 4, 5, 1}; int size = vals.length; for(int i = 0; i < size; i++) vals[i] = vals[i] * i; q) [ 6, 1, 4, 5, 1 ] b)[ 0, 1, 2, 3, 4] c)[0, 1, 8, 15, 4] d)[6, 2, 12, 20, 5] 2) Consider function foo defined below: static int[] foo(int vals[]) { int result[] = new int[vals.length - 1];...
9. Modify the quicksort and mergesort programs we learnt during the class to count the number...
9. Modify the quicksort and mergesort programs we learnt during the class to count the number of element comparisons for the two sorting methods. Use the following test drive to test them. public class Sorts {    int numComparisions = 0;    public void quicksort(int [] x, int l, int h)    { // your modifies codes go here    }    public void mergesort(int [] x, int l, int h)    { // your modifies codes go here   ...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT