Question

Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due...

Assignment #2: List - Array Implementation

Review pages 6-7 in "From Java to C++" notes.

Due Friday, February 9th, 2017 @ 11:59PM EST

Directions

Create a List object. Using the following definition (List.h file is also in the repository for your convenience) for a list, implement the member functions (methods) for the List class and store the implementation in a file called List.cpp. Use an array to implement the list. Write the client code (the main method and other non-class methods) and put those methods in file main.cpp.

// file: List.h 
#include<string>
using namespace std;
typedef string ElementType;
const int MAX = 100;
class List
{
 public: 
   List(); // Create an empty list
   bool isEmpty(); // Return true if the list is empty, otherwise return false
   void InsertAtEnd(ElementType x); // Insert a value x at the end of the list
   void ReverseDisplay(); // Display the values in the list in reverse (do not reverse the actual list)
   void GetValueAt(int n); // Return the value at the provided location n
   void DeleteAll(ElementType x); // If value x is in the list, remove all instances of x
   void Display(String type); // Display the strings in the list based on the user's specification (all of them, the even length ones, or the odd length ones).
   void Sort(); // Sort the list of strings based on length (shortest to longest)
   double Median(); // Compute and return the median string length for the strings in the list (must be sorted)
   void Shuffle(); // Shuffle the strings in the list around

 private:
        int N; // Number of values in list
  ElementType listArray[MAX]; // The array to hold the list values
};

Write a program to create a single list. Your client code should be menu driven using the following menu options:

  1. Insert
  2. Reverse Display
  3. Show Value at Location
  4. Delete Value
  5. Display Values (All, Even, or Odd)
  6. Sort
  7. Median
  8. Shuffle
  9. Exit

Option 1: Prompt the user for a non-empty string to insert at the end of the list.

Option 2: Display all of the strings in the list in reversed order without actually reversing the list.

Option 3: Prompt the user for a location in the linked list and display the string at that location.

Option 4: Prompt the user for which string to remove from list. Remove every instance of the value specified.

Option 5: Prompt the user to dislay either all strings in the list, all strings of even length, or all strings of odd length.

Option 6: Sort the values in the list based on string length from shortest to longest.

Option 7: Find the median length of the strings in the list and display it.

Option 8: Shuffle the strings in the list around.

Option 9: Exit the program.

Guidelines:

Program should not crash when incorrect input is provided when they are prompted to select a command

Menu should be displayed after completing each action for user to reference.

Command 5 should only accept "All", "Even", or "Odd". Any other input should be denied.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

copy to code:

List.h

/*
* List.h
*
* Created on: Feb 5, 2018
* Author: Mohammad Shahrukh
*/

#ifndef LIST_H_
#define LIST_H_


#include<string>
using namespace std;
typedef string ElementType;
const int MAX = 100;
class List
{
public:
List(); // Create an empty list
bool isEmpty(); // Return true if the list is empty, otherwise return false
void InsertAtEnd(ElementType x); // Insert a value x at the end of the list
void ReverseDisplay(); // Display the values in the list in reverse (do not reverse the actual list)
void GetValueAt(int n); // Return the value at the provided location n
void DeleteAll(ElementType x); // If value x is in the list, remove all instances of x
void Display(string type); // Display the strings in the list based on the user's specification (all of them, the even length ones, or the odd length ones).
void Sort(); // Sort the list of strings based on length (shortest to longest)
double Median(); // Compute and return the median string length for the strings in the list (must be sorted)
void Shuffle(); // Shuffle the strings in the list around

private:
int N; // Number of values in list
ElementType listArray[MAX]; // The array to hold the list values
};

#endif /* LIST_H_ */

List.cpp

#include"List.h"

#include<iostream>

using namespace std;

//initialze list by empty string

List:: List(){

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

listArray[i] ="";

}

N=0;

}

//check the length of N and return result

bool List:: isEmpty() // Return true if the list is empty, otherwise return false

{

if(N==0){

return true;

}else{

return false;

}

}

//insert value at the end

