NumberArrayClass.cpp
NumberArrayClassDemo.cpp
NumberArrayClass.h
/* NumberArray.h - NumberArrayClass class specifictation file. */
#ifndef NUMBER_ARRAY_CLASS_H
#define NUMBER_ARRAY_CLASS_H
#include <memory>
class NumberArray
{
private:
int length; // The length of the array
int position; // Holds the position, given as array index, to store a number at
int count; // Keeps track of the number of numbers stored in the array
float *numbers; // A dynamically allocated array to hold numbers
float highestNum; // The highest number in the array
float lowestNum; // The lowest number in the array
float total; // Holds the total sum of all numbers in the array
float average; // Holds the average of all numbers stored in the array
// Initializes the array elements to 0. There is no outside access to this function.
void initNums(int len)
{
length = len;
numbers = new float[length];
for (int i = 0; i < length; i++)
numbers[i] = 0.0;
}
public:
NumberArray(); // The default constructor
NumberArray(int); // A constructor accepting an integer value
~NumberArray() // The default destructor frees the memory
{
delete[] numbers;
numbers = nullptr;
}
// Mutators
bool setPosition(int);
bool setNumbers(float);
float getHighest();
float getLowest();
float getTotal();
float getAverage();
// Accessor
void printArray() const;
void displayNumbers(NumberArray *) const;
int getLength() const;
int countNumbers() const;
int getPosition() const;
bool displaySingle(int) const;
};
#endif
NumberArrayClass.cpp
/* NumberArrayClass.cpp - Implementation file for the NumberArrayClass class. */
#include <string>
#include <iostream>
#include "NumberArrayClass.h"
/* **********************************************************
NumberArray::NumberArray()
The default constructor initializing member variables to
their default values.
********************************************************** */
NumberArray::NumberArray()
{
length = 0;
position = 0;
count = 0;
highestNum = 0.0;
lowestNum = 0.0;
average = 0.0;
}
/* **********************************************************
NumberArray::NumberArray()
This constructor takes an integer argument, indicating the
number of elements to be stored in the array, as argument.
The class private member function initArray is called, and
memory is pre-allocated to hold that many elements.
********************************************************** */
NumberArray::NumberArray(int numEls)
{
initNums(numEls);
}
/* **********************************************************
NumberArray::setposition()
This function sets the position in the array in which a
number should be stored. If the position passed to it is
greater than 0 and lower than the length of the array, the
number is stored at that position. Else the user is shown
a message indicating that the position is invalid.
********************************************************** */
bool NumberArray::setPosition(int pos)
{
if (pos >= 0 && pos <= length)
{
position = pos;
return true;
}
else
{
std::cout << "\nThis position is invalid\n";
}
return false;
}
/* **********************************************************
NumberArray::setNumbers(accepts a float)
This function copies a number passed to it into the array
position indicated by the user, if the number currently
stored at that position is 0. If there is already a number
stored at that position, or a negative number is entered,
or there are no more free positions available to store a
number at, messages are displayed and negative is returned
from this function
********************************************************** */
bool NumberArray::setNumbers(float num)
{
if (countNumbers() != length)
{
if (numbers[getPosition()] == 0 && num > 0 && position <= length)
{
count++;
numbers[getPosition()] = num;
return true;
}
else if (numbers[getPosition()] > 0 && position <= length)
{
std::cout << "\nThis position already contains a number!\n";
count += 0;
return false;
}
else /*if (numbers[getPosition()] == 0 && num < 0 && position <= length)*/
{
std::cout << "\nOnly positive numbers are allowed!\n";
count += 0;
}
}
else
{
count += 0;
std::cout << "\nThere are no more positions availabale ..\n";
}
return false;
}
/* **********************************************************
NumberArray::displaySingle()
This function retrieves and displays a number, stored at
the index position passed to this function. If the index
does not exist, a message is displayed.
********************************************************** */
bool NumberArray::displaySingle(int pos) const
{
if ((pos) < 1 || (pos) > length)
{
std::cout << "Valid positions are 1 through " << length << "\n";
return false;
}
else
{
std::cout << "\nThe number stored at position: " << pos
<< " is " << numbers[pos-1] << "\n";
return true;
}
}
/* **********************************************************
NumberArray::printArray()
This function outputs the whole array to screen.
********************************************************** */
void NumberArray::printArray() const
{
for (int i = 0; i < length; i++)
{
std::cout << "pos: " << (i + 1) << " " << numbers[i] << "\n";
}
}
/* **********************************************************
NumberArray::displayNumbers()
This function accepts a pointer to a NumberArray object.
It calls a function that displays the array's contents.
********************************************************** */
void NumberArray::displayNumbers(NumberArray *numbers) const
{
std::cout << "\nThese are the numbers currently stored in your list:\n";
numbers->printArray();
}
/* **********************************************************
NumberArray::getHighest()
This function finds the highest number stored in the array
and returns it.
********************************************************** */
float NumberArray::getHighest()
{
highestNum = numbers[0];
for (int i = 1; i < length; i++)
{
if (numbers[i] > highestNum)
{
highestNum = numbers[i];
}
}
return highestNum;
}
/* **********************************************************
NumberArray::getLowest()
This function finds and returns the lowest number stored
in the array.
********************************************************** */
float NumberArray::getLowest()
{
lowestNum = numbers[0];
for (int i = 1; i < length; i++)
{
if (numbers[i] < lowestNum)
{
lowestNum = numbers[i];
}
}
return lowestNum;
}
/* **********************************************************
NumberArray::getTotal()
A helper function that caluclates the sum-total of all
numbers stored in the array and returns it.
********************************************************** */
float NumberArray::getTotal()
{
total = 0.0;
for (int i = 0; i < length; i++)
{
total += numbers[i];
}
return total;
}
/* **********************************************************
NumberArray::getAverage()
This function calculates and returns the average of all
numbers stored in the array.
********************************************************** */
float NumberArray::getAverage()
{
return average = (getTotal() / count);
}
/* **********************************************************
NumberArray::countNumbers()
This function keeps track of the number of numbers stored
in the array. The count is returend.
********************************************************** */
int NumberArray::countNumbers() const
{ return count; }
/* **********************************************************
NumberArray::getPosition()
This function returns the current position in the array.
********************************************************** */
int NumberArray::getPosition() const
{
return (position - 1);
}
/* **********************************************************
NumberArray::getLength()
This function returns the array's length.
********************************************************** */
int NumberArray::getLength() const
{
return length;
}
NumberArrayClassDemo.cpp
/* NumberArrayClassDemo.cpp - This program demonstrates the NumberArrayClass class. */
#include <string>
#include <iostream>
#include "NumberArrayClass.h"
enum MENU_ITEMS { ENTER_NUM = 'E', RETRIEVE_NUM = 'R', HIGHEST_NUM = 'H',
DISPLAY_NUMS = 'D', LOWEST_NUM = 'L', AVERAGE_NUM = 'A', QUIT = 'Q' };
char options();
void menu(int);
int main()
{
int numEls = 0;
std::cout << "NUMBER ARRAY DEMO\n\n";
std::cout << "How many numbers do you wish to store? ";
std::cin >> numEls;
menu(numEls);
std::cin.get();
std::cin.ignore();
return 0;
}
char options()
{
char mC = ' ';
std::cout << "\nNumber Array Demo\n\n"
<< "[E]nter Number\n"
<< "[D]isplay Number List\n"
<< "[R]etrieve Single Number\n"
<< "[A]verage of All Numbers\n"
<< "[H]ighest Number\n"
<< "[L]owest Number\n"
<< "[Q]uit\n\n";
std::cout << "Please Enter Your Choice: ";
std::cin >> mC;
while (toupper(mC) < ENTER_NUM && toupper(mC) > QUIT)
{
std::cout << "\nPlease Enter Your Choice: ";
std::cin >> mC;
}
return toupper(mC);
}
void menu(int numEls)
{
float num = 0.0;
int pos = 0;
char mC = ' ';
NumberArray *numPtr = new NumberArray(numEls);
do
{
mC = options();
std::cout << "\n";
switch (mC)
{
case ENTER_NUM:
{
std::cout << "Position: ";
std::cin >> pos;
while (numPtr->setPosition(pos) == false)
{
std::cout << "Position: ";
std::cin >> pos;
}
std::cout << "Number: ";
std::cin >> num;
while (numPtr->setNumbers(num) == false && numPtr->countNumbers() != numPtr->getLength())
{
std::cout << "Position: ";
std::cin >> pos;
while (numPtr->setPosition(pos) == false)
{
std::cout << "Position: ";
std::cin >> pos;
}
std::cout << "Number: ";
std::cin >> num;
}
} break;
case RETRIEVE_NUM:
{
std::cout << "\nWhich position do you wish to display? ";
std::cin >> pos;
while (numPtr->displaySingle(pos) == false)
{
std::cout << "Which position do you wish to display? ";
std::cin >> pos;
}
} break;
case DISPLAY_NUMS:
{
numPtr->displayNumbers(numPtr);
} break;
case HIGHEST_NUM:
{
std::cout << "\nThe highest number is: " << numPtr->getHighest() << "\n";
} break;
case LOWEST_NUM:
{
std::cout << "\nThe lowest number is: " << numPtr->getLowest() << "\n";
} break;
case AVERAGE_NUM:
{
std::cout << "\nThe average of all numbers is: " << numPtr->getAverage() << "\n";
} break;
case QUIT:
{
std::cout << "\nThank you for trying this demo! Have a nice day!";
} break;
}
} while (toupper(mC) != QUIT);
delete numPtr;
}
No comments:
Post a Comment