I'm trying to overload an int array, so basically if I have this in my main func
IntArray arr(10);
for(int i = 0; 0 < 10; i++)
a[i] = i * 10;
I was able to get
a[0] = 0, a[1] =10, a[2]=20.... so on
I was able to make this one work.
If I then do
IntArray arr(-3, 10)
with same for loop, I was able to get the appropriate answer as well.
BUT...
If I do
IntArray arr(6, 8)`
using same for loop I get c[6] = 1, c[7]=1, c[8] =0
I been trying to figure this out since yesterday, here's my code:
//---------------------Header file
#ifndef INTARRAY_H_
#define INTARRAY_H_
#include <iostream>
using namespace std;
class IntArray
{
private:
int first;
int last;
int size;
string arrName;
int* arrPtr;
public:
IntArray();
int& operator[](int i);
IntArray(int num);
IntArray(int num1, int num2);
int low();
int high();
void setName(string str);
//streams
friend istream& operator>>(istream& is, IntArray& d);
friend ostream& operator<<(ostream& os, IntArray& d);
};
#endif /* INTARRAY_H_ */
//---------------------
My int array class:
#include "IntArray.h"
IntArray::IntArray(){
size = 10;
first = 0; last = 9;
arrPtr = new int[size];
}
IntArray::IntArray(int num){
size = num;
first = 0; last = size-1;
arrPtr = new int[size];
//cout<<"\n the size "<<size<<endl;
//arrPtr[3] = 123;
//ex = 3;
}
IntArray::IntArray(int num1, int num2){
first = num1; last = num2;
size = last - first + 1;
//cout<<"\n the size "<<size<<endl;
arrPtr = new int[size];
}
int& IntArray::operator[](int i) {
if (i >= (last+1)){
cout<<"Error: Index out of range"<<endl;
exit(1);
}
return arrPtr[i];
}
int IntArray::getInt(int i){
return arrPtr[i];
}
//======================
int IntArray::low(){
return first;
}
int IntArray::high(){
return last;
}
//======================
//==========OUT stream==========
// for cout << justName<< endl;
ostream& operator<<(ostream& os, IntArray& aPtr) {
for(int i = aPtr.low(); i <= aPtr.high(); i++){
os << aPtr.arrName <<"[" << i << "] = " << aPtr[i] << " ";
}
return os;
}
void IntArray::setName(string str){
arrName = str;
}
For some apparent reason, after using my setName function on this test function, it changes the value to c[6] = 1, c[7]=1, c[8] =0.
void test3() {
IntArray c(6, 8);
for(int i = c.low(); i <= c.high(); i++)
c[i] = i * 10;
c.setName("c");
cout << c << endl;
}
Let me make this one clear. Like I said, I did narrowed down my problem.
IntArray a(10) //THIS ONE WORKS!
IntArray a(-3, 6) //THIS ONE WORKS!
IntArray a(6,8) //NOT working.
For the last one, the array outputs the right value, until I use the setName function.
This may not be your only problem but it looks like you want to allow users of your class to use the old Perl $[
To support this you'll need to modify it in your operator[]:
int& IntArray::operator[](int i) {
if (i > last || i < first ){
cout<<"Error: Index out of range"<<endl;
exit(1);
}
return arrPtr[i - first];
}
In the subscript operator you also should check that i is not less than first.
Tale into account that relative to the pointer you have to use indices starting from zero. So instead of
return arrPtr[i];
you have to write
return arrPtr[i - first];
Also it would be better if constructor
IntArray(int num);
would be declared like
IntArray( size_t num);
and correspondingly data member size would have type size_t
Related
I want to write a program that can use a dynamic array.
A size is to be passed via the constructor and as soon as this size is reached, a new array is generated in which the previous values are copied into it.
For this, I overloaded the [] operator. The program seems to work at first glance.
But after I tried to implement an array with the size 100 and to save 20000 elements here, different numbers are output.
At the first run, more than 7000 numbers were displayed. After another run over 1800. However, never the desired 20000.
What could be the reason for this?
#include <iostream>
using namespace std;
template<class T>
class Container{
public:
T *dynamicArray;
private:
T *newArray;
int size;
public:
Container(int size){
this->size=size;
dynamicArray=new T[size];
}
T operator[] (unsigned long index){
if(index>size-1){
newArray=new T[size+(index-size)];
T i;
for(i=0; i<(size+(index-size)); i++){
newArray[i]=dynamicArray[i];
}
delete[] dynamicArray;
dynamicArray=newArray;
delete[] newArray;
}
return dynamicArray[index];
}
};
int main()
{
Container <int> dArray(100);
for(int i=1; i<20000; i++){
dArray.dynamicArray[i]=i;
cout << dArray.dynamicArray[i] << "\n";
}
return 0;
}
Thank you!
first , this code didn't call the overloaded [] operator function , its just using default operator [] of type T array , and it never allocate any memory , you can output at allocate memory position
dArray.dynamischesArray[i]=i;
cout << dArray.dynamischesArray[i] << "\n";
and there it some error logic in the overloaded [] operator function
T operator[] (unsigned long index){
if(index>size-1){
neuesArray=new T[size+(index-size)];
T i;
for(i=0; i<(size+(index-size)); i++){
neuesArray[i]=dynamischesArray[i];
}
delete[] dynamischesArray;
dynamischesArray=neuesArray;
//delete[] neuesArray; cannot deleted ,course error
size = index + 1; //keep size sync real size
}
return dynamischesArray[index];
}
this is modified code can be run correct :
#include <iostream>
#include <string>
using namespace std;
template<class T>
class Container {
public:
T *dynamischesArray;
private:
T *neuesArray;
int size;
public:
Container(int size) {
this->size = size;
dynamischesArray = new T[size];
}
T& operator[] (unsigned long index) {
if (index > size - 1) {
cout << "allocate :" << index<<"\n";
neuesArray = new T[index+1];
unsigned long i;
for (i = 0; i < size; i++) {
neuesArray[i] = dynamischesArray[i];
}
delete[] dynamischesArray;
dynamischesArray = neuesArray;
//delete[] neuesArray;
size = index+1;
}
T&ret = dynamischesArray[index];
//return dynamischesArray[index];
return ret;
}
};
int main()
{
Container <int> dArray(100);
for (int i = 1; i < 20000; i++) {
dArray[i] = i;
cout <<"i="<<i<<" "<< dArray[i] << "\n";
//dArray.dynamischesArray[i] = i;
//cout << dArray.dynamischesArray[i] << "\n";
}
return 0;
}
this is result
I am having issues finishing passing an array via pointers through a series of functions. I create a function using dynamic allocation to create it. Even though that is successful I cannot get it to pass through functions that take pointers as arguments. The functions return the mean median and mode and have been completed. However I cannot pass them when converting them into pointer syntax. Thanks for the help in advance.
#include <iostream>
#include <cstdlib>
using namespace std;
int students;
int * studentarray;
int stumode;
double stuavg;
int stumed;
int arr;
int mode(int *[], int );
double average(int *[], int);
double median(int *[], int);
void selectSort(int [], int);
void swap(int *, int *);
int makeArray(int*, int);
int main()
{
studentarray = &arr;
cout << "How many students are there?" << endl;
cin >> students;
makeArray(studentarray, students);
for (int i = 0; i < students; i++) {
cout << "How many movies did student " << i + 1 << " view?" << endl;
cin >> studentarray[i];
}
selectSort(studentarray, students);
stumode = mode(&studentarray, students);
stuavg = average(&studentarray, students);
stumed = median(&studentarray, students);
cout << "The array has been sorted in ascending order." << endl;
cout << "The mode is " << stumode << "." << endl;
cout << "The mean is " << stuavg << "." << endl;
cout << "The median is " << stumed << "." << endl;
delete[] studentarray;
return 0;
}
int mode(int *arr, int size)
{
if (size <= 0) return 0;
int most = 0, position = 0, most_count = 0;
int counter = 1;
for (int i = 1; i < size; i++)
{
if (* (arr + i) != * (arr + position) )
{
if (counter > most)
{
most = counter;
most_count = 0;
}
else if (counter == most) most_count++;
position = i;
counter = 0;
}
else counter++;
}
if (most_count) return 0;
else return * ( arr + position );
}
double average(int *arr, int size)
{
if (size <= 0) return 0;
int total = 0;
for (int i = 0; i < size; i++) {
total += *(arr + i);
}
return (double)total / size;
}
double median(int *arr, int size)
{
if (size <= 0) return 0;
if (size % 2 == 0)
return (double) (* (arr + (size + 1) / 2));
else {
int mid = size / 2;
return (double)(* (arr + mid) + * (arr + mid + 1) / 2);
}
return 0;
}
void selectSort(int arr[], int size)
{
int min;
for (int i = 0; i < size - 1; i++)
{
min = i;
for (int j = i + 1; j < size; j++)
{
if ( arr[j] < arr[min])
{
min = j;
}
}
swap(&arr[min], &arr[i]);
}
}
void swap(int *one, int *two) {
int temp = *one;
*one = *two;
*two = temp;
}
int makeArray(int *arr, int size)
{
arr = new int[size];
return *arr;
}
Your implementation of makeArray is not right.
int makeArray(int *arr, int size)
{
// Allocates memory and assigns it to arr.
// This is a local change to arr. The value of the variable in
// main remains unchanged.
arr = new int[size];
// Returns an uninitialized value.
return *arr;
// The memory allocated in the previous line is now a memory leak.
}
You can make it simpler by using:
int* makeArray(int size)
{
return new int[size];
}
and use it in main as:
arr = makeArray(students);
However, I don't see how that is better than using:
arr = new int[students];
If you do that, makeArray becomes unnecessary. If makeArray needs to have additional code to fill up the array with some values, it will be useful. Otherwise, it does not add any useful functionality to your program.
Having said all of that, it is better to use std::vector instead of managing dynamically allocated memory in your own code. You would use:
std::vector<int> arr(students);
PS
I did not go through rest of your code. There might be other errors.
I am trying to use the dynamic memory method instead of the vector method to add elements. Initially, the maximum size of the dynamic memory is set to 5. However, as soon as I try to increase more than the capacity of the current the dynamic memory, the elements of the 0th or 1st index loss their references.
The program works fine if I do not specify the size of the dynamic memory,
like: dynamic_memory = new int;. I am wondering why they lose their references
with the resize of the dynamic memory to more than the initial capacity.
PS: I am using Code::Block 16.01
Here is my program.
#include <iostream>
#include <cstdlib>
using namespace std;
class DynamicVector
{
public:
DynamicVector();
virtual ~DynamicVector();
void insertElement(int input);
int showCapacity();
int showSize();
void doubleSize(int * dynamic_memory);
friend ostream& operator << (ostream& outs, const DynamicVector obj);
private:
int * dynamic_memory;
int max_count; // this is similar to the capacity of the vector
int current_count; // this is similar to size of a vector
};
DynamicVector::DynamicVector()
{
max_count = 5;
dynamic_memory = new int[max_count];
current_count = 0;
}
DynamicVector::~DynamicVector()
{
delete [] dynamic_memory;
}
int DynamicVector::showCapacity(){
return max_count;
}
void DynamicVector::insertElement(int input)
{
if (current_count >= max_count)
doubleSize(dynamic_memory);
dynamic_memory[current_count] = input;
current_count++;
}
void DynamicVector::doubleSize(int * dynamic_memory){
int * tmp = new int[max_count];
for (int i = 0; i < max_count; i++)
tmp[i] = dynamic_memory[i];
delete [] dynamic_memory;
max_count = max_count * 2;
dynamic_memory = new int[max_count];
for (int i = 0; i < max_count; i++)
dynamic_memory[i] = tmp[i];
delete [] tmp;
}
int DynamicVector::showSize(){
return current_count;
}
ostream& operator <<(ostream& outs, const DynamicVector obj)
{
for (int i = 0; i < obj.current_count; i++)
outs << obj.dynamic_memory[i] << endl;
return outs;
}
int main()
{
DynamicVector v;
int numberOfIntendedElement = 11;
cout << "Previously, the capacity of vector was: " << v.showCapacity() << endl;
for (int i = 0; i < numberOfIntendedElement; i++)
v.insertElement(i);
cout << "The capacity of the new vector is: " << v.showCapacity() << endl;
cout << "The size of the new vector is: " << v.showSize() << endl;
cout << "The values in the dynamic vector are: \n" << v << endl;
return 0;
}
Result:
41107976
42075512
2
3
4
5
6
7
8
9
10
In
void doubleSize(int * dynamic_memory);
the dynamic_memory defined here shadows the member dynamic_memory; for comedic hi-jinks and undefined behaviour.
The local dynamic_memory is re-pointed at the new buffer, but the member dynamic_memory continues to point at the deleted original address after the function exits. This means that all subsequent inserts go into invalid memory, and Crom only knows what will happen after that.
Solution
Pass in nothing and use the member variable. Redefine the function as
void doubleSize();
Other problems are addressed in the comments and need to be fixed.
Thank you all for your valuable comments and suggestions, especially user4581301 for pointing a comedic hijinks and undefined behavior. After I redefined the function as void doubleSize(), it worked fine. Here is my final working code.
#include <iostream>
#include <cstdlib>
using namespace std;
class DynamicVector
{
public:
DynamicVector();
virtual ~DynamicVector();
void insertElement(int input);
int showCapacity();
int showSize();
void doubleSize();
friend ostream& operator << (ostream& outs, const DynamicVector obj);
private:
int * dynamic_memory;
int max_count; // this is similar to the capacity of the vector
int current_count; // this is similar to size of a vector
};
DynamicVector::DynamicVector()
{
max_count = 5;
dynamic_memory = new int[max_count];
current_count = 0;
}
DynamicVector::~DynamicVector()
{
delete [] dynamic_memory;
}
int DynamicVector::showCapacity(){
return max_count;
}
void DynamicVector::insertElement(int input)
{
if (current_count >= max_count)
doubleSize();
dynamic_memory[current_count] = input;
current_count++;
}
void DynamicVector::doubleSize(){
int * tmp = new int[max_count];
for (int i = 0; i < max_count; i++)
tmp[i] = dynamic_memory[i];
delete [] dynamic_memory;
max_count = max_count * 2;
dynamic_memory = new int[max_count];
for (int i = 0; i < max_count/2; i++)
dynamic_memory[i] = tmp[i];
delete [] tmp;
}
int DynamicVector::showSize(){
return current_count;
}
ostream& operator <<(ostream& outs, const DynamicVector obj)
{
for (int i = 0; i < obj.current_count; i++)
outs << obj.dynamic_memory[i] << endl;
return outs;
}
int main()
{
DynamicVector v;
int numberOfIntendedElement = 11;
cout << "Previously, the capacity of vector was: " << v.showCapacity() << endl;
for (int i = 0; i < numberOfIntendedElement; i++)
v.insertElement(i);
cout << "The capacity of the new vector is: " << v.showCapacity() << endl;
cout << "The size of the new vector is: " << v.showSize() << endl;
cout << "The values in the dynamic vector are: \n" << v << endl;
return 0;
}
Output
Previously, the capacity of vector was: 5
The capacity of the new vector is: 20
The size of the new vector is: 11
The values in the dynamic vector are:
0
1
2
3
4
5
6
7
8
9
10
I am writing a program that displays integer arrays. I set the size of the array, but I am wondering how I can ask the user the index of the array that they want listed. Say the const SIZE = 10, and the user wants to see the first three in the array. I want to also write an exception that catches the error if the user input is over the size of the array. If you need to see some code, let me know. Any help is appreciated!
intergerarray.h
class IntArray
{
private:
int *aptr; // Pointer to the array
int arraySize; // Holds the array size
void subscriptError(); // Handles invalid subscripts
public:
class OutOfBoundException
{
public:
int index;
OutOfBoundException(){};
int getInde() { return index; }
};
IntArray(int); // Constructor
IntArray(const IntArray &); // Copy constructor
~IntArray(); // Destructor
int size() const // Returns the array size
{
return arraySize;
}
int &operator[](const int &); // Overloaded [] operator
};
IntergerArray.cpp
IntArray::IntArray(int s)
{
arraySize = s;
aptr = new int[s];
for (int count = 0; count < arraySize; count++)
*(aptr + count) = 0;
}
IntArray::IntArray(const IntArray &obj)
{
arraySize = obj.arraySize;
aptr = new int[arraySize];
for (int count = 0; count < arraySize; count++)
*(aptr + count) = *(obj.aptr + count);
}
IntArray::~IntArray()
{
if (arraySize > 0)
delete[] aptr;
}
void IntArray::subscriptError()
{
cout << "ERROR: Subscript out of range.\n";
exit(0);
}
int &IntArray::operator[](const int &sub)
{
if (sub < 0 || sub >= arraySize)
subscriptError();
return aptr[sub];
}
driver file.cpp
int main()
{
int SIZE = 10;
//int index;
//cout << "enter an index";
//cin >> index;
IntArray table(SIZE);
for (int x = 0; x < SIZE; x++)
table[x] = x;
for (int x = 0; x < SIZE; x++)
cout << table[x] << " ";
cout << endl;
//table[SIZE + 1] = 0;
return 0;
}
Isn't this what you are trying to do? why so much code for such a simple problem?
const int arraySize = 10;
int array[arraySize];
int elementToDis;
do
{
std::cout << "Number of array elements to display: ";
std::cin >> elementToDis;
} while (elementToDis > arraySize || elementToDis < 0); // this is your exeption
for (int ccc(0); ccc < elementToDis; ++ccc)
std::cout << "Index " << ccc << ": " << array[ccc] << '\n';
I think you want to display all elements lower than an index value entered by the user :
Let array[] be the array name of size=10,you can get an index value (say l) from the user and use that value inside a for loop for printing all elements in index lower than l
int array[size]
void disp_in(int l)
{
if(l>=size) // if l greater than or equal to size (as index start at 0)
throw l;
else
{
cout<<"Elements : ";
for(int i=0;i<=l;i++) //if we have say l=2 ,array values in indexes 0,1and 2 will be displayed
cout<<array[i];
}
}
int main ()
{
int l;
cout<<"Enter index : ";
cin>>l; //till this index value, the array value will be displayed
try
{
disp_in(l);
}
catch(int e)
{
cout<<"Index value greater than max index";
}
return 0;
}
You could try something like this:
#include <vector>
#include <iostream>
#include <algorithm>
#include <iterator>
void print_numbers( const std::vector<int>& array, int nNumbers, const char* pszSeparator )
{
if ( nNumbers > static_cast<int>(array.size()) )
{
throw std::exception();
}
std::copy( array.begin(), array.begin() + nNumbers, std::ostream_iterator<int>( std::cout, pszSeparator ) );
}
int main()
{
std::vector<int> array( 10 );
//Just for testing
{
int n = 0;
auto generator = [n]() mutable
{
return n++;
};
std::generate_n( array.begin(), array.size(), generator );
}
try
{
print_numbers(array, 11, "\n");
}
catch ( std::exception e )
{
std::cout << "Error message..." << std::endl;
}
return 0;
}
I have to write a program using this exact .h file not modifying anything. It must overload a few operators and be able to add polynomials.
Here is the .h file.
#ifndef H_polyClass
#define H_polyClass
#include <iostream>
using namespace std;
class polyClass
{
friend ostream& operator << (ostream&, const polyClass&);
//Overload the extraction operator
//Output format example: 5x^2 - 3x + 6
public:
void setPoly ( int myArray [], int myterms);
//Function to set the polynomial according to the parameter.
//Postcondition: polyArray = myArray; terms = myterms;
polyClass ( int myArray [], int myterms);
//Constructor
//Initializes the polynomial according to the parameter.
//Postcondition: polyArray = myArray; terms = myterms;
polyClass ();
//Default Constructor
//Initializes the polynomial.
//Postcondition: polyArray = NULL; terms = 0;
polyClass operator+ (const polyClass& otherPoly) const;
//overload the operator +
bool operator == (const polyClass& otherPoly) const;
//overload the operator ==
private:
int *polyArray; //pointer to an array that holds the term coefficients.
int terms; //the number of terms in the polynomial
};
#endif
Here is the .cpp file.
#include <iostream>
#include "polyClass.h"
using namespace std;
ostream &operator<<(ostream &out, const polyClass &poly)
{
out << poly.polyArray[1];
if(poly.terms > 1)
out << poly.polyArray[2];
if(poly.terms > 2){
for(int i = 2; i < poly.terms; i++){
out << poly.polyArray[i] << "x^" << i;
}
}
return out;
}
void polyClass::setPoly(int myArray[], int myterms)
{
for(int i = 0; i < myterms; i++)
polyArray[i] = myArray[i];
terms = myterms;
}
polyClass::polyClass(int myArray[], int myterms)
{
for(int i = 0; i < myterms; i++)
polyArray[i] = myArray[i];
terms = myterms;
}
polyClass::polyClass()
{
polyArray = NULL;
terms = 0;
}
polyClass polyClass::operator+(const polyClass &otherPoly) const
{
int size;
if(this->terms >= otherPoly.terms)
size = this->terms;
else
size = otherPoly.terms;
int *newArray = new int[size];
for(int i = 0; i < size; i++)
newArray[i] = otherPoly.polyArray[i] + this->polyArray[i];
return polyClass(newArray, size);
}
bool polyClass::operator==(const polyClass &otherPoly) const
{
if(otherPoly.terms != this->terms){
return false;
}
else if(otherPoly.terms == this->terms){
for(int i = 0; i < otherPoly.terms; i++){
if(polyArray[i] == this->polyArray[i])
return true;
}
}
else{
return false;
}
}
And so far this is my test.cpp file.
#include <iostream>
#include "polyClass.h"
using namespace std;
int main()
{
int myarray[] = {2,4,5};
int myarray2[] = {2,4,5};
int myarray3[] = {2,4};
polyClass p1(myarray, 3);
polyClass p2(myarray2, 3);
polyClass p3(myarray3, 2);
cout << "PolyClass p1 is : " << p1 << endl;
return 0;
}
The only error I'm getting using g++ is segmentation fault. I'm sure I have messed up some pointer array but I'm not sure. There is probably a few issues.
You never allocate memory for your int*.
polyClass::polyClass(int myArray[], int myterms)
{
for(int i = 0; i < myterms; i++)
polyArray[i] = myArray[i];
terms = myterms;
}
polyArray is not initialized here, but you try to access its elements.
Allocate memory for it first:
polyClass::polyClass(int myArray[], int myterms)
{
polyArray = new int[myterms]; //allocate memory here
for(int i = 0; i < myterms; i++)
polyArray[i] = myArray[i];
terms = myterms;
}