Question

In: Computer Science

C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter,...

C++.how to write a selection sort to sort it.

read_book_data()

This member function takes one parameter, a string that contains the name of a file. This string parameter can be a C++ string or a C string (your choice). The function returns nothing.

This constructor should do the following:

  1. Declare and open an input file stream variable using the file name string passed in as a parameter.

  2. Check to make sure the file was opened successfully. If not, print an error message and exit the program.

  3. Read the file into your book_store object using the ifstream::read() member function for binary input.

  4. Close the file stream variable.

  5. Sort the book objects in the array in ascending order by ISBN number using a sorting algorithm of your choice. Note that the ISBNs are C strings, which means that you will not be able to compare them using the standard relational operators. The ISBN is also private data of the book class, so code in the book_store class will need to call get_isbn() for book object rather than accessing the object's ISBN directly.

Note that the code described above will read data into all of the book data members. That includes both the array of 30 book objects, and the number of array elements filled with valid data. No further initialization of the data members will be needed

void book_store::read_book_data(string data)
{
ifstream inFile;

inFile.open(data, ios::binary);
if( inFile.fail() )
{
cout << "input file did not open";
exit(-1);
}

inFile.read((char*)this, sizeof(book_store));

inFile.close();
}

C++.how to write a selection sort to sort it.

Solutions

Expert Solution

SELECTION SORT:

The selection sort algorithm is a sorting technique to sort an array by repeatedly finding the minimum element from unsorted part and putting it at the beginning of the array. The algorithm maintains two subarrays in a given array.

1) Sorted Subarray
2) Unsorted Subarray.

In every iteration of selection sort, the minimum element from the unsorted subarray is picked and moved to the sorted subarray.

In the above problem , isbn numbers are the character arrays (C-strings).Every Object of the bookstore class contains the isbn number property which is private to the specific class.

To access the isbn number we use the get_isbn() acessor method.

We read the objects of the class from the File inFile using the read() method.

get_isbn() accesor method :

void bookstore::get_isbn(){
    return this.isbn()
}

CODE FOR THE SELECTION SORT:

#include <bits/stdc++.h> 
#include <string.h> 
using namespace std; 
#define MAX_LENGTH 100 

// isbn string should be smaller than MAX_LEN 
void Sort(char inFile) 
{ 
    int i = -1
    vector<book_store> arr = new vector<book_store>();
    
    // reading all the objects of book store class into an array
    while(!inFile.eof())
        arr[++i] = inFile.read((char*)&obj, sizeof(obj));    
    
 
    int n = arr.size()
        int start, end, min_index; 
        char minStr[MAX_LENGTH]; 
        for (start = 0; start < n-1; start++) 
        { 
                int min_index = start; 
                strcpy(minStr, arr[start].get_isbn()); 
                for (end = start + 1; end < n; end++) 
                { 
                        if (strcmp(minStr, arr[end].get_isbn()) > 0) 
                        { 
                                strcpy(minStr, arr[end].get_isbn()); 
                                min_index = end; 
                        } 
                } 

                if (min_index != start) 
                { 
                        char tempChar[MAX_LENGTH]; 
                        strcpy(tempChar, arr[start].get_isbn()); //swap item[pos] and item[i] 
                        strcpy(arr[start].get_isbn(), arr[min_index].get_isbn()); 
                        strcpy(arr[min_index].get_isbn() , tempChar); 
                } 
        } 
} 

NOTICE THAT INORDER TO COMPARE THE STRINGS WE USED THE strcmp() Standard method present in the

String library.

Tested the code with some sample isbn numbers:

(ISBN IS THE 13-DIGIT STANDARD BOOK NUMBER)


Related Solutions

in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function in C# that takes an array of double as the parameter, and return...
Write a function in C# that takes an array of double as the parameter, and return the average
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
C++ Write a function called linearSearch that takes an array as a parameter and search for...
C++ Write a function called linearSearch that takes an array as a parameter and search for a specific value inside this parameter. The function returns the frequency of a specific value in the array. In the main function: 1. Define an array called salaries of length 5. 2. Initialize the array by asking the user to input the values of its elements. 3. Define a variable called key and ask the user to enter a value for this variable. 4....
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and...
In C: Write a function definition called PhoneType that takes one character argument/parameter called phone and returns a double. When the variable argument phone contains the character a or A print the word Apple and return 1099.99 When phone contains anything else return a 0.0
Please write the code in c++ Write a function with one input parameter that is a...
Please write the code in c++ Write a function with one input parameter that is a vector of strings. The function should count and return the number of strings in the vector that have either an 'x' or a 'z' character in them. For example, when the function is called, if the vector argument contains the 6 string values, "enter", "exit", "zebra", "tiger", "pizza", "zootaxy" the function should return a count of 4. ("exit", "zebra", "pizza", and "zootaxy" all have...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is...
C++ A void function named NextLeapYear() that takes an int reference parameter. If the parameter is positive, the function will assign it the next leap year after it; otherwise, the function will assign 4 to it.
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It...
Write a function pretty_print, which takes one parameter that can be any type of namedtuple. It "pretty prints" the contents of the namedtuple, including both the names of its fields and their values. This is subject to a few rules. Each field and its value are displayed on one line, with the name of the field appearing first, followed by a colon and a space, followed by the value of the field converted to a string. The fields should be...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT