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

The following is a c++ program designed to take a series of numbers from a user,

ID: 3667549 • Letter: T

Question

The following is a c++ program designed to take a series of numbers from a user, then be able to sort and search through the numbers.

I am having an issue where the count value that keeps track of the current size of the array is not keeping track. I have an some extra cout functions to show that the count value isn't continuous for all of the functions. I am not allowed to alter any part of the header file, shown below.

Also, I am not sure how to use 'q' to stop the program, when the program is only accepting intergers. An answer to this issue would be fantastic! Below is the code.

Header file:

#ifndef INTSEQUENCE_H
#define INTSEQUENCE_H

const int INITIAL_CAPACITY = 5;

class IntSequence
{
public:
// create an iteger array with capacity = INITIAL_CAPACITY
IntSequence(); // default constructor
// create an iteger array with capacity = in_capacity
IntSequence(int in_capacity);

// insert item into the end of the array; count increased by 1
void insert(int item);

// display all the items in the array
void print();

// sort the sequence into non-decreasing order
// using Selection Sorting algorithm
void selection_sort();

// sort the sequence into non-decreasing order
// using Insertion Sorting algorithm
void insertion_sort();

// sort the sequence into non-decreasing order
// using Bubble Sorting algorithm
void bubble_sort();

// shuffle the items in the sequence
// generates a random permutation of array elements
void shuffle();

// search a target key in the array:
// if found return the index number; if not found return -1
int sequential_search(int key);

// sort the array into non-decreasing order first, then
// search a target key in the array:
// if found return the index number; if not found return -1
int binary_search(int key);

// destructor
~IntSequence();
// menu for searching functions
void searchmenu();
// menu for sorting functions
void sortmenu();
// main menu
void menu();
int check();
void swap(int A, int B);

private:
int capacity; // the capacity of the array
int count; // actual num of items in the array
int* seq; // the pointer points at the first item in the array
};

#endif

MAIN PROGRAM!

#include <iostream>

#include "Project1.h"

using namespace std;

int main(){

        IntSequence b;

        b.menu();

}

void IntSequence::menu(){

        IntSequence b;

        int Userval=0;

        int item;

        while(Userval!=6){

        cout<< count <<endl;

        cout<< capacity <<endl;

        cout << "1. Read" << endl;

        cout << "2. Print" << endl;

        cout << "3. Sort" <<endl;

        cout << "4. Shuffle" <<endl;

        cout << "5. Search" <<endl;

        cout << "6. Quit" <<endl;

        cout << "Option: " ;

                Userval=b.check();

                if (Userval==1)

                        while(item!=-1){

                                cout<<"Enter the next element (Enter 'q' to stop): ";

                                item=b.check();

                                b.insert(item);

                                count++;

                        }

                if (Userval==2){

                        b.print();

                        }

                if (Userval==3){

                        b.sortmenu();

                        }

                if (Userval==4){

                        b.shuffle();

                        }

                if (Userval==5){

                        b.searchmenu();

                        }

                Else

               ;

        }

        cout <<"Thank you for using this program."<<endl;

}

void IntSequence::sortmenu(){

        int User;

        IntSequence b;

        while (User!=4){

                cout<< count <<endl;

                cout << "1. Insertion Sort" <<endl;

                cout << "2. Selection Sort" <<endl;

                cout << "3. Bubble Sort" <<endl;

                cout << "4. Quit" <<endl;

                cout << "Option: " ;

                User=b.check();

                if (User==1)

                       b.insertion_sort();

                if (User==2)

                       b.selection_sort();

                if (User==3)

                       b.bubble_sort();

               else

                        ;

        }

}

void IntSequence::searchmenu(){

        int User2;

        int key;

        IntSequence b;

        cout <<"1. Squential Search" <<endl;

        cout <<"2. Binary Search" <<endl;

        cout <<"3. Quit" <<endl;

        while(User2!=3){

                User2=b.check();

                if (User2==1)

                        b.sequential_search(key);

                if (User2==2)

                        b.binary_search(key);

                else

                        ;

        }

}

