Question

6. First try to predict how many times will the following for loops execute? What is the output for (i = 17; i>1; i = i/2) fo

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 6

i is initialized to 17 and reducing by half each time so first for loop will be running for i = 17, 8, 4, 2,

For i = 17, inner for loop will be running for j = 2, 5, 8, 11, 14 => Total 5 times

For i = 8, inner for loop will be running for j = 2, 5 => Total 3 times

For i = 4, inner for loop will be running for j = 2 => Total 1 times

For i = 2,inner for loop will not run.

So, total time for loops execute = 5 + 2 + 1 = 8 times

Answer 7

#include <iostream> using namespace std; void print(int arr[], int size) { int i; for (i = 0; i < size; i++) cout « arr[i] «

Code

#include <iostream>
using namespace std;

void print(int arr[], int size)
{
   int i;
   for (i = 0; i < size; i++)
       cout << arr[i] << " ";
   cout << endl;
}

// Bubble sort
void bubbleSort(int arr[], int n)
{
   int i, j;
   // running bubble sort only for 2 passes
   for (i = 0; i < 2; i++)
   {
       for (j = 0; j < n - i - 1; j++)
       {
           if (arr[j] > arr[j + 1])
           {
               int temp = arr[j];
               arr[j] = arr[j + 1];
               arr[j + 1] = temp;
           }
       }
       // After each pass printing the result
       cout << "\nBubble Sort After Pass " << i << ": ";
       print(arr, n);
   }
}

int main()
{
   int arr[] = { 20, 32, 50, 48, 19, 10, 5, 8, 12};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Initially unsorted array : ";
   print(arr, n);
   bubbleSort(arr, n);
   return 0;
}

Test Output

Initially unsorted array 20 32 50 48 19 10 5 8 12 Bubble Sort After Pass 0: 20 32 48 19 10 5 8 12 50 Bubble Sort After Pass 1

Answer 8

#include <iostream> using namespace std; void print(int arr[], int size) |{ int i; for (i = 0; i < size; i++) cout <« arr[i]

Code

#include <iostream>
using namespace std;

void print(int arr[], int size)
{
   int i;
   for (i = 0; i < size; i++)
       cout << arr[i] << " ";
   cout << endl;
}

// Selection sort
void selectionSort(int arr[], int n)
{
   int i, j, min;
   // running selection sort only for 2 passes
   for (i = 0; i < 2; i++)
   {
       // Find the minimum element in unsorted array
       min = i;
       for (j = i + 1; j < n; j++)
       {
           if (arr[j] < arr[min])
               min = j;
       }
       // Swap the found minimum element with the first element
       int temp = arr[i];
       arr[i] = arr[min];
       arr[min] = temp;

       // After each pass printing the result
       cout << "\Selection Sort After Pass " << i << ": ";
       print(arr, n);
   }
}

int main()
{
   int arr[] = { 20, 32, 50, 48, 19, 10, 5, 8, 12};
   int n = sizeof(arr) / sizeof(arr[0]);
   cout << "Initially unsorted array : ";
   print(arr, n);
   selectionSort(arr, n);
   return 0;
}

Test Output

Initially unsorted array : 20 32 50 48 19 10 5 8 12 5 32 50 48 19 10 20 8 12 5 8 50 48 19 10 20 32 12 Selection Sort After Pa

Add a comment
Know the answer?
Add Answer to:
6. First try to predict how many times will the following for loops execute? What is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ help Write a program that obtains the execution times for the following three code segments...

    c++ help Write a program that obtains the execution times for the following three code segments with the different n. code 1: for(int i = 0; i <= n; i++)   { x = i; } code 2: for (int i = 0; i <= n; i++) { for (int j = 0; j <= n; j++) { x = i + j; } } code 3: for (int i = 0; i <= n; i++) { for (int j =...

  • 6. (4 points) The following code for insertion sort has been modified to print out the...

    6. (4 points) The following code for insertion sort has been modified to print out the sorted component of the array during the sort. What will be the output of running the main method of the following code? Try to trace by hand and then verify by executing the code public class Insertion ( public static void sort (String[] a) f int n- a.length; for (int i-1; i < n; i++) f for (int j i; j 0; j--) if...

  •   Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first...

      Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion:    Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) {     j=i;     temp=arr[i];                while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...

  • PART A: Predict how many lines of output will be displayed by the following program fragment:...

    PART A: Predict how many lines of output will be displayed by the following program fragment: i = 0 ​​ do { ​​for (j = 0; j < 4; j = j + 1) ​​printf("%d\n", i + j); ​​i = i + 1; ​​} while (i < 5); PART B: Then write either a C or a C++ program to verify your prediction. ​​ I know it will output 20 lines, the full C++ code is what I need.

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • 11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I...

    11:10 PM No SIM mycourselink.lakeheadu.ca Modify the following bubble sort program to improve its performance I Fig. 6.15: fig06 15.c 2 Sorting an array's values into ascending order. Finclude <stdio.h> 4 #define SIZE 10 6 function main begins program execution 7 int main(void) 9 initialize a lo int a CSIZEJ 12, 6, 4, 8, 10, 12, 89, 68, 45, 37) 12 putsc Data items in original order output original array IS for (size t i 0; i SIZE ++i) a[i])...

  • whats the answers For the following code, indicate how many times the loop body will execute...

    whats the answers For the following code, indicate how many times the loop body will execute for the following input values: 2 1 0? userNum - 3 while (userNum > 0) { // Do something // Get user um from input } 3 Given the following code, how many times will the inner loop body execute? int row: int col; for (row = 0; row < 2; row - row + 1) { for (col = 0; col < 3;...

  • Read the following code. Is the description of the order of execution (listed below the code)...

    Read the following code. Is the description of the order of execution (listed below the code) correct? #include <stdio.h> void printPizzaArea(double pizza Diameter) { double pizzaRadius, pizzaArea; const double pival = 3.14; pizzaRadius = pizzaDiameter / 2.0; pizzaArea = pival * pizzaRadius * pizzaRadius; printf("Pizza area is: %lf inches.\n", pizzaArea); } int main(void) { printPizzaArea(12.0); printPizzaArea(16.0); return 0; } 1. main starts executing 2. main calls printPizzaArea function, passing the diameter of a pizza 3. printPizzaArea function executes, computing and...

  • Write a C program for Linux called pipes.c that does the following: In the main() function,...

    Write a C program for Linux called pipes.c that does the following: In the main() function, it creates a pipe using the pipe() function, then creates two child processes with fork(). Child 1 redirects stdout to the write end of the pipe and then executes with execlp() the "ps -aux" command. Child 2 redirects its input from stdin to the read end of the pipe, then it executes the "sort -r -n -k 5" command. After creating both children, the...

  • What is the java code for this. The goal of this lab is to write two...

    What is the java code for this. The goal of this lab is to write two programs, Summation and Prime, that execute simple tasks. The first computes the summation of integers within a range with a gap that the user specifies. The second tests whether the integer that the user enters is a square and if not, whether it is composite or prime. Summation Let us see some execution examples first, to get the sense of how the program works....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT