Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Provided is a little class that stores numbers and can sort them. The sorting algorithm this class uses is insertion sort. The problem is that

Provided is a little class that stores numbers and can sort them. The sorting algorithm this class uses is insertion sort.

The problem is that the insertion sort is going into an infinite loop, and I need to diagnose why that is happening and to fix it.

Will also need to write the load_from_file and write_to_file member functions, as well as a main that drives the program.

You will find a little class that stores numbers in an array. Some of these functions have been written for you, and some are left for you to write yourself.

Goal:

Write the implementations for the load_from_file and save_to_file functions.

Write a main that asks the user for the name of their file. Remember you can read the filename into a string and then use the .c_str() function to use that string in an open command: o ifs.open(filename.c_str());

The main then:

loads the array from the file into a NumList object

Calls the i_sort function on the NumList

Writes the sorted numbers out to the screen, using the see_all function

* numlist.h

#ifndef NUMLIST_H

#define NUMLIST_H

#include

#include

#include

class NumList{

public:

static const size_t CAPACITY = 100;

/**

* @brief Construct a new NumList object

* @pre none

* @post used is initialized to zero

*

*/

NumList();

/**

* @brief Add a new number to the end of the list

* @pre none

* @post num is placed at the end of the list, if there is sufficient capacity

*

* @param num - the number being added

*/

void insert(int num);

/**

* @brief Get the number of elements in the list

*

* @return size_t - the number of elements in the list

*/

size_t size()const {return used;}

/**

* @brief Load data from a file into the array

* @pre ins is an input stream that was successfully connected to an open file

* @post the data from the file is loaded into the array

*

* @param ins - the stream to read data from

*/

void load_from_file(std::istream& ins);

/**

* @brief Save all of the numbers being stored in the object to a file

* @pre outs is an output stream that was successfully connected to an open file

* @post the numbers from the object are written to the file

*

* @param outs - the stream to write to

*/

void save_to_file(std::ostream& outs);

/**

* @brief Sort the numbers in the array using a bubble sort

* @pre none

* @post the number in the array are in sorted order

*

*/

void i_sort();

/**

* @brief Get the number at the spot given by index

* @pre the array has a number at the spot given by index

* @post the value at position index is returned

*

* @param index - the position in the array that you want to see the value for

* @return int - the value at position index

*/

int get_item(size_t index)const;

/**

* @brief Show all the numbers being stored in the array.

*

*/

void see_all()const;

private:

int data[CAPACITY];

size_t used;

};

#endif

*Numlist.cc

#include "numlist.h"

using namespace std;

// Constructor

NumList::NumList(){

used = 0;

}

void NumList::insert(int num){

if(used < CAPACITY){

data[used] = num;

used++;

}

else{

cout << "Error. List capacity has been reached. ";

}

}

void NumList::load_from_file(istream& ins){

// The implementation of this function is left to the student

}

void NumList::save_to_file(ostream& outs){

// The implementation of this function is left to the student

}

void NumList::see_all()const{

if(used == 0){

cout << "Empty list. ";

}

else{

for(size_t i = 0; i < used; ++i)

cout << data[i] << endl;

}

}

int NumList::get_item(size_t index)const{

if(index < used){

return data[index];

}

else{

return -1;

}

}

void NumList::i_sort(){

for (int i = 1; i < used; i++)

{

int next = data[i];

// Move all larger elements up

int j = i;

while (j < used && data[j = 1] > next)

{

data[j] = data[j - 1];

j--;

}

// Insert the element

data[j] = next;

}

}

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

Building The Data Warehouse

Authors: W. H. Inmon

4th Edition

0764599445, 978-0764599446

More Books

Students also viewed these Databases questions

Question

1. Which position would you take?

Answered: 1 week ago