Assume valid input from user. Files to create: intArrayWrapper.h intArrayWrapper
ID: 3841317 • Letter: A
Question
Assume valid input from user. Files to create: intArrayWrapper.h intArrayWrapper.opp Main. opp Makefile Create a class called IntArraywrapper wren the following members: Public IntArraywrapper(int size) Creates an array of ints of the given size Store the size in m_size Assigns default value of zero to an positions in the array Assumes valid parameters, no checking needed -IntArraywrapper() Deletes the array (will be verified with valgrind) int getsize () const Returns the size of the array void servalueAtPosition(int index, int value) Stores the value in the array at the index given Assumes valid parameters, no checking needed Returns the value in the array at the given position lint getsize() const Returns the largest value in the array Private int* m_arr int m_size In main create the following functions Given an fillArrayWrapper (IntArrayWrapper & wrapper) object, let the user fill it with values bool search(const IntArrayWrapper& wrapper, int value) Returns true if the value is in the wrapper, false otherwise Create a stack-allocated IntArrayWrapper of size 5 Call fillArrayWrapper to handle the storing of values Print all values in the array (formatted as shown) Let's the user search for a value Example run: Enter value 1: 10 Enter value 2: 15 Enter value 3: 5 Enter value 4: 99 Enter value 5: 30 Here are your values: [10, 15, 5, 99, 30] Enter search value: 99 33 is in the ArrayWrapperExplanation / Answer
Here is the implementation with all the necessary condition mentioned !
IntArrayWrapper.h
#ifndef IntArrayWrapper_H
#define IntArrayWrapper_H
#include <iostream>
using namespace std;
class IntArrayWrapper{
public:
IntArrayWrapper();
IntArrayWrapper(int size);
~IntArrayWrapper();
int getSize() const;
void setValueAtPosition(int index,int value);
int getValueAtPosition(int pos) const;
int getMax() const;
private:
int* m_arr;
int m_size;
};
#endif
IntArrayWrapper.cpp
#include <iostream>
#include "IntArrayWrapper.h"
using namespace std;
IntArrayWrapper::IntArrayWrapper(int size){
m_arr = new int(size);
m_size = size;
for(int i=0;i<size;i++)m_arr[i]=0;
}
IntArrayWrapper::~IntArrayWrapper(){
delete [] m_arr;
}
int IntArrayWrapper::getSize() const{
return m_size;
}
void IntArrayWrapper::setValueAtPosition(int index,int value){
m_arr[index]=value;
}
int IntArrayWrapper::getValueAtPosition(int pos) const{
return m_arr[pos];
}
int IntArrayWrapper::getMax() const{
int largest=m_arr[0];
for(int i=0;i<m_size;i++){
if(m_arr[i]>largest) largest=m_arr[i];
}
return largest;
}
Main.cpp
#include <iostream>
#include "IntArrayWrapper.cpp"
using namespace std;
void fillArrayWrapper(IntArrayWrapper& wrapper){
int size=wrapper.getSize();
int value;
for(int i=0;i<size;i++){
cout << "Enter Value " << i << ": ";
cin >> value;
wrapper.setValueAtPosition(i,value);
}
}
bool search(const IntArrayWrapper& wrapper, int value){
int size=wrapper.getSize();
for(int i=0;i<size;i++){
if(value == wrapper.getValueAtPosition(i)) return true;
}
return false;
}
int main(){
IntArrayWrapper* wrapper=new IntArrayWrapper(5);
fillArrayWrapper(*wrapper);
int size=wrapper->getSize();
int value;
cout << "Here are your values: {";
for(int i=0;i<size;i++){
cout << wrapper->getValueAtPosition(i) ;
if(i<size-1)cout << ", ";
}
cout << "} ";
cout << "Enter search value: ";
cin >> value;
if(search(*wrapper,value)) cout << value << " is in the ArrayWrapper";
else cout << value << " is not in the ArrayWrapper";
return 0;
}
Compile and have fun ! Let me know if you have any doubts, guide on makefile isn't given and also I have already added the library function to the code. So, it isn't needed here ! But still I have made one, a very basic one:
IDIR =../include
CC=g++
CFLAGS=-I$(IDIR)
ODIR=obj
LDIR =../lib
LIBS=-lm
all:
g++ Main.cpp -o Main
The logic of the program has been implemented in the code, makefile is very easy to make ! The above provided doesn't deal with dependencies because I have included header files in my code. So, let me know if you have any doubts !