Write a cpp function pushzerostoend to push all the zeros

Assignment Help C/C++ Programming
Reference no: EM131853540

Assignment

1. Write a C++ function rotate(int arr[], int d, int n) that rotates arr[] of size n to the left by d elements.

2. Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), write a C++ function maxProfit(int prices[], int size ) to find the maximum profit.

3. Given an array of random numbers, write a C++ function pushZerosToEnd(int arr[], int n) to push all the zero's of a given array to the end of the array.

4. Given two sorted arrays, write a C++ function mergeArrays(int arr1[], int arr2[], int n1, int n2, int arr3[]) to merge them in a sorted manner.

5. Given an array of size n, write a C++ function findMajority(int arr[], int size) to find the majority element. The majority element is the element that appears more than ⌊n/2⌋ times. You may assume that the array is non-empty and the majority element always exist in the array.

6. Mini-project: Extending the ADT Bag using vector

In CISC 1100 or 1400, you learned how to compute the union, intersection, and difference of sets:

A∪B = {x: x ∈ A ∨ ∈ B}
A∩B = {x: x ∈ A ∧ ∈ B}
A-B = {x: x ∈ A ∧ ∉ B}

The union, intersection, and difference of bags are defined analogously. The only real difference is that since bags may contain repeated elements, the union (or intersection or difference) of bags may contain repeated elements. For example, suppose that we define bags

A = [1, 1, 2, 2, 3, 4, 4, 4] and B = [1, 2, 3, 3, 4, 4, 5].

(Here, we use [. . .] to denote bags, similarly to the way that {... } denotes a set. Also, there's no reason to assume that a bag is sorted; we show the bag elements in sorted order to simply to make it easier to follow the example.) Then

A∪B = [1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 5]
A∩B = [1, 2, 3, 4, 4]
A - B= [1, 2, 4]

Before doing so, note that the author has given us two different implementations: array-based and link-based. The main problem with the former is that the implementer must decide how big to make the underlying array. This problem doesn't exist for the latter; however, linked structures are more complicated than arrays. However, if we were to use a vector-based implementation, then we wouldn't need to worry about how to choose the size of an array; moreover, vectors are easier to handle than linked structures. So the first thing to do is to create VectorBag, a vector-based implementation. Since vectors may be thought of as "safe arrays", the best thing to do would be to start with the array-based implementation and work from there.

Once you've done this, you should now add new operations to the VectorBag class.

Since the standard symbols ? and ? are not available on our keyboard and since union is a reserved word, let's use + and * to denote union and intersection; of course, - is the obvious choice for denoting set difference. This means that you'll want to define operator+ (and so forth), making sure to add appropriate comments that describe what these do.

Your main task will be to design, write, and test the VectorBag class.

- VectorBag.h: interface file for the VectorBagclass.
- VectorBag.cpp: the implementation file for VectorBag class.
- Note: you will add 3 new functions operator+(), operator -() and operator*(). The prototype of operator+() function is
VectorBag<ItemType> operator+(VectorBag<ItemType> &anotherBag);

I am providing you with the following files, which may be found in the directory ~zhou/datastr/hw/hw1 or on the blackboard for this project:

- The file proj1.cpp, for testing your VectorBag class. This file is similar to the main.cpp that the authors provide for testing the ArrayBag class. Of course, it works for VectorBag, rather than for the ArrayBag class. In addition:

• The displayBag() function will work for a VectorBag<ItemType>. It also displays the items in sorted order. Hence the < operation must be defined for ItemType.

• The new operation (?, ?, -) are tested.

- A working version of the program, called proj1 , for you to try out.
- Your recollection of overloaded operators (such as operator+ and such like) may be a bit rusty. I have provided you with a file point.cc, which defines a Point class that has an operator+ (for adding two Points).

Reference no: EM131853540

Questions Cloud

Case two-the white arch casino : Enoch Thompson was employed for several years as a dealer at the White Arch Casino (WAC) in Las Vegas, Nevada. WAC is one of Colossal Corporation
Thirsty for the triple bottom line : Evaluate Seth Goldman's leadership performance on the concept of the "Triple Bottom Line" using at least two (2) examples from each resource to support your eva
Explain three cognitive biases in negotiations : Explain three cognitive biases in negotiations and give an example for each. Your example could be one that you observed or experienced during your life or an o
Teamwork makes the dream work : Recall what you learned this week about the five stages of group development and team performance. With that in mind, respond to the following:
Write a cpp function pushzerostoend to push all the zeros : Given an array of random numbers, write a C++ function pushZerosToEnd(int arr[], int n) to push all the zero's of a given array to the end of the array.
Supply chain management from a strategic perspective : What are the three C's? How do they relate to supply chain management from a strategic perspective?
Describe the two different type of activities : Describe the two different type of activities that occur within the value chain. How do these activities relate to the supply chain?
Males in the same upper management positions : Why do females tend to receive a lesser pay than males in the same upper management positions? Please cite source if any, thank you.
How important is the honest pursuit of excellence : Here are questions to get you thinking:How do I test and evaluate the choices I make?What constitutes reality, and how do I find it?

Reviews

Write a Review

C/C++ Programming Questions & Answers

  Create program that uses functions and reference parameters

Create program that uses functions and reference parameters, and asks user for the outside temperature.

  Write a program using vectors and iterators

Write a program using vectors and iterators that allows a user to maintain a personal list of DVD titles

  Write the code required to analyse and display the data

Calculate and store the average for each row and column. Determine and store the values for the Average Map.

  Write a webservices application

Write a webservices application that does a simple four function calculator

  Iimplement a client-server of the game

Iimplement a client-server version of the rock-paper-scissors-lizard-Spock game.

  Model-view-controller

Explain Model-View-Controller paradigm

  Design a nested program

How many levels of nesting are there in this design?

  Convert celsius temperatures to fahrenheit temperatures

Write a C++ program that converts Celsius Temperatures to Fahrenheit Temperatures.

  Evaluate and output the value in the given base

Write C program that will input two values from the user that are a Value and a Base with which you will evaluate and output the Value in the given Base.

  Design a base class shape with virtual functions

Design a base class shape with virtual functions

  Implementation of classes

Implementation of classes Chart and BarChart. Class barChart chould display a simple textual representation of the data

  Technical paper: memory management

Technical Paper: Memory Management, The intent of this paper is to provide you with an in depth knowledge of how memory is used in executing, your programs and its critical support for applications.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd