Write a C++ program that operationalizes the (awful) sort shown in the following flow chart

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write a C++ program that operationalizes the (awful) sort shown in the following flow
chart (2 images seen below, the first image to look at is the one that says start and then follow into the other image). You must use this sorting algorithm, with variables scoped as indicated in the flow chart

The image contains two flowcharts related to a sorting procedure. Here's a detailed transcription and explanation:

**Flowchart on the Left (Main Procedure):**

1. **Start**: The process begins at the "Start" point.
2. **Step 1**: 
   - Input `n`, which is an integer.
3. **Step 2**: 
   - Define `a(i)` where `i` ranges from 1 to `n`, representing the integer array.
4. **Step 3**: 
   - Call the `sort` procedure.
5. **Step 4**: 
   - Output `a(i)` for `i` from 1 to `n`.
6. **Stop**: The process terminates.

**Legend for Main Procedure:**
- `n`: integer
- `a`: integer array
- `sort`: procedure name

**Flowchart on the Right (Sort Procedure):**

1. **Sort**: The procedure begins at the "sort" point.
2. **Step 1**: 
   - Loop for `j` from 1 to `n-1`.
3. **Step 2**: 
   - Check if `a(j)` is greater than `a(j+1)`.
   - If **Yes**, proceed to step 3.
   - If **No**, return to step 1 for the next iteration.
4. **Step 3**: 
   - Perform the `move` operation, likely to swap values of `a(j)` and `a(j+1)`.
5. **Return**: The process returns upon completion of sorting.

**Legend for Sort Procedure:**
- `n`: global integer
- `a`: global integer array
- `j`: integer
- `move`: procedure to swap or move elements

These flowcharts depict a classic sorting algorithm, likely a version of bubble sort, where elements are repeatedly compared and swapped if they are in the wrong order.
Transcribed Image Text:The image contains two flowcharts related to a sorting procedure. Here's a detailed transcription and explanation: **Flowchart on the Left (Main Procedure):** 1. **Start**: The process begins at the "Start" point. 2. **Step 1**: - Input `n`, which is an integer. 3. **Step 2**: - Define `a(i)` where `i` ranges from 1 to `n`, representing the integer array. 4. **Step 3**: - Call the `sort` procedure. 5. **Step 4**: - Output `a(i)` for `i` from 1 to `n`. 6. **Stop**: The process terminates. **Legend for Main Procedure:** - `n`: integer - `a`: integer array - `sort`: procedure name **Flowchart on the Right (Sort Procedure):** 1. **Sort**: The procedure begins at the "sort" point. 2. **Step 1**: - Loop for `j` from 1 to `n-1`. 3. **Step 2**: - Check if `a(j)` is greater than `a(j+1)`. - If **Yes**, proceed to step 3. - If **No**, return to step 1 for the next iteration. 4. **Step 3**: - Perform the `move` operation, likely to swap values of `a(j)` and `a(j+1)`. 5. **Return**: The process returns upon completion of sorting. **Legend for Sort Procedure:** - `n`: global integer - `a`: global integer array - `j`: integer - `move`: procedure to swap or move elements These flowcharts depict a classic sorting algorithm, likely a version of bubble sort, where elements are repeatedly compared and swapped if they are in the wrong order.
## Flowchart Explanation for Educational Website

### Move Procedure
- **Start Point**
  - The procedure begins with a predefined input.
  
- **Step 1**
  - `temp = a(j+1)`
  - `a(j+1) = a(j)`
  - This step involves storing a temporary value and shifting elements in an array.
  
- **Step 2**
  - Calls the `Findkay` procedure to locate a position.
  
- **Step 3**
  - `a(k) = temp`
  - Places the temporary value back in the array at position `k`.
  
- **Step 4**
  - Return to the calling procedure. The task is complete.

### Findkay Procedure
- **Start Point**
  - The procedure is initiated from the `Move` procedure.

- **Step 1**
  - `k = j`
  - `sw = 0`
  - Initialize variable `k` and set switch `sw`.

- **Step 2**
  - A loop begins: `while (k > 1) and (sw = 0)`
  - This evaluates if the conditions for the loop are still true.

- **Step 3**
  - `a(k-1) > temp`
  - If true, continues the loop; otherwise, moves to Step 5.
  
- **Step 4**
  - `a(k) = a(k-1)`
  - `k = k - 1`
  - Elements in the array are shifted.
  - Loop repeats or proceeds to Step 5.
  
- **Step 5**
  - `sw = 1`
  - Ends the loop and finalizes position determination.
  
- **Step 4 (Final)**
  - Return to `Move` procedure, completing the `Findkay` process.

### Diagram Explanation
The flowchart consists of two main processes: the `Move` and `Findkay` procedures. The diagrams illustrate the sequential steps, decisions, and iterative loops for sorting elements in an array. Each step is labeled, showing the operations performed, such as assignments, conditions, and loops. Diagram legends provide explanations for symbols and variable notations.
Transcribed Image Text:## Flowchart Explanation for Educational Website ### Move Procedure - **Start Point** - The procedure begins with a predefined input. - **Step 1** - `temp = a(j+1)` - `a(j+1) = a(j)` - This step involves storing a temporary value and shifting elements in an array. - **Step 2** - Calls the `Findkay` procedure to locate a position. - **Step 3** - `a(k) = temp` - Places the temporary value back in the array at position `k`. - **Step 4** - Return to the calling procedure. The task is complete. ### Findkay Procedure - **Start Point** - The procedure is initiated from the `Move` procedure. - **Step 1** - `k = j` - `sw = 0` - Initialize variable `k` and set switch `sw`. - **Step 2** - A loop begins: `while (k > 1) and (sw = 0)` - This evaluates if the conditions for the loop are still true. - **Step 3** - `a(k-1) > temp` - If true, continues the loop; otherwise, moves to Step 5. - **Step 4** - `a(k) = a(k-1)` - `k = k - 1` - Elements in the array are shifted. - Loop repeats or proceeds to Step 5. - **Step 5** - `sw = 1` - Ends the loop and finalizes position determination. - **Step 4 (Final)** - Return to `Move` procedure, completing the `Findkay` process. ### Diagram Explanation The flowchart consists of two main processes: the `Move` and `Findkay` procedures. The diagrams illustrate the sequential steps, decisions, and iterative loops for sorting elements in an array. Each step is labeled, showing the operations performed, such as assignments, conditions, and loops. Diagram legends provide explanations for symbols and variable notations.
Expert Solution
Step 1: Algorithm

Here, the task mentioned in the question is to write a code to implement the sorting flow chart provided. For the sake of understanding, this float chart can be converted to an algorithm as follows.


Algorithm:
1. Define three functions: Move, Findkay, and Sort.

Function Move(a, j, n):
   1. Create a temporary variable 'temp' and store a[j + 1] in it.
   2. Set a[j + 1] to a[j].
   3. Set a[j] to 'temp'.

Function Findkay(a, j, n):
   1. Initialize k to j and sw to 0.
   2. While k is greater than 0 and sw is 0:
      a. If a[k - 1] is greater than a[k], swap a[k - 1] and a[k], and set sw to 1.
      b. Decrement k by 1.
   3. Return k.

Function Sort(a, n):
   1. Iterate over j from 0 to n - 2:
      a. Call Findkay(a, j, n) and store the result in k.
      b. If k is not equal to j, call Move(a, j, n).

2. In the main function:
   1. Read the number of elements 'n' from the user.
   2. Create an array 'a' of size 'n' and read the elements from the user.
   3. Call Sort(a, n) to sort the array using the given algorithm.
   4. Print the sorted array.

3. End of the algorithm.


steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Algebraic Expressions
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education