Skip to content

Commit

Permalink
WIP on ex7mod03
Browse files Browse the repository at this point in the history
  • Loading branch information
loyc12 committed Jan 23, 2024
1 parent d97d329 commit e6ec055
Show file tree
Hide file tree
Showing 6 changed files with 305 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# 42
*.DS_Store
*.vscode
*Run

# Prerequisites
*.d
Expand Down
2 changes: 1 addition & 1 deletion exTemplate/Template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Template::Template( const Template &other )
std::cout << "[ Called copy constr. for a TEMPLATE instance ]\n";
this->name = other.getName();
}
Template::~Template() { std::cout << "[ Destroying a TEMPLATE instance ]\n"; }

Template &Template::operator= ( const Template &other )
{
Expand All @@ -27,7 +28,6 @@ Template &Template::operator= ( const Template &other )
return *this;
}

Template::~Template() { std::cout << "[ Destroying a TEMPLATE instance ]\n"; }


// Setters - Getters
Expand Down
109 changes: 109 additions & 0 deletions mod7/ex02/Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#include "Array.hpp"


// Constructors - Destructor

Array::Array()
{
std::cout << "[ Called def. constr. for an ARRAY instance ]\n";

this->size = 0;
this->arr = new int[ this->size ];

}
Array::Array( int s )
{
std::cout << "[ Called param. cons. for an ARRAY instance ]\n";

this->size = s;
this->arr = new int[ this->size ];

for ( int i = 0; i < this->size; i++ )
this->arr[ i ] = 0;
}
Array::Array( const Array &other )
{
std::cout << "[ Called copy constr. for an ARRAY instance ]\n";

this->size = other.getSize();
this->arr = new int[ this->size ];

for ( int i = 0; i < this->size; i++ )
this->arr[ i ] = other.getValue( i );
}
Array::~Array()
{
std::cout << "[ Destroying an ARRAY instance ]\n";

delete [] this->arr;
}


// Operator Overloads

Array &Array::operator= ( const Array &other )
{
std::cout << "[ Called assign. op. for an ARRAY instance ]\n";

delete [] this->arr;

this->size = other.getSize();
this->arr = new int[ this->size ];

for ( int i = 0; i < this->size; i++ )
this->arr[ i ] = other.getValue( i );

return *this;
}
int &Array::operator[] ( int i )
{
//std::cout << "[ Called subscript. op for an ARRAY instance ]\n";

return this->arr[ this->getIndex( i )];
}


// Setters - Getters

int Array::getIndex( int i ) const
{
if ( i < -this->size || i >= this->size )
throw InvalidIndex();

else if ( i < 0 ) // allows reading the array backwards via negative indexes
return this->size + i;

else
return i;
}

void Array::setSize( int s )
{
if ( s < 0 )
throw InvalidSize();

int *tmp = new int[ s ];

for ( int i = 0; i < this->size; i++ )
tmp[ this->getIndex( i ) ] = this->arr[ this->getIndex( i )];

delete [] this->arr;

this->size = s;
this->arr = tmp;
}
void Array::setValue( int i, int v ) { this->arr[ this->getIndex( i )] = v; }

int Array::getSize( void ) const { return this->size; }
int Array::getValue( int i ) const { return this->arr[ this->getIndex( i )]; }



std::ostream &operator<< (std::ostream &out, const Array &rhs)
{
out << "[ ";
for ( int i = 0; i < rhs.getSize(); i++ )
out << rhs.getValue( i ) << " ";
out << "]";
return (out);
}
54 changes: 54 additions & 0 deletions mod7/ex02/Array.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#ifndef ARRAY_HPP
# define ARRAY_HPP

# include <exception>
# include <iostream>
# include <sstream>
# include <string>

class Array
{
private:
// Attributes
int *arr;
int size;

protected:
int getIndex( int i ) const;

// Nested Classes
class InvalidSize : public std::exception
{
public:
virtual const char *what() const throw() { return "array error : size cannot be negative"; }
};
class InvalidIndex : public std::exception
{
public:
virtual const char *what() const throw() { return "array error : index must be within bounds"; }
};

public:
// Constructors - Destructor
Array();
Array( const int s );
Array( const Array &other );
~Array();

// Operator Overloads
Array &operator= ( const Array &other );
int &operator[] ( int i );

// Setters - Getters
void setSize( int s );
void setValue( int i, int v );

int getSize( void ) const;
int getValue( int i ) const;


};

std::ostream &operator<< (std::ostream &out, const Array &rhs);

#endif // ARRAY_HPP
1 change: 1 addition & 0 deletions mod7/ex02/Settings.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ NAME = Run

