I am not sure of the syntax to retrieve a number from any element of the class array. It would also be great if you tell me where I could make the code more efficient according to the specifications. I would really appreciate the help. Thanks. The code is below. I've changed the data type of the class array to "double" to hold the floating point numbers but it is still just taking integers. Thanks in advance for your help.
//Header file
#ifndef NumberList_H // include guard to keep from being run twice
#define NumberList_H // defines class
class NumberList
{
private:
int *list; //Pointer to the array
int numElements; //Number of elements
public:
bool isValid(int); //Validates subscript
NumberList(int); //Constructor
~NumberList();//Destructor
void setElement(int, int);
int getElement(int); //Returns an element
// Mutators used to alter variables
void setHighest(double); // set the highest value
void setLowest(double); // set the lowest value
void setAverage(double); // set the average value
// Accessors used to get the variables
double getHighest() const;
double getLowest() const;
double getAverage() const;
};
#endif // closes guard
//Implementation code
#include <iostream>
#include <cstdlib>
#include "numberList.h"
using namespace std;
//The constructor accepts integer argument and dynamically allocate the array
//to hold that many numbers
// The default constructor
NumberList::NumberList(int size)
{
list = new int[size]; // points to a dynamically allocated array of integers
numElements = size; // number of elements in the array
for(int index=0; index<size; index++)
list[index]=0;
}
//The destructor releases allocated memory
NumberList::~NumberList()
{
delete []list;
}
//isValid member function
bool NumberList::isValid(int element)
{
bool status;
if(element < 0 || element >= numElements)
status=false;
else
status = true;
return status;
}
//setElement member function
void NumberList::setElement(int element, int value)
{
if (isValid(element))
list[element]=value;
else
{
cout<<"Error: Invalid subscript\n";
exit(EXIT_FAILURE);
}
}
//getElement member function
int NumberList::getElement(int element)
{
if(isValid(element))
return list[element];
else
{
cout<<"Error: Invalid subscript\n";
exit(EXIT_FAILURE);
}
}
//getHighest
double NumberList::getHighest() const
{
int count; // local to count in loop
double highest; // to hold highest
// set the first array element to highest
highest = list[0];
// step through array size to compare
for (count =1; count < numElements; count++)
{
if (list[count] > highest)
{
// stores the highest number
highest = list[count];
}
}
return highest;
}
// getLowest
double NumberList::getLowest() const
{
int count; // local to count in loop
double lowest; // to hold lowest
// set the first array element to lowest
lowest = list[0];
// step through array size to compare
for (count = 1; count < numElements; count++)
{
if (list[count] < lowest)
{
// stores the lowest number
lowest = list[count];
}
}
return lowest;
}
// only return value
double NumberList::getAverage() const
{
double total = 0.0; // accumulator for function
int count; // local to count in loop
double average=0.0; // to hold average
// step through array size to add up numbers
for (count = 1; count < numElements; count++)
{
total =+ list[count];
}
average = (total/count);
return average;
}
//Function Main
#include <iostream>
#include <iomanip>
#include "numberList.h"
using namespace std;
// Main to run program
int main()
{
const int SIZE=3;
NumberList numbers(SIZE);
int val, x;
numbers.setElement(2,14);
for(x=0; x<SIZE;x++)
{
numbers.setElement(0,9);//store a number in any element of the array
cout<<" "<<numbers.getElement(x)<<endl;
}
// Display the Array's data.
cout << "\t Array Information \n";
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << "\t Highest : " << numbers.getHighest() << endl;
cout << "\t Lowest: " << numbers.getLowest() << endl;
cout << "\t Average: " << numbers.getAverage() << endl;
cout << endl;
cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout << endl;
cin.get();
}
This post has been edited by Jin_minx: 03 February 2010 - 03:19 AM

New Topic/Question
Reply




MultiQuote







|