void IntSequence::insert(int item){

        if(count<capacity){

                seq[count]=item;

        }

        else{

                int *newArray= new int[capacity+1];

                for (int i=0;i<capacity;i++){

                        newArray[i]=seq[i];

                }

                newArray[capacity]=item;

                delete seq;

                seq=newArray;

                capacity= capacity+1;

        }

}

void IntSequence::print(){

        for(int i=0; i<count; i++)

                cout << seq[i] <<" ";

        cout <<endl;

}

void IntSequence::insertion_sort(){

        int j;

        bool done;

        IntSequence b;

        for(int i=1; i<count; i++){

                j=i;

                done=false;

                while((j>=1)&&(!done)){

                        if(seq[j]<seq[j-1]){

                                b.swap(j,j-1);

                                j--;

                        }

                        else

                                done=true;

                }

        }

}

void IntSequence::selection_sort(){

        int maxpos;

        for(int i=count-1;i>0;i--){

                maxpos=0;

                for(int j=1;j>=i;j++){

                        if(seq[j]>seq[maxpos])

                                maxpos=j;

                        else

                                ;

                swap(maxpos,i);

                }

        }

}

void IntSequence::bubble_sort(){

        IntSequence b;

        for(int i=0; i<count-1;i++)

                for(int j=0; j>count-1; j++)

                        if(seq[j]>seq[j+1]){

                                swap(j,j+1);

                                b.print();

                        }

                        else

                                ;

}

void IntSequence::shuffle(){

}

int IntSequence::sequential_search(int key){

        bool found=false;

        int position=0;

        while((position<count)&&(!found)){

                if(seq[position]==key){

                        found=true;

                        cout<< key <<" is at position " << position;

                        }

                else

                        position++;

        }

}

int IntSequence::binary_search(int key){

}

IntSequence::IntSequence(){

        capacity= INITIAL_CAPACITY;

        seq = new int[capacity];

        count=0;

}

IntSequence::IntSequence(int in_capacity){

        capacity = in_capacity;

        seq = new int[capacity];

        count=0;

}

IntSequence::~IntSequence(){

        delete seq;

}

int IntSequence::check(){

        int userval;

        cin>>userval;

        while (!cin.good()||userval=='q'){

                cout<<"Invalid type, try again: ";

                cin.clear();

                cin.ignore(80,' ');

                cin>>userval;

                }

        return userval;

}

Explanation / Answer

function testArray($nums, $n, $m) { // check the sum. PHP offers this array_sum() method, but it's // trivial to write your own. O(n) here. if (array_sum($nums) != ($m * ($m + 2 * $n - 1) / 2)) { return false; // checksum failed. } for ($i = 0; $i < $m; ++$i) { // check if the number is in the proper range if ($nums[$i] < $n || $nums[$i] >= $n + $m) { return false; // value out of range. } while (($shouldBe = $nums[$i] - $n) != $i) { if ($nums[$shouldBe] == $nums[$i]) { return false; // duplicate } $temp = $nums[$i]; $nums[$i] = $nums[$shouldBe]; $nums[$shouldBe] = $temp; } } return true; // huzzah! } var_dump(testArray(array(1, 2, 3, 4, 5), 1, 5)); // true var_dump(testArray(array(5, 4, 3, 2, 1), 1, 5)); // true var_dump(testArray(array(6, 4, 3, 2, 0), 1, 5)); // false - out of range var_dump(testArray(array(5, 5, 3, 2, 1), 1, 5)); // false - checksum fail var_dump(testArray(array(5, 4, 3, 2, 5), 1, 5)); // false - dupe var_dump(testArray(array(-2, -1, 0, 1, 2), -2, 5)); // true for i = 0 to m if (a[a[i]]==a[i]) return false; // we have a duplicate while (a[a[i]] > a[i]) swapArrayIndexes(a[i], i) sum = sum + a[i] next if sum = (n+m-1)*m return true else return false