void List::InsertAtEnd(ElementType x)// Insert a value x at the end of the list

{

listArray[N] = x;

N++;

}

//print the value from last

void List :: ReverseDisplay() // Display the values in the list in reverse (do not reverse the actual list)

{

cout<<"String list in reverse order is "<<endl;

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

cout<<listArray[i]<<" ";

}

}

void List::GetValueAt(int n) // Return the value at the provided location n

{

if(!isEmpty()){

if(n>N){

cout<<"Out of bound position selected";

return;

}else

cout<<"Value at location "<<n<<" is "<<listArray[n-1];

}else{

cout<<"List is empty";

}

}

void List :: DeleteAll(ElementType x) // If value x is in the list, remove all instances of x

{

if(!isEmpty()){

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

if(x.compare(listArray[i])==0){

for(int j=i;j<N-1;j++){

listArray[j] = listArray[j+1];

}

listArray[N-1]="";

i--;

N--;

}

}

}else{

cout<<"List is empty";

}

}

void List :: Display(string type) // Display the strings in the list based on the user's specification (all of them, the even length ones, or the odd length ones).

{

if(type.compare("ALL")==0){

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

cout<<listArray[i]<<" ";

}

}else if(type.compare("EVEN")==0){

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

if(listArray[i].length()%2==0)

cout<<listArray[i]<<" ";

}

}else if(type.compare("ODD")==0){

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

if(listArray[i].length()%2 !=0)

cout<<listArray[i]<<" ";

}

}else{

cout<<"Invalid choice entered";

}

}

void List :: Sort()// Sort the list of strings based on length (shortest to longest)

{

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

int length = listArray[i].length();

for(int j=i;j<N;j++){

int lengthComp = listArray[j].length();

if(length>lengthComp){

string temp=listArray[i];

listArray[i] = listArray[j];

listArray[j] = temp;

}

}

}

}

//if N is even it's the average of mid two elements else retunr mid element length

double List :: Median() // Compute and return the median string length for the strings in the list (must be sorted)

{

if(isEmpty()){

return 0.0;

}

Sort();

if(N%2==0){

double lengths = listArray[N/2-1].length()+listArray[N/2].length();

return lengths/2;

}else{

return listArray[N/2].length();

}

}

void List :: Shuffle() // Shuffle the strings in the list aroun

{

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

int position = rand()%N;

string temp=listArray[i];

listArray[i] = listArray[position];

listArray[position] = temp;

}

}

main.cpp

#include <iostream>

#include<string>

#include "List.h"

using namespace std;

int main() {

int choice =0;

List list;

while(choice!=9){

cout<<"\nEnte your choice"<<endl;

cout<<"1. Insert\n2. Reverse Display\n3. Show Value at Location";

cout<<"\n4. Delete Value\n5. Display Values (All, Even, or Odd)\n6. Sort\n7. Median";

cout<<"\n8. Shuffle\n9. Exit";

cin>>choice;

if(choice==1){

string x;

cout<<"Enter the string ";

cin>>x;

list.InsertAtEnd(x);

}else if(choice==2){

list.ReverseDisplay();

}else if(choice==3){

cout<<"Enter the position ";

int position;

cin>>position;

list.GetValueAt(position);

}else if(choice==4){

cout<<"Enter element needs to delete " ;

string element;

cin>>element;

list.DeleteAll(element);

}else if(choice==5){

string choiceSub="";

cout<<"Enter ALL to show all"<<endl;

cout<<"Enter EVEN to show even length one"<<endl;

cout<<"Enter ODD to show odd length all"<<endl;

cin>>choiceSub;

list.Display(choiceSub);

}else if(choice==6){

list.Sort();

}else if(choice==7){

cout<<"The median string length is "<<list.Median();

}else if(choice==8){

list.Shuffle();

}else if(choice==9){

cout<<"Exit program ";

}else {

cout<<"Invalid choice ";

}

}

return 0;

}

output

11 E Console <terminated> firstHello.exe [C/C++ Application] C:Users\Mohamn Ente your choice 1. Insert 2. Reverse Display 3.

Add a comment
Know the answer?
Add Answer to:
Assignment #2: List - Array Implementation Review pages 6-7 in "From Java to C++" notes. Due...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using the following definition (List.h file) for a list, implement the member functions (methods) for the...

    Using the following definition (List.h file) for a list, implement the member functions (methods) for the List class and store the implementation in a List.cpp file. Use a doubly linked list to implement the list. Write the client code (the main method and other non-class methods) and put in file driver.cpp. file: List.h typedef int ElementType; class node{ ​ElementType data; ​node * next; node* prev; }; class List { public: List(); //Create an empty list bool Empty(); // return true...

  • Problem: Implement an interface that manipulates a list of strings. You will be provided with the...

    Problem: Implement an interface that manipulates a list of strings. You will be provided with the following files (see below): • StringList.h containing a class declaration, set up for a linked list representation. • Driver.cpp containing a main function you can use to test your implementation. You will be responsible for providing the StringList.cpp file, including the implementation of the StringList member functions (described below): StringList and ~StringList: creates an empty list, and deallocates all the nodes in the list,...

  • (+30) Provide a python program which will Populate an array(list) of size 25 with integers in...

    (+30) Provide a python program which will Populate an array(list) of size 25 with integers in the range -100 (negative 100)   to +100 inclusive Display the array and its length (use the len function) Display the average of all the integers in the array Display the number of even integers (how many) Display the number of odd integers (how many) Display the number of integers > 0   (how many) Display the number of integers < 0   (how many) Display the...

  • Topics: list, file input/output (Python) You will write a program that allows the user to read...

    Topics: list, file input/output (Python) You will write a program that allows the user to read grade data from a text file, view computed statistical values based on the data, and to save the computed statistics to a text file. You will use a list to store the data read in, and for computing the statistics. You must use functions. The data: The user has the option to load a data file. The data consists of integer values representing student...

  • For Project 02 you’re going to take starter code I’m providing (See in BlackBoard under Projects->Project...

    For Project 02 you’re going to take starter code I’m providing (See in BlackBoard under Projects->Project 02). The zip file contains a P02.java, a HelperClass.java, and an enumeration under Money.java. The only coding you’ll do will be in P02.java. I’ve already provided you with the menu and switch as well as all the methods you’ll need stubbed out. Your assignment is to take the program and complete it without adding any more methods but merely adding code to those stubbed...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • Solve all using Python 7. Create a list of strings, don't ask from the user, and...

    Solve all using Python 7. Create a list of strings, don't ask from the user, and return the count of the number of strings where the string length is 2 or more and the first and last chars of the string are the same (use for loop to go through the list). 8. The file State.txt contains the 50 U.S. states in the order in which they joined the union. Write a program to display the original 13 states in...

  • C++ programming For this assignment, write a program that will act as a geometry calculator. The...

    C++ programming For this assignment, write a program that will act as a geometry calculator. The program will be menu-driven and should continue to execute as long as the user wants to continue. Basic Program Logic The program should start by displaying a menu similar to the following: Geometry Calculator 1. Calculate the area of a circle 2. Calculate the area of a triangle 3. Quit Enter your choice(1-3): and get the user's choice as an integer. After the choice...

  • JAVA Language public class ArraySkills { public static void main(String[] args) { // *********************** // For...

    JAVA Language public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // String[] myData; // 1. Instantiate the given array to hold 10 Strings. // 2. Add your name to the Array at index 0 and a friend's name to the Array at index 4 // 3. Move your...

  • Java Inventory Management Code Question

    Inventory ManagementObjectives:Use inheritance to create base and child classesUtilize multiple classes in the same programPerform standard input validationImplement a solution that uses polymorphismProblem:A small electronics company has hired you to write an application to manage their inventory. The company requested a role-based access control (RBAC) to increase the security around using the new application. The company also requested that the application menu must be flexible enough to allow adding new menu items to the menu with minimal changes. This includes...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT