Commit 1593c26e authored by Rachel Wil Sha Singh's avatar Rachel Wil Sha Singh 💬
Browse files

Smart array

parent 55bd887f
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Smart Array" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Smart Array" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Smart Array" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Extensions />
</Project>
</CodeBlocks_project_file>
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="Smart Array" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/Smart Array" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
<Target title="Release">
<Option output="bin/Release/Smart Array" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Release/" />
<Option type="1" />
<Option compiler="gcc" />
<Compiler>
<Add option="-O2" />
</Compiler>
<Linker>
<Add option="-s" />
</Linker>
</Target>
</Build>
<Compiler>
<Add option="-Wall" />
</Compiler>
<Unit filename="SmartArray.hpp" />
<Unit filename="main.cpp" />
<Extensions />
</Project>
</CodeBlocks_project_file>
# depslib dependency file v1.0
1612314281 source:/home/wilsha/RACHEL/_ADULTING/TEACHING/2021-01_SPRING/cs250-public/example-code/Smart Array/Smart Array/main.cpp
<iostream>
"SmartArray.hpp"
1612314319 /home/wilsha/RACHEL/_ADULTING/TEACHING/2021-01_SPRING/cs250-public/example-code/Smart Array/Smart Array/SmartArray.hpp
<stdexcept>
#ifndef _SMART_ARRAY_HPP
#define _SMART_ARRAY_HPP
#include <stdexcept>
template <typename T>
class SmartArray
{
public:
SmartArray();
void Add( T value );
void Remove( int index );
void Remove( T value );
int Search( T value );
T Get( int index );
void Display();
void Clear();
int Size();
private:
T m_arr[10];
int m_itemCount;
const int ARRAY_SIZE;
};
template <typename T>
SmartArray<T>::SmartArray()
: ARRAY_SIZE( 10 )
{
m_itemCount = 0;
}
template <typename T>
void SmartArray<T>::Add( T value )
{
if ( m_itemCount == ARRAY_SIZE )
{
// The array is full; can't take any more items!
throw runtime_error( "Array is full!" );
}
else if ( value == T() )
{
// Don't allow user to insert empty items!
throw logic_error( "Can't add empty stuff to array!" );
}
m_arr[m_itemCount] = value;
m_itemCount++;
}
template <typename T>
void SmartArray<T>::Remove( int index )
{
if ( index < 0 || index >= ARRAY_SIZE )
{
throw range_error( "Index is out of bounds!" );
}
for ( int i = index; i < m_itemCount - 1; i++ )
{
m_arr[i] = m_arr[i+1];
}
m_itemCount--;
}
template <typename T>
void SmartArray<T>::Remove( T value )
{
int index = Search( value );
Remove( index );
}
template <typename T>
int SmartArray<T>::Search( T value )
{
for ( int i = 0; i < m_itemCount; i++ )
{
if ( m_arr[i] == value )
{
return i;
}
}
throw runtime_error( "Item not found in array!" );
}
template <typename T>
T SmartArray<T>::Get( int index )
{
if ( index < 0 || index >= ARRAY_SIZE )
{
throw range_error( "Index is out of bounds!" );
}
return m_arr[ index ];
}
template <typename T>
void SmartArray<T>::Display()
{
for ( int i = 0; i < m_itemCount; i++ )
{
cout << i << " = " << m_arr[i] << endl;
}
}
template <typename T>
void SmartArray<T>::Clear()
{
m_itemCount = 0;
}
template <typename T>
int SmartArray<T>::Size()
{
return m_itemCount;
}
#endif
#include <iostream>
using namespace std;
#include "SmartArray.hpp"
int main()
{
SmartArray<string> studentList;
studentList.Add( "Nick" );
studentList.Add( "Abhi" );
studentList.Add( "Mike" );
studentList.Add( "Julie" );
studentList.Add( "Rai" );
studentList.Add( "Asha" );
bool done = false;
while ( !done )
{
cout << endl << "------------------------" << endl;
cout << "1. Add item" << endl;
cout << "2. Get size" << endl;
cout << "3. Display array" << endl;
cout << "4. Search" << endl;
cout << "5. Get" << endl;
cout << "6. Clear" << endl;
cout << "7. Remove Index" << endl;
cout << "8. Remove Name" << endl;
int choice;
cin >> choice;
if ( choice == 1 )
{
try
{
cout << "Enter a student name: ";
string name;
cin.ignore();
getline( cin, name );
studentList.Add( name );
}
catch ( const runtime_error& ex )
{
cout << "ERROR: " << ex.what() << endl;
}
catch ( const logic_error& ex )
{
cout << "ERROR: " << ex.what() << endl;
}
}
else if ( choice == 2 )
{
cout << "We have " << studentList.Size()
<< " items stored in the array" << endl;
}
else if ( choice == 3 )
{
studentList.Display();
}
else if ( choice == 4 )
{
cout << "Enter a student name: ";
string name;
cin.ignore();
getline( cin, name );
try
{
int index = studentList.Search( name );
cout << "Found at " << index << endl;
}
catch( const runtime_error& ex )
{
cout << "ERROR: " << ex.what() << endl;
}
}
else if ( choice == 5 )
{
cout << "Enter an index: ";
int index;
cin >> index;
try
{
string value = studentList.Get( index );
cout << "Student at index " << index
<< " is " << value << endl;
}
catch( const range_error& ex )
{
cout << "ERROR: " << ex.what() << endl;
}
}
else if ( choice == 6 )
{
studentList.Clear();
}
else if ( choice == 7 )
{
cout << "Enter an index: ";
int index;
cin >> index;
try
{
studentList.Remove( index );
}
catch( const range_error& ex )
{
cout << "ERROR: " << ex.what() << endl;
}
}
else if ( choice == 8 )
{
cout << "Enter a student name: ";
string name;
cin.ignore();
getline( cin, name );
try
{
studentList.Remove( name );
}
catch( const runtime_error& ex )
{
cout << "ERROR: " << ex.what() << endl;
}
}
}
return 0;
}
#include <iostream>
using namespace std;
template <typename T>
void OutputArray( T arr[], int size )
{
for ( int i = 0; i < size; i++ )
{
cout << "arr[" << i << "] = " << arr[i] << endl;
}
}
template <typename T>
T Sum( T arr[], int size )
{
T sum = 0;
for ( int i = 0; i < size; i++ )
{
sum += arr[i];
}
return sum;
}
int main()
{
cout << "SUMMING AN INT ARRAY" << endl;
int intArr[] = { 1, 2, 3, 4, 5 };
int intSum = Sum( intArr, 5 );
OutputArray( intArr, 5 );
cout << "Sum is " << intSum << endl << endl;
cout << "SUMMING A FLOAT ARRAY" << endl;
float floatArr[] = { 0.25, 0.35, 0.47, 0.33 };
float floatSum = Sum( floatArr, 4 );
OutputArray( floatArr, 4 );
cout << "Sum is " << floatSum << endl;
return 0;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment