Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write a c++ code with proper use of variable constants for my pseudo code

ID: 3732014 • Letter: P

Question

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 semester’s 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

Explanation / Answer

#include<iostream>

using nameapce std;

void swap(int a,int b){

int temp=a;

a=b;

b=temp;

}

void bubbleSort(int array[],int size){

int index1,index2,count=0;

for(index1=size-1;index1>=0;index--){

for(index2=0;index2<index1;index2++){

if(array[index2]>array[index2+1]){

swap(array[index2],array[index2+1]);

count=count+1;

}

}

}

cout<<"Count of swaps in bubble sort: "<<count<<endl;

}

void selectionSort(int array[],int size){

int startScan,count=0;

int minIndex;

int minValue;

int index;

for(startScan=0;startScan<=size-2;startScan++){

int minIndex=startScan;

int minValue=array[startScan];

for(index=startScan+1;index<=size-1;index++){

if(array[index]<minValue){

minValue=array[index];

minIndex=index;

}

}

swap(array[minIndex],array[startScan])

count=count+1;

}

cout<<"Count of swaps in selection sort: "<<count<<endl;

}

void insertionSort(int array[],int size){

int index,count=;0;

int scan;

int unsortedValue;

for(index=1;index<=size;index++){

int unsortedValue=array[index];

int scan=index;

while(scan>0 && array[scan-1]<array[scan]){

swap(array[scan-1],array[scan]);

scan=scan-1;

count=count+1;

}

array[scan]=unsortedValue;

}

cout<<"Count of swaps in insertion sort: "<<count;

}

int main(){

int size=20;

int inputNumber,index,countS,countB;

int numbers1[size];

int numbers2[size];

int numbers3[size];

for(int index=0;index<size;index++){

numbers1[index]=rand()%10; //Since dividing by 10

numbers2[index]=rand()%10; //The remainder will be

numbers3[index]=rand()%10; //from 0 to 9.

}

for(index=0;index<size;index++){

cout<<"Random array1 "<<index+1<<"is: "<<numbers1[index];

}

for(index=0;index<size;index++){

cout<<"Random array2 "<<index+1<<"is: "<<numbers2[index];

}

for(index=0;index<size;index++){

cout<<"Random array3 "<<index+1<<"is: "<<numbers3[index];

}

bubbleSort(numbers1,size);

selectionSort(numbers2,size);

insertionSort(numbers3,size);

for(index=0;index<size;index++){

cout<<"Sorted array1 "<<index+1<<"is: "<<numbers1[index];

}

for(index=0;index<size;index++){

cout<<"Sorted array2 "<<index+1<<"is: "<<numbers2[index];

}

for(index=0;index<size;index++){

cout<<"Sorted array3 "<<index+1<<"is: "<<numbers3[index];

}

}