Ask Question, Ask an Expert

+61-413 786 465

info@mywordsolution.com

Ask C/C++ Expert


Home >> C/C++

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 operator+(VectorBag &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. 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).

C/C++, Programming

  • Category:- C/C++
  • Reference No.:- M92681897

Have any Question?


Related Questions in C/C++

Question 1find the minimum and maximum of a list of numbers

Question: 1. Find the Minimum and Maximum of a List of Numbers: 10 points File: find_min_max.cpp Write a program that reads some number of integers from the user and finds the minimum and maximum numbers in this list. Th ...

Project - space race part a console Project - Space Race Part A: Console Implementation

Project - Space Race Part A: Console Implementation INTRODUCTION This assignment aims to give you a real problem-solving experience, similar to what you might encounter in the workplace. You have been hired to complete a ...

There are several ways to calculate the pulse width of a

There are several ways to calculate the pulse width of a digital input signal. One method is to directly read the input pin and another method (more efficient) is to use a timer and pin change interrupt. Function startTi ...

1 implement the binary search tree bst in c using the node

1. Implement the Binary Search Tree (BST) in C++, using the Node class template provided below. Please read the provided helper methods in class BST, especially for deleteValue(), make sure you get a fully understanding ...

What are the legal requirements with which websites must

What are the legal requirements with which websites must comply in order to meet the needs of persons with disabilities? Why is maximizing accessibility important to everyone?

Assignment word matchingwhats a six-letter word that has an

Assignment: Word Matching What's a six-letter word that has an e as its first, third, and fifth letter? Can you find an anagram of pine grave. Or how about a word that starts and ends with ant (other than ant itself, of ...

Why do researcher drop the ewaste and where does it end

Why do researcher drop the ewaste and where does it end up?

Assign ment - genetic algorithmin this assignment you will

ASSIGN MENT - GENETIC ALGORITHM In this assignment, you will use your C programming skills to build a simple Genetic Algorithm. DESCRIPTION OF THE PROGRAM - CORE REQUIREMENTS - REQ1: Command-line arguments The user of yo ...

Software development fundamentals assignment 1 -details amp

Software Development Fundamentals Assignment 1 - Details & Problems - In this assignment, you are required to answer the short questions, identify error in the code, give output of the code and develop three C# Console P ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As