Solved–Homework Assignment, Arrays, Pointers and introduction to Abstract Data Types– Solution

$25.00 $14.00

Arrays and vectors can be used to hold lists. For this homework assignment, you will be making a book list program. Write a program to support the following functions on a book list. You can assume that you will not be buying more than 20 books: Insert new element at the end of the list…

You’ll get a: . zip file solution

 

 

Description

5/5 – (2 votes)

Arrays and vectors can be used to hold lists. For this homework assignment, you will be making a book list program. Write a program to support the following functions on a book list. You can assume that you will not be buying more than 20 books:

Insert new element at the end of the list

Insert new element at a certain position

Find an element (with its ISBN number and list its position) using linear search

Find an element (with its ISBN number and list its position) of a sorted list using binary search Delete an element that is at a certain position

Delete an element by using its ISBN number

Sort the list by the ISBN numbers (using selection sort) – Use pointers for accessing array elements

Sort the list by the ISBN numbers (using bubble sort) – Use pointers for accessing array elements

Print out the list

For the sorting functions, do not use the array indices to access the array elements. For example, do not use array[5]. Instead, use pointers to access the array elements (example: *(array + 5)).

Your program should begin with a loop asking the user what they want to do. Clearly, at the beginning since there is no list, they have to begin by inserting a new element at position 1 (note, when calling the function to insert an element at a certain position, you have to check that the position is within bounds of the current book list).

For your implementation, you can assume that the user will enter the ISBN number of the book (so you can use integers for your list). Use arrays or vectors. (If you use vectors; however, you must implement all of the functionality below and not use the built-in functions on vectors for this assignment.) You only need to use either arrays or vectors.

The function headers should look something like the following (variables names and parameters don’t need to exactly match):

void insert(int mylist[ ], int num_in_list, int new_element);

void insert_at(int mylist[ ], int num_in_list, int at_position, int new_element); int find_linear(int mylist [ ], int num_in_list, int element);

int find_binary(int mylist [ ], int num_in_list, int element, bool sorted); void delete_item_position(int mylist [ ], int num_in_list, int position) ; void delete_item_isbn(int mylist [ ], int num_in_list, int element) ; void sort_list_selection(int mylist [ ], int num_in_list);

void sort_list_bubble(int mylist [ ], int num_in_list);

void print (int mylist[ ], int num_in_list);

Things to note:

  1. You can assume that the first digit in the ISBN numbers stored will not be 0.

  1. When you insert or delete an element from an array at a certain position, you will have to move up or move down the rest of the items on the list. You can use any algorithm you wish to do this.

  1. For both insert and delete, you must check that you do not insert more than 20 items or delete from a list that has no items in it.

  2. If there is more than one occurrence of an ISBN you can just delete the first one

  1. For the binary search, you will need to check to see if the list is sorted already. If it is, binary search can be used. If it is not sorted yet, you will need to print out that the list needs to be sorted first before performing binary search. You can just use a variable to keep track of if the list has been sorted before using the binary search option.

  1. Make sure to use pointers to access array elements for the sorting functions. Do not access the array using indices such as array [5]. This is to make sure you have an understanding of how to access array elements with pointers.

Please see the next page for an example of what the program could look like.

SAMPLE EXECUTION OF PROGRAM:

Welcome to the Book list program.

What would you like to do?

  1. add an element to end of list

  2. add an element at a location

  1. find an element by ISBN number (linear search)

  2. find an element by ISBN number (binary search)

  3. delete an element at position

  4. delete an element by ISBN number

  1. sort the list (using selection sort)

  2. sort the list (using bubble sort)

  3. print the list

    1. exit your choice —

Please type in the element 1454226834

Your list is now

  1. 1454226834

  1. add an element to end of list

  1. add an element at a location

  1. find an element by ISBN number (linear search)

  1. find an element by ISBN number (binary search)

  2. delete an element at position

  1. delete an element by ISBN number

  2. sort the list (using selection sort)

  3. sort the list (using bubble sort)

  4. print the list

    1. exit your choice —

Please type in the element 2454326534

At what position?

Your list is now

  1. 2454326534

  2. 1454226834

  1. add an element to end of list

  1. add an element at a location

  1. find an element by ISBN number (linear search)

  1. find an element by ISBN number (binary search)

  2. delete an element at position

  1. delete an element by ISBN number

  2. sort the list (using selection sort)

  3. sort the list (using bubble sort)

  4. print the list

    1. exit your choice —

Which element would you like to delete (by ISBN number)? 1454226834

Your list is now

  1. 2454326534

  1. add an element to end of list

  2. add an element at a location

  1. find an element by ISBN number (linear search)

  1. find an element by ISBN number (binary search)

  1. delete an element at position

  2. delete an element by ISBN number

  3. sort the list (using selection sort)

  4. sort the list (using bubble sort)

  1. print the list

  1. exit your choice —