Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Lab05.cpp #include #include #include #include Array.h void RandInts(ds::Array & arr,int lo,int hi) { int mx = (hi >= lo)?(hi):(lo); int mn = (lo ds::it::Iterator *

image text in transcribed

Lab05.cpp

#include
#include
#include
#include "Array.h"
void RandInts(ds::Array& arr,int lo,int hi)
{
int mx = (hi >= lo)?(hi):(lo);
int mn = (lo
ds::it::Iterator* iter = arr.ToIterator();
while(iter->HasNext())
{
iter->Next() = rand() % (mx - mn + 1) + mn;
}
delete iter;
}
void SortedInts(ds::Array& arr,int start,bool dup)
{
int prev = start;
int c = (dup)?(0):(1);
ds::it::Iterator* iter = arr.ToIterator();
while(iter->HasNext())
{
prev = prev + rand() % 10 + c;
iter->Next() = prev;
}
delete iter;
}
int main()
{
srand(time(NULL));
return 0;
}

Array.h

#ifndef ARRAY_H
#define ARRAY_H
#include
#include
#include
#include "Iterator.h"
namespace ds
{
namespace it
{
template
class Iterator : public IteratorInterface
{
private:
T* data;
int cnt;
int capacity;
public:
Iterator() : Iterator(NULL,0) {}
Iterator(T* data,int capacity) : data(data), capacity(capacity), cnt(0)
{
if(this->capacity
{
this->capacity = 0;
}
}
bool HasNext() const
{
return (cnt
}
T& Next()
{
int c = cnt;
cnt += 1;
return data[c];
}
};
}
template
class Array
{
private:
T* data;
int capacity;
public:
Array() : Array(100) {}
Array(int capacity) : capacity(capacity)
{
if(this->capacity
{
this->capacity = 100;
}
data = new T[this->capacity];
for(int i = 0;i capacity;i += 1)
{
data[i] = T();
}
}
Array(const Array& obj)
{
/*Deep Copy*/
capacity = obj.capacity;
data = new T[capacity];
for(int i = 0;i
{
data[i] = obj.data[i];
}
}
Array& operator=(const Array& rhs)
{
if(this != &rhs)
{
capacity = rhs.capacity;
delete[] data;
data = new T[capacity];
for(int i = 0;i
{
data[i] = rhs.data[i];
}
}
return *this;
}
~Array() {delete[] data;}
const T& operator[](int idx) const
{
if(idx = capacity)
{
throw "Out of bound";
}
return data[idx];
}
T& operator[](int idx)
{
if(idx = capacity)
{
throw "Out of bound";
}
return data[idx];
}
int Length() const {return capacity;}
std::string ToString() const
{
std::stringstream out;
out
for(int i = 0;i
{
out
if(i + 1
{
out
}
}
out
return out.str();
}
it::Iterator* ToIterator() const
{
return new it::Iterator(data,capacity);
}
friend std::ostream& operator& obj)
{
out
return out;
}
};
}
#endif

C++ Programming.

For this lab, your objective is to define two of the three functions below. You may only use what is included in the "lab05.cpp" file. Given an array of integers that range from 0 to n-1 where n is the length of the array, return the value of the array that is repeated the most. If more than one value have the maximum frequency, return the minimum of the values. Given two arrays, return the number of values they have in common. Given a sorted array of integers and a nonnegative target value, return the number of pairs whose difference is equal to the target. Do not include pairs of the same element. For this lab, your objective is to define two of the three functions below. You may only use what is included in the "lab05.cpp" file. Given an array of integers that range from 0 to n-1 where n is the length of the array, return the value of the array that is repeated the most. If more than one value have the maximum frequency, return the minimum of the values. Given two arrays, return the number of values they have in common. Given a sorted array of integers and a nonnegative target value, return the number of pairs whose difference is equal to the target. Do not include pairs of the same element

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions

Question

Know the basic parts of a strategic planning process

Answered: 1 week ago