Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please write a c++ code with proper use of variable constants for my pseudo code. I am take a class that is teaching me flow

Please write a c++ code with proper use of variable constants for my pseudo code. I am take a class that is teaching me flow charts and pseudocode and I would like to see the actual code so that I can start preparing myself for next semesters classes. Here is the pseudo code:

// main module

Module main()

// Local variables

Constant Integer SIZE = 20

Declare Integer inputNumber, index, countS, countB

Declare Integer numbers1[SIZE], numbers2[SIZE], numbers3[SIZE],

// Get random numbers

For index = 0 to SIZE - 1

Set numbers1[index] = random(0, 9)

Set numbers2[index] = random(0, 9)

Set numbers3[index] = random(0, 9)

End For

// display numbers

For index = 0 to SIZE - 1

Display "Random array1 ", index + 1, " is: ", numbers1[index]

End For

For index = 0 to SIZE - 1

Display "Random array2 ", index + 1, " is: ", numbers2[index]

End For

For index = 0 to SIZE - 1

Display "Random array3 ", index + 1, " is: ", numbers3[index]

End For

// sort numbers

Call bubbleSort(numbers1, SIZE)

Call selectionSort (numbers2, SIZE)

Call insertionSort (numbers3, SIZE)

// display sorted numbers

For index = 0 to SIZE - 1

Display "Sorted array1 ", index + 1, " is: ",numbers[index]

End For

For index = 0 to SIZE - 1

Display "Sorted array2 ", index + 1, " is: ",numbers[index]

End For

For index = 0 to SIZE - 1

Display "Sorted array3 ", index + 1, " is: ",numbers[index]

End For

End Module

// The bubbleSort module accepts an array of Integers and array size

// and sorts array in ascending order

Module bubbleSort(Integer Ref array[], integer arraySize)

Declare index1, index2, count = 0

// bubblesort scores

For index1 = arraySize 1 to 0 Step -1

For index2 = 0 to index1 1

If array[index2] > array[index2 + 1] Then

Call swapI(array[index2], array[index2 + 1)

Set count = count + 1

End If

End For

End For

Display "Count of swaps in bubble sort: ", count

End Module

// The swapI module accepts two Integer elements

// and swaps their contents

Module swapI(Integer ref a, Integer ref b)

Declare Integer temp

// Swap a and b

Set temp = a

Set a = b

Set b = temp

End Module

// The selectionSort module accepts an array of integers

// and the array's size as arguments. When the module is

// finished, the values in the array will be sorted in

// ascending order.

Module selectionSort(Integer Ref array[], Integer arraySize)

// startScan will hold the starting position of the scan.

Declare Integer startscan, count = 0

// minIndex will hold the subscript of the element with

// the smallest value found in the scanned area.

Declare Integer minIndex

// minValue will hold the smallest value found in the

// scanned area.

Declare Integer minValue

// index is a counter variable used to hold a subscript.

Declare Integer index

// The outer loop iterates once for each element in the

// array, except the last element. The startScan variable

// marks the position where the scan should begin.

For startScan = 0 To arraySize 2

// Assume the first element in the scannable area

// is the smallest value.

Set minIndex = startScan

Set minValue = array[startScan]

// Scan the array, starting at the 2nd element in

// the scannable area. We are looking for the smallest

// value in the scannable area.

For index = startScan + 1 To arraySize - 1

If array[index] < minValue Then

Set minValue = array[index]

Set minIndex = index

End If

End For

// Swap the element with the smallest value

// with the first element in the scannable area.

Call swapI(array[minIndex], array[startScan])

Set count = count + 1

End For

Display "Count of swaps in selection sort: ", count

End Module

Module insertionSort(Integer Ref array[], Integer arraySize)

// Loop counter

Declare Integer index, count = 0

// Variable used to scan through the array.

Declare Integer scan

// Variable to hold the first unsorted value.

Declare Integer unsortedValue

// The outer loop steps the index variable through

// each subscript in the array, starting at 1. This

// is because element 0 is considered already sorted.

For index = 1 To arraySize 1

// The first element outside the sorted subset is

// array[index). Store the value of this element

// in unsortedValue.

Set unsortedValue = array[index]

// Start scan at the subscript of the first element

// outside the sorted subset.

Set scan = index

// Move the first element outside the sorted subset

// into its proper position within the sorted subset.

While (scan> 0 AND array[scan-l] < array[scan))

Call swapI(array[scan-l), array[scan])

Set scan = scan 1

Set count = count + 1

End While

// Insert the unsorted value in its proper position

// within the sorted subset.

Set array[scan] = unsortedValue

End For

Display "Count of swaps in insertion sort: ", count

End Module

Output should be as below:

8 Sorting Benchmarks (bubble, selection & insertion sort)

The following represents the output from 3 separate files: BubbleSort, SelectSort and InsertionSort.

Don't ask the user to enter any numbers, but 'hard code' the values in the "Original order" as shown.

Original order: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 Bubble Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 Number of location swaps: 89

Original order: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 Selection Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 Number of location swaps: 19

Original order: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 Insertion Sorted: 5 11 12 22 22 26 28 32 39 45 51 53 56 62 74 78 79 87 90 93 Number of location swaps: 19

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals, Design, and Implementation

Authors: David M. Kroenke, David J. Auer

14th edition

133876705, 9781292107639, 1292107634, 978-0133876703

More Books

Students also viewed these Databases questions

Question

5. How can we use language to enhance skill in perceiving?

Answered: 1 week ago

Question

What actions might have prevented Bobs resignation?

Answered: 1 week ago