# Source file names (prefix their subdir if needed)
FILES = main \
Array \

# Directory names
SRCDIR =
Expand Down
149 changes: 139 additions & 10 deletions mod7/ex02/main.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,159 @@
#include <iostream>
#include "Array.hpp"

template < typename T > void print( T const &i ) { std::cout << i << std::endl; }
#define MAX_VAL 10
/*
int intTester()
{
Array<int> numbers(MAX_VAL);
int* mirror = new int[MAX_VAL];
srand(time(NULL));
for (int i = 0; i < MAX_VAL; i++)
{
const int value = rand();
numbers[i] = value;
mirror[i] = value;
}
//SCOPE
{
Array<int> tmp = numbers;
Array<int> test(tmp);
}
for (int i = 0; i < MAX_VAL; i++)
{
if (mirror[i] != numbers[i])
{
std::cerr << "didn't save the same value!!" << std::endl;
return 1;
}
}
try
{
numbers[-2] = 0;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
try
{
numbers[MAX_VAL] = 0;
}
catch(const std::exception& e)
{
std::cerr << e.what() << '\n';
}
template < typename T > void iter( T *ptr, size_t len, void ( *f )( T const & ) )
for (int i = 0; i < MAX_VAL; i++)
{
numbers[i] = rand();
}
delete [] mirror;//
return 0;
} */
void intTester()
{
T *arr = reinterpret_cast< T * >( ptr );
for ( size_t i = 0; i < len; i++ )
f( arr[ i ]);
Array numbers(MAX_VAL);
int* mirror = new int[MAX_VAL];
srand(time(NULL));
std::cout << "\nArray : " << numbers << std::endl;

for (int i = 0; i < MAX_VAL; i++)
{
const int value = rand() % 256;
numbers[i] = value;
mirror[i] = value;
}
std::cout << "Array : " << numbers << std::endl << std::endl;

//SCOPE
{
Array tmp = numbers;
Array test(tmp);
}

for (int i = 0; i < MAX_VAL; i++)
{
if (mirror[i] != numbers[i])
{
std::cerr << "\ndidn't save the same value!!" << std::endl;
return;
}
}

try {
std::cout << "\nTrying to access index MIN_VAL - 1" << std::endl;
int value = numbers[-MAX_VAL - 1];
std::cout << "Value : " << value << std::endl;
numbers[-MAX_VAL - 1] = value;
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; }

try {
std::cout << "\nTrying to access index MIN_VAL" << std::endl;
int value = numbers[-MAX_VAL];
std::cout << "Value : " << value << std::endl;
numbers[-MAX_VAL] = value;
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; }

try {
std::cout << "\nTrying to access index -1" << std::endl;
int value = numbers[-1];
std::cout << "Value : " << value << std::endl;
numbers[-1] = value;
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; }

try {
std::cout << "\nTrying to access index 0" << std::endl;
int value = numbers[0];
std::cout << "Value : " << value << std::endl;
numbers[0] = value;
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; }

try {
std::cout << "\nTrying to access index MAX_VAL - 1" << std::endl;
int value = numbers[MAX_VAL - 1];
std::cout << "Value : " << value << std::endl;
numbers[MAX_VAL - 1] = value;
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; }

try {
std::cout << "\nTrying to access index MAX_VAL" << std::endl;
int value = numbers[MAX_VAL];
std::cout << "Value : " << value << std::endl;
numbers[MAX_VAL] = value;
} catch(const std::exception& e) { std::cerr << e.what() << '\n'; }

std::cout << "\nTrying to reassign values forwards" << std::endl;
for (int i = 0; i < MAX_VAL; i++) { numbers[i] = rand() % 256; }
std::cout << "Array : " << numbers << std::endl;


std::cout << "\nTrying to reassign values backwards" << std::endl;
for (int i = 0; i > -MAX_VAL; --i) { numbers[i] = rand() % 256; }
std::cout << "Array : " << numbers << std::endl;

delete [] mirror;

std::cout << std::endl;
}

void runTests( void )
{
std::cout << "\nO================================ TEST 1 ================================O\n" << std::endl;
{
int arr[] = { 1, 2, 3, 4, 5 };
iter( arr, 5, print);

}
std::cout << "\nO================================ TEST 2 ================================O\n" << std::endl;
{
float arr[] = { 1.1f, 2.2f, 3.3f, 4.4f, 5.5f };
iter( arr, 5, print);

}
std::cout << "\nO================================= END ==================================O\n\n" << std::endl;
}

int main( void )
{
runTests();
//runTests();
intTester();
//TemplateTester();
}

0 comments on commit e6ec055

Please sign in to comment.