Question

In: Computer Science

Give examples of a C++ program that allow users to manipulate (adding, removing, sort, etc.) a...

Give examples of a C++ program that allow users to manipulate (adding, removing, sort, etc.) a list of items. To start, you need to propose a problem that you would like to work on and you can't use TO-DO list.

Solutions

Expert Solution

We can take a problem as follows:

Suppose we have to create a list of phone numbers (contacts list) in a phone. Here, we can add numbers, remove numbers and sort numbers.

Given is the solution of the above problem in C++:

#include <bits/stdc++.h>

using namespace std;

int main()
{
    vector <int> numbers;       //we create an empty vector named numbers
    cout<<"Enter the operation you want to perform: "<<endl;
    int c;                      //we declare int c to take input of the operation.
    int x;                      //we declare x to take input of the number.
    int f = 1;                  //we declare f to indicate whether the program has to be ended or not.

    while(f == 1)
    {
        cout<<"(1). Add"<<endl;         //displaying the menu.
        cout<<"(2). Remove"<<endl;
        cout<<"(3). Sort"<<endl;
        cin>>c;
        switch(c)                   //we pass c within the switch case.
        {
            case 1:                 //case in which user wants to add a number.
                cout<<"Enter number to be added: ";
                cin>>x;
                numbers.push_back(x);       //number is pushed back to the list.
                break;
            case 2:                 //case in which user wants to remove a number.
                cout<<"Enter number to be deleted: ";
                cin>>x;
                for(int i=0;i<numbers.size();i++){
                    if(numbers[i]==x)
                    {
                        numbers.erase(numbers.begin()+i);       //number is erased from the position where it is found.
                    }
                }
                break;
            case 3:                 //case in which user wants to sort the list.
                cout<<"The contact list has been sorted. "<<endl;
                sort(numbers.begin(),numbers.end());
                break;
            default:                //deafult case if user does not enter valid number.
                cout<<"Enter valid number.";
        }

        cout<<"Do you want to perform another operation? If yes Enter 1: ";
        cin>>x;                         //taking input in case the user wants to re-run the program.
        if(x==1)
        {
            f = 1;                      //case when the user wants to re-run it.
        }
        else
        {
            f = 0;                      //case when the user wants to end the program.
        }
    }
    return 0;                           //terminating the program.
}

Kindly like if this helps :)


Related Solutions

String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a...
String Manipulator Write a program to manipulate strings. (Visual Studio C++) In this program take a whole paragraph with punctuations (up to 500 letters) either input from user, initialize or read from file and provide following functionalities within a class: a)   Declare class Paragraph_Analysis b)   Member Function: SearchWord (to search for a particular word) c)   Member Function: SearchLetter (to search for a particular letter) d)   Member Function: WordCount (to count total words) e)   Member Function: LetterCount (ONLY to count all...
Write in C++ code A system that allow users to place an order for the food,...
Write in C++ code A system that allow users to place an order for the food, view all the food details, modify or delete the food details. Design using objected oriented programming and list/link-list. Food order information contain : OrderID(auto assigned,unique), Food Code, flavor , weight, unit price, qty and customer information such as id, name, address and contact number. order ID shall be automatically assigned with a unique ID when new order is added. When viewing food details, it...
Write a C++ program to help the Administration of a football league to manipulate the list...
Write a C++ program to help the Administration of a football league to manipulate the list of players registered in different teams. There are 26 teams participating in the league, each is denoted by a letter in the range A to Z. Each team can have 11 players at most. The information of all teams' players are stored in a text file named 'players.dat'. Each line from the input file contains the details of one player; where the player's details...
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
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football...
In C++ Create a program that uses Selection Sort and Insertion Sort for the National Football League list of current players. It's going to be a big list(over 1000 names). Please identify, in comments, which part of the code is for the Selection Sort and which part of the code is for Insertion Sort. It must have both. Inputting data from a text file named "Players.txt" Only want the name of the player(first name then last name), their team name,...
Write and test a C program to implement Bubble Sort. . In your C program, you...
Write and test a C program to implement Bubble Sort. . In your C program, you should do: Implement the array use an integer pointer, get the size of the array from standard input and use the malloc function to allocate the required memory for it. Read the array elements from standard input. Print out the sorted array, and don’t forget to free the memory. Debug your program using Eclipse C/C++ CDT.
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort()...
Using Java create a program that does the following: Modify the LinkedList1 class by adding sort() and reverse() methods. The reverse method reverses the order of the elements in the list, and the sort method rearranges the elements in the list so they are sorted in alphabetical order. Do not use recursion to implement either of these operations. Extend the graphical interface in the LinkedList1Demo class to support sort and reverse commands, and use it to test the new methods....
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
Write a C++ program that attempts to make the Radix Sort more practical: make it sort...
Write a C++ program that attempts to make the Radix Sort more practical: make it sort strings of a maximum length of 15. Have an array of strings. Then in the Radix Sort, create an array of LinkedQueue with 95 queues (95 are the printable characters starting with space). Those queues will be used to separate the data then combine them (i.e. bins). Randomly generate 10,000 strings with lengths from 1 to 15 (during the sort and with strings less...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT