Question

You are given a list, L, and another list, P, containing integers sorted in ascending order....

You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLots(L, P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the elements in positions 1, 3, 4, and 6 in L are printed. Write the procedure printLots(L, P). You may use only the public STL container operations. What is the running time of your procedure?

Homework Answers

Answer #1

The solution is provided in the code snippet provided below using vector of STL.

Vector are same as dynamic arrays placed in contiguous storage .

// printLots prints elements in vector L at positions specified in vector P
void printLots(vector<int> L, vector<int> P)          
{
    // This for loop runs for every element in P
    // P.size() returns the number of elements in vector P
    for (int i = 0; i < P.size(); i++) 
    {
        // This line will print elememts in L where i is the current position accessed in P  
        // and (P[i] - 1) is the required position in L 
        // since the vector starts from 0th location.
        cout << L[ P[i] - 1 ]<<" ";      
    }
}

The running time of this procedure is equal to the number of elements/values in vector P which will be equal to P.size() as for loop runs P.size() times.

Know the answer?
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
Similar Questions
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
based on the code below, answer the questions Question 1: The LinkedList class uses another class...
based on the code below, answer the questions Question 1: The LinkedList class uses another class called Node (which is defined inside LinkedList.java). What are the fields in the Node class? Question 2: The Node class uses generics. Why? Question 3: The linkFirst method contains the following lines of code: if (f == null) last = newNode; else f.prev = newNode; What is the value of the linked list’s size attribute at this point in the code if f ==...
Implement a singly linked list having all unique elements with the following operations.I 0 x –...
Implement a singly linked list having all unique elements with the following operations.I 0 x – Inserts element x at the end. I 1 y x – If the element y exists, then insert element x after the element y, else insert element y before the existing element x. Assuming either the element x or the element y exists. I 2 z y x – Inserts element x in the middle of the elements z and y. The element z...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as...
Java question, Please answer everything. Thank you Answer the following questions as briefly (but completely) as possible: What is a checked exception, and what is an unchecked exception? What is NullPointerException? Which of the following statements (if any) will throw an exception? If no exception is thrown, what is the output? 1: System.out.println( 1 / 0 ); 2: System.out.println( 1.0 / 0 ); Point out the problem in the following code. Does the code throw any exceptions? 1: long value...
Do the following problems. 1. Each of three barrels from a manufacturing line are classified as...
Do the following problems. 1. Each of three barrels from a manufacturing line are classified as either above (a) or below (b) the target weight. Provide the ordered sample space. 2. The heat on each of two soldered parts is measured and labeled as either low (l), medium (m), or high (h). State the number of elements in the ordered sample space. 3. Consider the set of Beatles songs with a primary writer as either Paul McCartney (P) or John...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
Clinical Scenario: You are admitting this patient from the ED and have completed the following H&P....
Clinical Scenario: You are admitting this patient from the ED and have completed the following H&P. CC: Abdominal Pain HPI: Ms. ABC is a 40-year-old Caucasian female who presented to the ED with a complaint of abdominal pain x 1week. She reports LUQ and epigastric abdominal pain, which radiates to the back, is constant, and gets better with sitting up or leaning forward. She has had some associated nausea, vomiting, fever, constipation, and fatigue. She has had multiple admissions for...
a)How is it possible to determine if CaCO3 is Cl- free after synthesis? b)How can the...
a)How is it possible to determine if CaCO3 is Cl- free after synthesis? b)How can the Cl- ions be remove from CaCO3 after synthesis? I should answer the questions from the following experiment but if you know the answer and you are sure, yo do not need to read experiment. Please answer correctly because i hav no chance to make wrong :(((( Physical and Chemical Properties of Pure Substances Objective The aim of today’s experiment is to learn handling chemicals...
Review the Robatelli's Pizzeria Case Study. Develop another internal controls system, but this time, in the...
Review the Robatelli's Pizzeria Case Study. Develop another internal controls system, but this time, in the purchases and fixed assets business areas. Prepare a 12- to 16-slide presentation describing the purchases and fixed assets business areas. Be sure to incorporate speaker notes as well as appropriate visuals, graphics, fonts, etc. Include any associated risk in these areas. Describe specific internal controls that include authorization of transactions, segregation of duties, adequate records and documentation, security of assets, and independent checks and...