class Numbers {
private:
double *arr;
int size;
public:
//constructor
Numbers(int n) {
//check for input
if (n < 1) {
cout << "There should be at least 1 element";
cout << " in array!\n";
//terminate program immediately
exit(EXIT_FAILURE);
}
//otherwise
size = n;
//dynamically allocate array of
//doubles of size n
arr = new double[size];
//initialize all of the values to 0
//so they don't contain "garbage"
for (int i = 0; i < size; i++) {
*(arr + i) = 0;
}
}
//function to store a number in any
//position in the array
void storeAtPosition(int pos, double d) {
//perform bounds checking
if (pos <= 0 || pos > size) {
cout << "Position should be between 1 ";
cout << "and " << size << endl;
return;
}
//subtract 1 from pos to save it
//to the correct position in array
*(arr + pos - 1) = d;
}
//function to get element at any position
double getElementAt(int pos) const {
//perform bounds checking
while (pos <= 0 || pos > size) {
cout << "Position should be between 1 ";
cout << "and " << size << endl;
cin >> pos;
}
//subtract 1 from pos to get to
//the correct position in array
return *(arr + pos - 1);
}
//function to get highest element in array
double getHighest() const {
double highest;
//initialize it to first element
highest = *(arr);
//step though all elements of array
//and update highest as necessary
for (int i = 0; i < size; i++) {
if (*(arr + i) > highest)
highest = *(arr + i);
}
//now highest should hold the
//highest value in the array
return highest;
}
//function to get lowest element in array
double getLowest() const {
double lowest;
//initialize it to first element
lowest = *(arr);
//step though all elements of array
//and update highest as necessary
for (int i = 0; i < size; i++) {
if (*(arr + i) < lowest)
lowest = *(arr + i);
}
//now lowest should hold the
//lowest value in the array
return lowest;
}
//function to return total
double getAverage() const {
double average, total = 0;
//step through the array and get total
for (int i = 0; i < size; i++)
total += *(arr + i);
//get average by typecasting to double
//to prevent integer division
average = (double)total / size;
int main()
{
//create a Numbers object
cout << "Now creating an object that will hold";
cout << " 5 numbers...\n";
Numbers obj1(5);
cout << "Done!\n";
//populate array with values
cout << "\nNow populating array with values:\n";
for (int i = 1; i <= 5; i++) {
cout << "Now writing " << ((17 * i) % (i*i))*1.0;
cout << " at position " << i << "...\n";
obj1.storeAtPosition(i, ((17 * i) % (i*i))*1.0);
}
//variables to hold user choices
int pos;
double num;
cout << "\nNow modify one of the values.\n";
cout << "Enter the number you want to store: ";
cin >> num;
cout << "Now enter the position in which to store it: ";
cin >> pos;
obj1.storeAtPosition(pos, num);
cout << "Done!\n";
//display contents of array in object
cout << "\nNow displaying contents of array...\n";
for (int i = 1; i <= 5; i++) {
cout << "Position " << i << ": ";
cout << obj1.getElementAt(i) << endl;
}
I copied and pasted your code all as one slab and got it to run as shown below. All I did was comment out the #include ... at the ************ line
What you should do is put the numbers.h (and numbers.cpp file if it exists) parts in separate and program accessible file(s). That is if you haven't already done that in which case the reason it doesn't work is the file(s) aren't in the program include path - either make the IDE project changes to the include directory or hard-code the full path in your #include line.
//contents of Numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <iostream>
usingnamespace std;
class Numbers {
private:
double *arr;
int size;
public:
//constructor
Numbers(int n) {
//check for input
if (n < 1) {
cout << "There should be at least 1 element";
cout << " in array!\n";
//terminate program immediately
exit(EXIT_FAILURE);
}
//otherwise
size = n;
//dynamically allocate array of
//doubles of size n
arr = newdouble[size];
//initialize all of the values to 0
//so they don't contain "garbage"
for (int i = 0; i < size; i++) {
*(arr + i) = 0;
}
}
//function to store a number in any
//position in the array
void storeAtPosition(int pos, double d) {
//perform bounds checking
if (pos <= 0 || pos > size) {
cout << "Position should be between 1 ";
cout << "and " << size << endl;
return;
}
//subtract 1 from pos to save it
//to the correct position in array
*(arr + pos - 1) = d;
}
//function to get element at any position
double getElementAt(int pos) const {
//perform bounds checking
while (pos <= 0 || pos > size) {
cout << "Position should be between 1 ";
cout << "and " << size << endl;
cin >> pos;
}
//subtract 1 from pos to get to
//the correct position in array
return *(arr + pos - 1);
}
//function to get highest element in array
double getHighest() const {
double highest;
//initialize it to first element
highest = *(arr);
//step though all elements of array
//and update highest as necessary
for (int i = 0; i < size; i++) {
if (*(arr + i) > highest)
highest = *(arr + i);
}
//now highest should hold the
//highest value in the array
return highest;
}
//function to get lowest element in array
double getLowest() const {
double lowest;
//initialize it to first element
lowest = *(arr);
//step though all elements of array
//and update highest as necessary
for (int i = 0; i < size; i++) {
if (*(arr + i) < lowest)
lowest = *(arr + i);
}
//now lowest should hold the
//lowest value in the array
return lowest;
}
//function to return total
double getAverage() const {
double average, total = 0;
//step through the array and get total
for (int i = 0; i < size; i++)
total += *(arr + i);
//get average by typecasting to double
//to prevent integer division
average = (double)total / size;
return average;
}
//destructor
~Numbers() {
//free up allocated memory
delete[] arr;
}
};
#endif
//contents of main.cpp
#include <iostream>
#include <string>
#include <iomanip>
//#include "Numbers.h" /// ************************************
usingnamespace std;
int main()
{
//create a Numbers object
cout << "Now creating an object that will hold";
cout << " 5 numbers...\n";
Numbers obj1(5);
cout << "Done!\n";
//populate array with values
cout << "\nNow populating array with values:\n";
for (int i = 1; i <= 5; i++) {
cout << "Now writing " << ((17 * i) % (i*i))*1.0;
cout << " at position " << i << "...\n";
obj1.storeAtPosition(i, ((17 * i) % (i*i))*1.0);
}
//variables to hold user choices
int pos;
double num;
cout << "\nNow modify one of the values.\n";
cout << "Enter the number you want to store: ";
cin >> num;
cout << "Now enter the position in which to store it: ";
cin >> pos;
obj1.storeAtPosition(pos, num);
cout << "Done!\n";
//display contents of array in object
cout << "\nNow displaying contents of array...\n";
for (int i = 1; i <= 5; i++) {
cout << "Position " << i << ": ";
cout << obj1.getElementAt(i) << endl;
}
//get highest, lowest and average
cout << fixed << setprecision(2);
cout << "\nHighest value: " << obj1.getHighest();
cout << "\nLowest value: " << obj1.getLowest();
cout << "\nAverage: " << obj1.getAverage();
//return 0 to mark successful termination
return 0;
}
Now creating an object that will hold 5 numbers...
Done!
Now populating array with values:
Now writing 0 at position 1...
Now writing 2 at position 2...
Now writing 6 at position 3...
Now writing 4 at position 4...
Now writing 10 at position 5...
Now modify one of the values.
Enter the number you want to store: